logo
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.

Published Dec 13, 2023

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

I am using vscode local environment with AWS Credential configured.

1
2
! python --version
Python 3.11.5

1
! pip install --upgrade pip

1
2
3
4
! pip install --no-build-isolation --force-reinstall \
"boto3>=1.33.6" \
"awscli>=1.31.6" \
"botocore>=1.33.6"

1
2
3
4
5
6
7
8
9
import json
import os
import sys

import boto3
import botocore

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

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

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

1
2
3
4
5
6
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"
)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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.")

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.