Created
February 18, 2025 20:08
-
-
Save artyom/f4b090ecba242822fb6aeadce7f41afe to your computer and use it in GitHub Desktop.
AWS Bedrock converse API call example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
Related documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse.html