Skip to main content

AWS Bedrock

API KEYS

os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""

Usage

import os 
from litellm import completion

os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""

response = completion(
model="bedrock/anthropic.claude-instant-v1",
messages=[{ "content": "Hello, how are you?","role": "user"}]
)

Supported AWS Bedrock Models

Here's an example of using a bedrock model with LiteLLM

Model NameCommandEnvironment Variables
Anthropic Claude-V2completion(model='bedrock/anthropic.claude-v2', messages=messages)os.environ['ANTHROPIC_ACCESS_KEY_ID'], os.environ['ANTHROPIC_SECRET_ACCESS_KEY']
Anthropic Claude-Instant V1completion(model='bedrock/anthropic.claude-instant-v1', messages=messages)os.environ['ANTHROPIC_ACCESS_KEY_ID'], os.environ['ANTHROPIC_SECRET_ACCESS_KEY']
Anthropic Claude-V1completion(model='bedrock/anthropic.claude-v1', messages=messages)os.environ['ANTHROPIC_ACCESS_KEY_ID'], os.environ['ANTHROPIC_SECRET_ACCESS_KEY']
AI21 J2-Ultracompletion(model='bedrock/ai21.j2-ultra', messages=messages)os.environ['AWS_ACCESS_KEY_ID'], os.environ['AWS_SECRET_ACCESS_KEY'], os.environ['AWS_REGION_NAME']
AI21 J2-Midcompletion(model='bedrock/ai21.j2-mid', messages=messages)os.environ['AWS_ACCESS_KEY_ID'], os.environ['AWS_SECRET_ACCESS_KEY'], os.environ['AWS_REGION_NAME']

Streaming

import os 
from litellm import completion

os.environ["AWS_ACCESS_KEY_ID"] = ""
os.environ["AWS_SECRET_ACCESS_KEY"] = ""
os.environ["AWS_REGION_NAME"] = ""

response = completion(
model="bedrock/anthropic.claude-instant-v1",
messages=[{ "content": "Hello, how are you?","role": "user"}],
stream=True
)

for chunk in response:
print(chunk)

Example Streaming Output Chunk

{
"choices": [
{
"finish_reason": null,
"index": 0,
"delta": {
"content": "ase can appeal the case to a higher federal court. If a higher federal court rules in a way that conflicts with a ruling from a lower federal court or conflicts with a ruling from a higher state court, the parties involved in the case can appeal the case to the Supreme Court. In order to appeal a case to the Sup"
}
}
],
"created": null,
"model": "amazon.titan-tg1-large",
"usage": {
"prompt_tokens": null,
"completion_tokens": null,
"total_tokens": null
}
}