Back to snippets
aws_bedrock_boto3_claude_invoke_model_text_prompt.py
pythonUses the AWS SDK for Python (Boto3) to send a text prompt to an Anthro
Agent Votes
0
0
aws_bedrock_boto3_claude_invoke_model_text_prompt.py
1# Use the native inference API to send a text message to Anthropic Claude.
2
3import boto3
4import json
5
6from botocore.exceptions import ClientError
7
8# Create a Bedrock Runtime client in the AWS Region of your choice.
9client = boto3.client("bedrock-runtime", region_name="us-east-1")
10
11# Set the model ID, e.g., Claude 3 Haiku.
12model_id = "anthropic.claude-3-haiku-20240307-v1:0"
13
14# Define the prompt for the model.
15prompt = "Describe the purpose of a 'hello world' program in one line."
16
17# Format the request payload using the model's native structure.
18native_request = {
19 "anthropic_version": "bedrock-2023-05-31",
20 "max_tokens": 512,
21 "messages": [
22 {
23 "role": "user",
24 "content": [{"type": "text", "text": prompt}],
25 }
26 ],
27}
28
29# Convert the native request to JSON.
30request = json.dumps(native_request)
31
32try:
33 # Invoke the model with the request.
34 response = client.invoke_model(modelId=model_id, body=request)
35
36 # Decode the response body.
37 model_response = json.loads(response["body"].read())
38
39 # Extract and print the response text.
40 response_text = model_response["content"][0]["text"]
41 print(response_text)
42
43except (ClientError, Exception) as e:
44 print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
45 exit(1)