AWS Bedrock

Our foundation models models are available through Amazon Bedrock. AI21 Models on Bedrock are hosted on AWS and usage is per token. You do not need to provision or manage these models yourself.

🚧

Limited region support

Bedrock is currently supported only in the region us-east-1.

Usage

You'll be using the AI21 Python SDK to access all AI21 models.

  1. Create an Amazon Bedrock instance and enable access to AI21 models on Bedrock.
  2. Install or update the AI21 Studio Python SDK with the latest AWS integration:
    pip install -U "ai21[AWS] >= 2.13.0"
  3. Create your AI21BedrockClient client.
    • You can either use your current environment's AWS session keys, or else create and pass in a boto3 session explicitly, if you want to manage your keys explicitly. You do not need to use AI21 client keys, only AWS keys, because AWS handles your AI21 authorization for you.
    • The only region currently supported is us-east-1.
  4. Follow the same usage as described in the AI21 Studio Python SDK Guide, but be aware that only certain models are available on Bedrock.

Examples

from ai21 import AI21BedrockClient, BedrockModelID
from ai21.models.chat import ChatMessage

client = AI21BedrockClient(region='us-east-1') # region is optional, as you can use the env variable instead

messages = [
  ChatMessage(content="You are a helpful assistant", role="system"),
  ChatMessage(content="What is the meaning of life?", role="user")
]

response = client.chat.completions.create(
    messages=messages,
    model_id=BedrockModelID.JAMBA_INSTRUCT_V1,
)
from ai21 import AI21BedrockClient, BedrockModelID
from ai21.models.chat import ChatMessage

boto_session = boto3.Session(
    region_name="us-east-1",
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
    aws_session_tokeמ=aws_session_token,
)

client = AI21BedrockClient(session=boto_session, model_id=BedrockModelID.JAMBA_INSTRUCT_V1)

messages = [
  ChatMessage(content="You are a helpful assistant", role="system"),
  ChatMessage(content="What is the meaning of life?", role="user")
]

response = client.chat.completions.create(
    messages=messages,
    model_id=BedrockModelID.JAMBA_INSTRUCT_V1,
)

πŸ“˜

See full usage examples here.

More information