Skip to content

Commit 699f600

Browse files
committed
2025-05-26
1 parent 3b70c2e commit 699f600

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

apigw-lambda-bedrock-sam/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Amazon API Gateway to AWS Lambda to Amazon Bedrock using SAM
22

3-
This sample project deploys an Amazon API Gateway REST API with an AWS Lambda integration. The Lambda function is written in Python, calls the Amazon Bedrock API for Anthropic Claude-v2 model and returns a response containing the generated content.
3+
This sample project deploys an Amazon API Gateway REST API with an AWS Lambda integration. The Lambda function is written in Python, calls the Amazon Bedrock API for Anthropic Claude 3.5 Sonnet model and returns a response containing the generated content.
44

55
Learn more about this pattern at Serverless Land Patterns: serverlessland.com/patterns/apigw-lambda-bedrock
66

@@ -100,16 +100,16 @@ Amazon Bedrock users need to request access to models before they are available
100100
101101
## How it works
102102
103-
This SAM project uses Amazon Bedrock API for Anthropic Claude-v2 model to generate content based on given prompt. This is exposed through a serverless REST API. Please refer to the architecture diagram below:
103+
This SAM project uses Amazon Bedrock API for Anthropic Claude 3.5 Sonnet model to generate content based on given prompt. This is exposed through a serverless REST API. Please refer to the architecture diagram below:
104104
![End to End Architecture](images/architecture.png)
105105
106106
Here's a breakdown of the steps:
107107

108108
1. **Amazon API Gateway**: Receives the HTTP POST request containing the prompt.
109109

110-
2. **AWS Lambda**: Triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using boto3 bedrock-runtime API. It uses Anthropic Claude-v2 model and sets other required parameters to fixed values for simplicity.
110+
2. **AWS Lambda**: Triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using boto3 bedrock-runtime API. It uses Anthropic Claude 3.5 Sonnet model and sets parameters like temperature, max tokens, and other configuration options.
111111

112-
3. **Amazon Bedrock**: Based on the given prompt, using Anthropic Claude-v2 model generates the content and returns the response to Lambda.
112+
3. **Amazon Bedrock**: Based on the given prompt, using Anthropic Claude 3.5 Sonnet model generates the content and returns the response to Lambda.
113113

114114
4. **Response**: Lambda processes the Bedrock output and sends it back to the user via the API Gateway.
115115

apigw-lambda-bedrock-sam/src/bedrock_integration.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,46 @@ def lambda_handler(event, context):
1313
prompt = body["prompt"]
1414
print("Prompt = " + prompt)
1515

16-
# Create the body
17-
body = json.dumps({
18-
'prompt': "\n\nHuman:" + prompt + "\n\nAssistant:",
16+
# Create the request body using the messaging API format for Claude 3.5
17+
request_body = json.dumps({
18+
"anthropic_version": "bedrock-2023-05-31",
19+
"max_tokens": 200,
20+
"messages": [
21+
{
22+
"role": "user",
23+
"content": prompt
24+
}
25+
],
1926
"temperature": 0.5,
2027
"top_p": 1,
21-
"top_k": 250,
22-
"max_tokens_to_sample": 200,
23-
"stop_sequences": ["\n\nHuman:"]
28+
"top_k": 250
2429
})
2530

2631
# Set the model id and other parameters required to invoke the model
27-
model_id = 'anthropic.claude-v2'
32+
model_id = 'anthropic.claude-3-5-sonnet-20240620-v1:0'
2833
accept = 'application/json'
2934
content_type = 'application/json'
3035

3136
# Invoke Bedrock API
32-
response = bedrock.invoke_model(body=body, modelId=model_id, accept=accept, contentType=content_type)
33-
print(response)
34-
37+
response = bedrock.invoke_model(body=request_body, modelId=model_id, accept=accept, contentType=content_type)
38+
3539
# Parse the response body
3640
response_body = json.loads(response.get('body').read())
3741
print(response_body)
42+
43+
# Extract the completion from the messaging API response format
44+
completion = response_body.get('content', [{}])[0].get('text', '')
45+
stop_reason = response_body.get('stop_reason', '')
46+
47+
# Format the response to maintain backward compatibility with the existing API
48+
formatted_response = {
49+
'completion': completion,
50+
'stop_reason': stop_reason
51+
}
3852

3953
return {
4054
'statusCode': 200,
4155
'body': json.dumps({
42-
'generated-text': response_body
56+
'generated-text': formatted_response
4357
})
4458
}

0 commit comments

Comments
 (0)