# 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: https://www.dataopslabs.com/p/aws-bedrock-learning-series-blog

Blog 2: https://www.dataopslabs.com/p/family-of-titan-text-models-cli-demo

Blog 3: https://www.dataopslabs.com/p/family-of-titan-text-models-boto3

Blog 4: [https://blog.dataopslabs.com/aws-bedrock-boto3-demo-anthropic-claude](https://blog.dataopslabs.com/aws-bedrock-boto3-demo-anthropic-claude)

# Github Link - Notebook

[https://github.com/jayyanar/learning-aws-bedrock/blob/main/blog5/Bedrock\_AI21Labs.ipynb](https://github.com/jayyanar/learning-aws-bedrock/blob/main/blog5/Bedrock_AI21Labs.ipynb)

# Environment Setup

I am using vscode local environment with AWS Credential configured.

### Install Latest Python

```xml
! python --version
Python 3.11.5
```

### Upgrade pip

```xml
! pip install --upgrade pip
```

### Install latest boto3,awscli, boto3-core

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

### Load the Library

```xml
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

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

### Configure the Model configuration

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

### Invoke the Model

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

### Parse the response for Text Completion

```xml
# 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(extracted_text)
else:
    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.*
