Skip to content

Commit 0ca75de

Browse files
authored
Pattern update - apigw-rest-api-sqs-lambda-bedrock-cdk
Pattern update - apigw-rest-api-sqs-lambda-bedrock-cdk
2 parents 1bf5124 + 8c64695 commit 0ca75de

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

apigw-rest-api-sqs-lambda-bedrock-cdk/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ The following diagram illustrates the solutions architecture
2727
cd apigw-rest-api-sqs-lambda-bedrock-cdk.
2828
```
2929
30-
1. Deploy the stack to your default AWS account and region. The output of this command should give you the REST API URL.
30+
1. Deploy the stack to your default AWS account and region with Amazon Bedrock availability. The output of this command should give you the REST API URL.
3131
```
3232
cd cdk
3333
cdk deploy
3434
```
3535
3636
## How it works
3737
38-
This pattern creates an Amazon API Gateway REST API, an Amazon SQS queue and an AWS Lambda function. AWS Lambda function consumes messages from SQS using [AWS lambda event source mapping](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html). The lambda function then calls Bedrock using the request from SQS. The Lambda function is set with the max concurrency of 10 (configurable through [cdk.context.json](./cdk/cdk.context.json)). This setting is enabled to make sure the requests does not overwhelm Amazon Bedrock.
38+
This pattern creates an Amazon API Gateway REST API, an Amazon SQS queue and an AWS Lambda function. AWS Lambda function consumes messages from SQS using [AWS lambda event source mapping](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html). The lambda function then calls Amazon Bedrock using the request from SQS. The Lambda function is set with the max concurrency of 10 (configurable through [cdk.context.json](./cdk/cdk.context.json)). This setting is enabled to make sure the requests does not overwhelm Amazon Bedrock.
3939
40-
To achieve full functionality with this pattern, you must send the response from Lambda function to client using techniques such as websocket or GraphQL subscriptions. This pattern is an asynchronous pattern. For a simple pattern that synchronously call Bedrock, explore this [synchronous pattern](../apigw-lambda-bedrock-cdk-python).
40+
To achieve full functionality with this pattern, you must send the response from Lambda function to client using techniques such as websocket or GraphQL subscriptions. This pattern is an asynchronous pattern. For a simple pattern that synchronously call Amazon Bedrock, explore this [synchronous pattern](../apigw-lambda-bedrock-cdk-python).
4141
4242
## Testing
4343
@@ -46,7 +46,7 @@ Upon deployment, you will see the API endpoint URL in the output. It will take t
4646
`https://${API_ID}.execute-api.${REGION_NAME}.amazonaws.com/prod/`
4747
4848
49-
1. [Enable the model](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html#manage-model-access) in Bedrock if you have not done before. The model used by the sample is "anthropic.claude-v2"
49+
1. [Enable the model](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html#manage-model-access) in Amazon Bedrock if you have not done before. The model used by the sample is "anthropic.claude-3-5-sonnet-20240620-v1:0".
5050
5151
2. Post the request to the api
5252
```bash
@@ -63,6 +63,7 @@ curl -X POST https://${API_ID}.execute-api.${REGION_NAME}.amazonaws.com/prod/inv
6363
- [Tutorial: Build an API Gateway REST API with AWS integration](https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html)
6464
- [How do I use API Gateway as a proxy for another AWS service?](https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-proxy-integrate-service/)
6565
- [Amazon Bedrock documentation](https://aws.amazon.com/bedrock)
66+
- [Anthropic Claude Messages API](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html)
6667
- [API Gateway websocket](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html)
6768
- [Publishing message to IoT Topic](https://serverlessland.com/patterns/apigw-lambda-iot-cdk)
6869

apigw-rest-api-sqs-lambda-bedrock-cdk/src/invoke-model/lambda_function.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import boto3
33

4-
modelId = "anthropic.claude-v2"
4+
modelId = "anthropic.claude-3-5-sonnet-20240620-v1:0"
55

66
def lambda_handler(event, context):
77
# TODO implement
@@ -15,10 +15,12 @@ def lambda_handler(event, context):
1515
print(request)
1616
prompt_data = request["prompt"]
1717
body = json.dumps({
18-
'prompt': f'Human:{prompt_data}\n\nAssistant:',
19-
'max_tokens_to_sample': 1028,
18+
'anthropic_version': 'bedrock-2023-05-31',
19+
'messages': [
20+
{'role': 'user', 'content': prompt_data}
21+
],
22+
'max_tokens': 1024,
2023
'temperature': 1,
21-
'top_k': 250,
2224
'top_p': 0.999,
2325
'stop_sequences': ['\n\nHuman:']
2426
})
@@ -30,10 +32,12 @@ def lambda_handler(event, context):
3032
accept= "*/*"
3133

3234
)
33-
body = json.loads(response["body"].read().decode("utf-8"))
35+
response_body = json.loads(response.get('body').read())
36+
print(response_body)
37+
body = response_body.get('content')[0].get('text')
3438

3539
print(body)
3640

37-
#### Publish response to websocket, IoT Core topic
41+
3842

3943

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
boto3==1.28.57
2-
botocore==1.31.57
1+
boto3==1.37.11
2+
botocore==1.37.11

0 commit comments

Comments
 (0)