Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions apigw-rest-stepfunctions-express-sync-bedrock-sam/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ The stack will output the **api endpoint**. You can use *Postman* or *curl* to s

```
curl -H "Content-type: application/json" -X POST -d '{"prompt_one": "Write a 500 word blog post on The Beatles"}' <Your API endpoint>

```
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.

Expand All @@ -95,4 +94,4 @@ After runnning the above command, API Gateway will invoke the State machine and
----
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
SPDX-License-Identifier: MIT-0
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"Type": "Task",
"Resource": "arn:aws:states:::bedrock:invokeModel",
"Parameters": {
"ModelId": "cohere.command-text-v14",
"ModelId": "cohere.command-r-v1:0",
"Body": {
"prompt.$": "$.prompt_one",
"message.$": "$.prompt_one",
"max_tokens": 2000
},
"ContentType": "application/json",
Expand All @@ -17,31 +17,41 @@
"Next": "Add first result to conversation history",
"ResultPath": "$.result_one",
"ResultSelector": {
"result_one.$": "$.Body.generations[0].text"
"result_one.$": "$.Body.text"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
},
"Add first result to conversation history": {
"Type": "Pass",
"Next": "Invoke model with second prompt",
"Parameters": {
"convo_one.$": "States.Format('{}',$.result_one.result_one)"
"chat_history": [
{
"role": "USER",
"message.$": "$.prompt_one"
},
{
"role": "CHATBOT",
"message.$": "$.result_one.result_one"
}
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I used chat_history format instead of ChatML format like Human: and Assistant:.

},
"ResultPath": "$.convo_one"
"ResultPath": "$.chat_history"
},
"Invoke model with second prompt": {
"Type": "Task",
"Resource": "arn:aws:states:::bedrock:invokeModel",
"Parameters": {
"ModelId": "cohere.command-text-v14",
"ModelId": "cohere.command-r-v1:0",
"Body": {
"prompt.$": "States.Format('{}\n{}','Human: Now write a short story based on the following. Assistant:', $.convo_one.convo_one)",
"message": "Now write a short story based on the following.",
"chat_history.$": "$.chat_history.chat_history",
"max_tokens": 1000
},
"ContentType": "application/json",
"Accept": "*/*"
},
"ResultSelector": {
"result_two.$": "$.Body.generations[0].text"
"result_two.$": "$.Body.text"
},
"ResultPath": "$.result_two",
"End": true
Expand Down
11 changes: 4 additions & 7 deletions apigw-rest-stepfunctions-express-sync-bedrock-sam/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Resources:
#Name: APIGWStepFunctionExpressSync
DefinitionUri: statemachine/stateMachine.asl.json
DefinitionSubstitutions:
ModelId: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/cohere.command-text-v14
ModelId: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/cohere.command-r-v1:0
Role:
Fn::GetAtt: [ StatesMachineExecutionRole, Arn ]
Type: EXPRESS
Expand All @@ -32,7 +32,7 @@ Resources:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Join [ "/", [ "stepfunctions", StateMachineExpressSync]]

##########################################################################
# REST API #
##########################################################################
Expand Down Expand Up @@ -73,7 +73,6 @@ Resources:
Action: "states:StartSyncExecution"
Resource: !GetAtt StateMachineExpressSync.Arn


StatesMachineExecutionRole:
Type: "AWS::IAM::Role"
Properties:
Expand Down Expand Up @@ -103,15 +102,15 @@ Resources:
- "logs:PutResourcePolicy"
- "logs:DescribeResourcePolicies"
- "logs:DescribeLogGroups"
Resource: "*"
Resource: "*"
- PolicyName: BedrockAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "bedrock:InvokeModel"
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/cohere.command-text-v14
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/cohere.command-r-v1:0

##########################################################################
# Outputs #
Expand All @@ -120,5 +119,3 @@ Outputs:
PromptChainApi:
Description: "Sync WF API endpoint"
Value: !Sub "https://${RestApiforSyncWF}.execute-api.${AWS::Region}.amazonaws.com/dev"