Skip to content

Commit a4314e6

Browse files
authored
Merge pull request #2864 from kakakakakku/apigw-rest-stepfunctions-express-sync-bedrock-sam
apigw-rest-stepfunctions-express-sync-bedrock-sam: Migrate from Cohere Command R to Amazon Nova 2 Lite
2 parents 7dba92a + 12665b9 commit a4314e6

5 files changed

Lines changed: 80 additions & 54 deletions

File tree

apigw-rest-stepfunctions-express-sync-bedrock-sam/Readme.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Prompt Chaining with Amazon API Gateway, AWS Step Functions and Amazon Bedrock.
22

3-
The AWS Serverless Application Model (SAM) template deploys an Amazon API Gateway HTTP API endpoint connected to an AWS Step Functions state machine. This example demonstrates how to invoke an Express state machine synchronously and utilize AWS Step Functions intrinsic functions to chain two prompts, which are then used to invoke the Amazon Bedrock language model. The output from the state machine execution is returned to the client within 29 seconds, using the HTTP API. This no-code example showcases how the results from the first prompt can be used to provide context for the second prompt, allowing the language model to deliver a highly-curated response. By chaining these prompts, the system can leverage the capabilities of the language model to generate more meaningful and contextual outputs.
3+
The AWS Serverless Application Model (SAM) template deploys an Amazon API Gateway REST API endpoint connected to an AWS Step Functions state machine. This example demonstrates how to invoke an Express state machine synchronously and utilize AWS Step Functions intrinsic functions to chain two prompts, which are then used to invoke the Amazon Bedrock language model. The output from the state machine execution is returned to the client within 29 seconds, using the REST API. This no-code example showcases how the results from the first prompt can be used to provide context for the second prompt, allowing the language model to deliver a highly-curated response. By chaining these prompts, the system can leverage the capabilities of the language model to generate more meaningful and contextual outputs.
44

55
Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-rest-stepfunctions-express-sync-bedrock-sam)
66

@@ -12,7 +12,7 @@ Important: this application uses various AWS services and there are costs associ
1212
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
1313
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
1414
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15-
* [NOTE! Manage Access to Amazon Bedrock Foundation Models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) at the time of writing, this example uses Amazon Bedrock foundation model cohere.command-text-v14
15+
* This example uses Amazon Bedrock foundation model amazon.nova-2-lite-v1:0
1616

1717

1818
## Deployment Instructions
@@ -45,40 +45,33 @@ Important: this application uses various AWS services and there are costs associ
4545
4646
## How it Works
4747
In this example, the state machine is invoked with a JSON payload
48+
4849
```asl
4950
{
5051
"prompt_one": "Write a 500 word blog post on The Beatles"
5152
}
5253
```
54+
5355
During execution, the Task state calls the Bedrock API and the response is passed to the task 'result_one'.
56+
5457
```asl
5558
{
56-
"result_one.$": "$.Body.generations[0].text"
57-
}
58-
```
59-
A Pass state is then used to format the data using an Intrinsic Function (States.) which is passed to the next state.
60-
```asl
61-
{
62-
"convo_one.$": "States.Format('{}',$.result_one.result_one)"
63-
}
64-
```
65-
The second prompt is then executed with new instructions and the results from the first execution. This provides the prompt with more context
66-
```asl
67-
{
68-
"prompt.$": "States.Format('{}\n{}','Human: Now write a short story based on the following. Assistant:', $.convo_one.convo_one)",
69-
"max_tokens": 1000
59+
"result_one.$": "$.Body.output.message.content[0].text"
7060
}
7161
```
72-
By default, the state then sends the task result as output.
7362

63+
A Pass state is then used to build the conversation history with the first prompt, the model's response, and the second prompt, which is passed to the next state.
64+
65+
The second prompt is then executed with the conversation history that includes the results from the first execution. This provides the model with more context.
66+
67+
By default, the state then sends the task result as output.
7468

7569
## Testing
7670

7771
The stack will output the **api endpoint**. You can use *Postman* or *curl* to send a POST request to the API Gateway endpoint.
7872

7973
```
8074
curl -H "Content-type: application/json" -X POST -d '{"prompt_one": "Write a 500 word blog post on The Beatles"}' <Your API endpoint>
81-
8275
```
8376
After runnning the above command, API Gateway will invoke the State machine and return the results back to the client instead of just the State machine's execution Id.
8477

@@ -93,6 +86,6 @@ After runnning the above command, API Gateway will invoke the State machine and
9386
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
9487
```
9588
----
96-
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
89+
Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
9790

98-
SPDX-License-Identifier: MIT-0
91+
SPDX-License-Identifier: MIT-0

apigw-rest-stepfunctions-express-sync-bedrock-sam/apigw-rest-stepfunctions-express-sync-bedrock-sam.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"title": "Prompt Chaining with Amazon API Gateway and AWS Step Functions",
3-
"description": "Prompt Chaining no-code example with Amazon API Gateway HTTP API, AWS Step Functions and Amazon Bedrock.",
2+
"title": "Prompt Chaining with Amazon API Gateway and AWS Step Functions.",
3+
"description": "Prompt Chaining no-code example with Amazon API Gateway REST API, AWS Step Functions and Amazon Bedrock.",
44
"language": "",
55
"level": "200",
66
"framework": "AWS SAM",
77
"introBox": {
88
"headline": "How it works",
99
"text": [
1010
"The provided example demonstrates the deployment of a serverless application using the AWS Serverless Application Model (SAM) template.",
11-
"This serverless application includes an Amazon API Gateway HTTP API endpoint that is connected to an AWS Step Functions state machine.",
11+
"This serverless application includes an Amazon API Gateway REST API endpoint that is connected to an AWS Step Functions state machine.",
1212
"The state machine is designed to invoke an Express workflow synchronously.",
1313
"It utilizes AWS Step Functions' intrinsic functions to chain two prompts, which are then used to invoke the Amazon Bedrock language model.",
14-
"The output from the state machine execution is returned to the client within 29 seconds through the HTTP API.",
14+
"The output from the state machine execution is returned to the client within 29 seconds through the REST API.",
1515
"This no-code example showcases how the results from the first prompt can be used to provide context for the second prompt, allowing the language model to deliver a highly-curated response.",
1616
"By chaining these prompts, the system can leverage the capabilities of the language model to generate more meaningful and contextual outputs."
1717
]
@@ -73,7 +73,7 @@
7373
"x": 20,
7474
"y": 50,
7575
"service": "apigw",
76-
"label": "API Gateway HTTP API"
76+
"label": "API Gateway REST API"
7777
},
7878
"icon2": {
7979
"x": 50,
@@ -90,12 +90,12 @@
9090
"line1": {
9191
"from": "icon1",
9292
"to": "icon2",
93-
"label": ""
93+
"label": "Request ANY {proxy+}"
9494
},
9595
"line2": {
9696
"from": "icon2",
9797
"to": "icon3",
98-
"label": ""
98+
"label": "Invoke"
9999
}
100100
}
101101
}

apigw-rest-stepfunctions-express-sync-bedrock-sam/example-pattern.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"title": "Prompt Chaining with Amazon API Gateway and AWS Step Functions.",
3-
"description": "Prompt Chaining no-code example with Amazon API Gateway HTTP API, AWS Step Functions and Amazon Bedrock.",
3+
"description": "Prompt Chaining no-code example with Amazon API Gateway REST API, AWS Step Functions and Amazon Bedrock.",
44
"language": "",
55
"level": "200",
66
"framework": "AWS SAM",
77
"introBox": {
88
"headline": "How it works",
99
"text": [
1010
"The provided example demonstrates the deployment of a serverless application using the AWS Serverless Application Model (SAM) template.",
11-
"This serverless application includes an Amazon API Gateway HTTP API endpoint that is connected to an AWS Step Functions state machine.",
11+
"This serverless application includes an Amazon API Gateway REST API endpoint that is connected to an AWS Step Functions state machine.",
1212
"The state machine is designed to invoke an Express workflow synchronously.",
1313
"It utilizes AWS Step Functions' intrinsic functions to chain two prompts, which are then used to invoke the Amazon Bedrock language model.",
14-
"The output from the state machine execution is returned to the client within 29 seconds through the HTTP API.",
14+
"The output from the state machine execution is returned to the client within 29 seconds through the REST API.",
1515
"This no-code example showcases how the results from the first prompt can be used to provide context for the second prompt, allowing the language model to deliver a highly-curated response.",
1616
"By chaining these prompts, the system can leverage the capabilities of the language model to generate more meaningful and contextual outputs."
1717
]

apigw-rest-stepfunctions-express-sync-bedrock-sam/statemachine/stateMachine.asl.json

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,76 @@
66
"Type": "Task",
77
"Resource": "arn:aws:states:::bedrock:invokeModel",
88
"Parameters": {
9-
"ModelId": "cohere.command-text-v14",
9+
"ModelId": "global.amazon.nova-2-lite-v1:0",
1010
"Body": {
11-
"prompt.$": "$.prompt_one",
12-
"max_tokens": 2000
13-
},
14-
"ContentType": "application/json",
15-
"Accept": "*/*"
11+
"messages": [
12+
{
13+
"role": "user",
14+
"content": [
15+
{
16+
"text.$": "$.prompt_one"
17+
}
18+
]
19+
}
20+
],
21+
"inferenceConfig": {
22+
"maxTokens": 2000
23+
}
24+
}
1625
},
1726
"Next": "Add first result to conversation history",
1827
"ResultPath": "$.result_one",
1928
"ResultSelector": {
20-
"result_one.$": "$.Body.generations[0].text"
29+
"result_one.$": "$.Body.output.message.content[0].text"
2130
}
2231
},
2332
"Add first result to conversation history": {
2433
"Type": "Pass",
2534
"Next": "Invoke model with second prompt",
2635
"Parameters": {
27-
"convo_one.$": "States.Format('{}',$.result_one.result_one)"
36+
"messages": [
37+
{
38+
"role": "user",
39+
"content": [
40+
{
41+
"text.$": "$.prompt_one"
42+
}
43+
]
44+
},
45+
{
46+
"role": "assistant",
47+
"content": [
48+
{
49+
"text.$": "$.result_one.result_one"
50+
}
51+
]
52+
},
53+
{
54+
"role": "user",
55+
"content": [
56+
{
57+
"text": "Now write a short story based on the above."
58+
}
59+
]
60+
}
61+
]
2862
},
29-
"ResultPath": "$.convo_one"
63+
"ResultPath": "$.conversation"
3064
},
3165
"Invoke model with second prompt": {
3266
"Type": "Task",
3367
"Resource": "arn:aws:states:::bedrock:invokeModel",
3468
"Parameters": {
35-
"ModelId": "cohere.command-text-v14",
69+
"ModelId": "global.amazon.nova-2-lite-v1:0",
3670
"Body": {
37-
"prompt.$": "States.Format('{}\n{}','Human: Now write a short story based on the following. Assistant:', $.convo_one.convo_one)",
38-
"max_tokens": 1000
39-
},
40-
"ContentType": "application/json",
41-
"Accept": "*/*"
71+
"messages.$": "$.conversation.messages",
72+
"inferenceConfig": {
73+
"maxTokens": 2000
74+
}
75+
}
4276
},
4377
"ResultSelector": {
44-
"result_two.$": "$.Body.generations[0].text"
78+
"result_two.$": "$.Body.output.message.content[0].text"
4579
},
4680
"ResultPath": "$.result_two",
4781
"End": true

apigw-rest-stepfunctions-express-sync-bedrock-sam/template.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
AWSTemplateFormatVersion: '2010-09-09'
22
Transform: AWS::Serverless-2016-10-31
3-
Description: Prompt Chaining with Amazon API Gateway HTTP API, AWS StepFunctions and Amazon Bedrock.
3+
Description: Prompt Chaining with Amazon API Gateway REST API, AWS StepFunctions and Amazon Bedrock.
44

55
Resources:
66

@@ -14,7 +14,7 @@ Resources:
1414
#Name: APIGWStepFunctionExpressSync
1515
DefinitionUri: statemachine/stateMachine.asl.json
1616
DefinitionSubstitutions:
17-
ModelId: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/cohere.command-text-v14
17+
ModelId: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:inference-profile/global.amazon.nova-2-lite-v1:0
1818
Role:
1919
Fn::GetAtt: [ StatesMachineExecutionRole, Arn ]
2020
Type: EXPRESS
@@ -32,7 +32,7 @@ Resources:
3232
Type: AWS::Logs::LogGroup
3333
Properties:
3434
LogGroupName: !Join [ "/", [ "stepfunctions", StateMachineExpressSync]]
35-
35+
3636
##########################################################################
3737
# REST API #
3838
##########################################################################
@@ -73,7 +73,6 @@ Resources:
7373
Action: "states:StartSyncExecution"
7474
Resource: !GetAtt StateMachineExpressSync.Arn
7575

76-
7776
StatesMachineExecutionRole:
7877
Type: "AWS::IAM::Role"
7978
Properties:
@@ -103,15 +102,17 @@ Resources:
103102
- "logs:PutResourcePolicy"
104103
- "logs:DescribeResourcePolicies"
105104
- "logs:DescribeLogGroups"
106-
Resource: "*"
105+
Resource: "*"
107106
- PolicyName: BedrockAccess
108107
PolicyDocument:
109108
Version: "2012-10-17"
110109
Statement:
111110
- Effect: Allow
112111
Action:
113112
- "bedrock:InvokeModel"
114-
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/cohere.command-text-v14
113+
Resource:
114+
- !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:inference-profile/global.amazon.nova-2-lite-v1:0
115+
- arn:aws:bedrock:*::foundation-model/amazon.nova-2-lite-v1:0
115116

116117
##########################################################################
117118
# Outputs #
@@ -120,5 +121,3 @@ Outputs:
120121
PromptChainApi:
121122
Description: "Sync WF API endpoint"
122123
Value: !Sub "https://${RestApiforSyncWF}.execute-api.${AWS::Region}.amazonaws.com/dev"
123-
124-

0 commit comments

Comments
 (0)