Created
October 2, 2025 11:29
-
-
Save labeveryday/ee85b3d9a5d5dfcb4bc4197e7537b447 to your computer and use it in GitHub Desktop.
Converse API with Amazon Bedrock API Key exmaple
This file contains hidden or 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
| """ | |
| In this example you get a Bedrock API key from Amazon Bedrock and load it. | |
| It will look like: AWS_BEARER_TOKEN_BEDROCK=bedrock-api-key-......... | |
| """ | |
| import os | |
| import boto3 | |
| from dotenv import load_dotenv | |
| from pprint import pprint | |
| # Load environment variables from a .env file | |
| load_dotenv() | |
| # Create an Amazon Bedrock client | |
| client = boto3.client( | |
| service_name="bedrock-runtime", | |
| region_name="us-east-1" # If you've configured a default region, you can omit this line | |
| ) | |
| # Define the model and message | |
| model_id = "us.anthropic.claude-3-5-haiku-20241022-v1:0" | |
| messages = [{"role": "user", "content": [{"text": "Hello"}]}] | |
| response = client.converse( | |
| modelId=model_id, | |
| messages=messages, | |
| ) | |
| print(f"Agent response: {response['output']['message']['content'][0]['text']}") | |
| print("\n") | |
| pprint(f"Metrics: {response['metrics']}") | |
| print(f"Stop reason: {response['stopReason']}") | |
| pprint(f"Token usage: {response['usage']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment