AWS BedRock - Boto3 Demo - AI21 Labs Models

AWS BedRock - Boto3 Demo - AI21 Labs Models

Explore AI21 Labs Jurassic-2 Ultra, AI21's most advanced model, excels in complex tasks like question answering and summarization. Jurassic-2 Mid, slightly less powerful but cost-effective, suits various language comprehension and generation tasks.

Previous Blog on this Learning Series

Blog 1:

Blog 2:

Blog 3:

Blog 4:

Github Link - Notebook

Environment Setup

I am using vscode local environment with AWS Credential configured.

Install Latest Python

! python --version
Python 3.11.5

Upgrade pip

! pip install --upgrade pip

Install latest boto3,awscli, boto3-core

! pip install --no-build-isolation --force-reinstall \
    "boto3>=1.33.6" \
    "awscli>=1.31.6" \

Load the Library

import json
import os
import sys

import boto3
import botocore

bedrock = boto3.client(service_name="bedrock")
bedrock_runtime = boto3.client(service_name="bedrock-runtime")

AI21 Labs - Jurrasic Ultra Model

Set the Prompt

jurrasic_ultra_prompt = "Write a Article about new services announced by AWS from 2015 to 2020"

Configure the Model configuration

body = json.dumps({
    "prompt": jurrasic_ultra_prompt,
    "temperature":0, #Temperature controls randomness; higher values increase diversity, lower values boost predictability.

Invoke the Model

response = bedrock_runtime.invoke_model(
    modelId="ai21.j2-ultra-v1", # REPLACE WITH ai21.j2-mid-v1 lessthan powerful than Ultra but cost effective
    accept= "*/*", 

Parse the response for Text Completion

# Assuming response.get('body') is a StreamingBody
response_body = json.loads(response.get('body').read())
outputText = response_body.get('completions')

# Check if 'completions' key exists in the response body
if outputText:
    # Assuming outputText is a list of dictionaries, you can iterate through it
    for item in outputText:
        # Assuming 'data' key exists in each dictionary
        data_text = item.get('data', {}).get('text')

        # Check if 'text' key exists in the 'data' dictionary
        if data_text:
            # Extract the text after the newline character and strip any leading/trailing spaces
            extracted_text = data_text[data_text.index('\n') + 1:].strip()
    print("No 'completions' key found in the response body.")

Text Completion

AWS (Amazon Web Services) is a cloud computing platform that offers a variety of services to businesses, including compute, storage, database, analytics, and machine learning. AWS has been adding new services on a regular basis, and in this article, we will take a look at some of the new services that AWS has announced from 2015 to 2020.

In 2015, AWS announced a number of new services, including Amazon Aurora, a MySQL-compatible database; Amazon Elastic Container Service (ECS), a container orchestration service; and Amazon Elastic Container Registry (ECR), a container image registry.

In 2016, AWS announced a number of new services, including Amazon Elastic File System (EFS), a scalable file storage service; Amazon Elastic Container Service for Kubernetes (EKS), a managed Kubernetes service; and Amazon Elastic Container Service (ECS), a container orchestration service.