Skip to content

Instantly share code, notes, and snippets.

@tpai
Created June 17, 2024 16:01
Show Gist options
  • Save tpai/7f3c3de86f01b764bab01a0ecda75749 to your computer and use it in GitHub Desktop.
Save tpai/7f3c3de86f01b764bab01a0ecda75749 to your computer and use it in GitHub Desktop.
AWS Bedrock API call (Claude 3 Sonnet)
import requests
import json
import boto3
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
# AWS credentials
access_key = '<access-key>'
secret_key = '<secret-key>'
region = 'us-west-2'
service = 'bedrock'
model = 'anthropic.claude-3-sonnet-20240229-v1:0'
# Endpoint and model
endpoint = f'https://bedrock-runtime.{region}.amazonaws.com/model/{model}/invoke'
# Payload
payload = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1000,
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "How many colors does rainbow have?"
}
]
}
]
}
# Convert payload to JSON
data = json.dumps(payload)
# Prepare the request
request = AWSRequest(method="POST", url=endpoint, data=data, headers={'Content-Type': 'application/json'})
credentials = boto3.Session(aws_access_key_id=access_key, aws_secret_access_key=secret_key).get_credentials()
SigV4Auth(credentials, service, region).add_auth(request)
prepared_request = request.prepare()
# Send the request
response = requests.post(endpoint, headers=prepared_request.headers, data=data)
# Print the response
print(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment