Skip to content

Instantly share code, notes, and snippets.

@artyom
Created February 18, 2025 20:08
Show Gist options
  • Save artyom/f4b090ecba242822fb6aeadce7f41afe to your computer and use it in GitHub Desktop.
Save artyom/f4b090ecba242822fb6aeadce7f41afe to your computer and use it in GitHub Desktop.
AWS Bedrock converse API call example
#!/usr/bin/env python3
import boto3
from botocore.exceptions import ClientError
client = boto3.client("bedrock-runtime", region_name="us-east-1")
model_id = "us.amazon.nova-micro-v1:0"
user_message = "Describe the purpose of a 'hello world' program in one line."
conversation = [
{
"role": "user",
"content": [{"text": user_message}],
}
]
try:
response = client.converse(
modelId=model_id,
messages=conversation,
system=[{"text":"Keep you responses short"}],
inferenceConfig={"maxTokens": 2500, "temperature": 0.01},
)
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)
except (ClientError, Exception) as e:
print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment