From 8924bd112de8b51695ba51b9dd3cb388a56af0b6 Mon Sep 17 00:00:00 2001 From: kakakakakku Date: Mon, 24 Nov 2025 16:53:07 +0900 Subject: [PATCH] apigw-rest-stepfunctions-express-sync-bedrock-sam: Update ModelId to Cohere Command R --- .../Readme.md | 3 +-- .../statemachine/stateMachine.asl.json | 26 +++++++++++++------ .../template.yml | 11 +++----- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/apigw-rest-stepfunctions-express-sync-bedrock-sam/Readme.md b/apigw-rest-stepfunctions-express-sync-bedrock-sam/Readme.md index 7bf1f1476..561e11985 100644 --- a/apigw-rest-stepfunctions-express-sync-bedrock-sam/Readme.md +++ b/apigw-rest-stepfunctions-express-sync-bedrock-sam/Readme.md @@ -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"}' - ``` 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. @@ -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 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/apigw-rest-stepfunctions-express-sync-bedrock-sam/statemachine/stateMachine.asl.json b/apigw-rest-stepfunctions-express-sync-bedrock-sam/statemachine/stateMachine.asl.json index 0c7ad7f68..a188324d2 100644 --- a/apigw-rest-stepfunctions-express-sync-bedrock-sam/statemachine/stateMachine.asl.json +++ b/apigw-rest-stepfunctions-express-sync-bedrock-sam/statemachine/stateMachine.asl.json @@ -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", @@ -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" } }, "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" + } + ] }, - "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 diff --git a/apigw-rest-stepfunctions-express-sync-bedrock-sam/template.yml b/apigw-rest-stepfunctions-express-sync-bedrock-sam/template.yml index f37b37af4..793793c75 100644 --- a/apigw-rest-stepfunctions-express-sync-bedrock-sam/template.yml +++ b/apigw-rest-stepfunctions-express-sync-bedrock-sam/template.yml @@ -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 @@ -32,7 +32,7 @@ Resources: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Join [ "/", [ "stepfunctions", StateMachineExpressSync]] - + ########################################################################## # REST API # ########################################################################## @@ -73,7 +73,6 @@ Resources: Action: "states:StartSyncExecution" Resource: !GetAtt StateMachineExpressSync.Arn - StatesMachineExecutionRole: Type: "AWS::IAM::Role" Properties: @@ -103,7 +102,7 @@ Resources: - "logs:PutResourcePolicy" - "logs:DescribeResourcePolicies" - "logs:DescribeLogGroups" - Resource: "*" + Resource: "*" - PolicyName: BedrockAccess PolicyDocument: Version: "2012-10-17" @@ -111,7 +110,7 @@ Resources: - 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 # @@ -120,5 +119,3 @@ Outputs: PromptChainApi: Description: "Sync WF API endpoint" Value: !Sub "https://${RestApiforSyncWF}.execute-api.${AWS::Region}.amazonaws.com/dev" - -