Skip to content

Commit

Permalink
Manual add pattern appsync-stepfunctions-express-sync-bedrock-sam
Browse files Browse the repository at this point in the history
  • Loading branch information
julianwood committed Sep 4, 2024
1 parent 7d4a4d0 commit 6670d44
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 0 deletions.
109 changes: 109 additions & 0 deletions appsync-stepfunctions-express-sync-bedrock-sam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Prompt Chaining with AWS AppSync, AWS Step Functions and Amazon Bedrock.

The AWS Serverless Application Model (SAM) template deploys an AWS AppSync serverless GraphQL 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 GraphQL 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.

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

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
* [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


## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```
cd appsync-stepfunctions-express-sync-bedrock-sam
```
3. From the command line, use AWS SAM build to prepare an application for subsequent steps in the developer workflow, such as local testing or deploying to the AWS Cloud:
```
sam build
```
4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```
sam deploy --guided
```
5. During the prompts:
* Enter a stack name
* Select the desired AWS Region
* Allow SAM to create roles with the required permissions if needed.
6. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
## Architecture
![appsync-1](images/appsync.png)
## How it Works
In this example, the state machine is invoked with a JSON payload
```asl
{
"prompt_one": "Write a 500 word blog post on The Beatles"
}
```
During execution, the Task state calls the Bedrock API and the response is passed to the task 'result_one'.
```asl
{
"result_one.$": "$.Body.generations[0].text"
}
```
A Pass state is then used to format the data using an Intrinsic Function (States.) which is passed to the next state.
```asl
{
"convo_one.$": "States.Format('{}',$.result_one.result_one)"
}
```
The second prompt is then executed with new instructions and the results from the first execution. This provides the prompt with more context
```asl
{
"prompt.$": "States.Format('{}\n{}','Human: Now write a short story based on the following. Assistant:', $.convo_one.convo_one)",
"max_tokens": 1000
}
```
By default, the state then sends the task result as output.


## Testing
1. Within your AWS Console navigate to AppSync in the region you deployed this sample in.

2. Select the AppSyncApi

3. On the left side select Queries

4. Below the Run dropdown paste the following
```graphql
mutation MyMutation {
startWorkflow(prompt_one: "Write a 500 word blog post on The Beatles") {
input
output
}
}
```
5.Then Select Run and MyMutation
After runnning the above command, AppSync will invoke the State machine and return the complete results as shown below

![run-1](images/image-1.png)

## Cleanup

1. Delete the stack
```bash
sam delete
```
2. Confirm the stack has been deleted
```bash
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"
```
----
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"title": "Prompt Chaining with AWS AppSync, AWS Step Functions and Amazon Bedrock.",
"description": "Prompt Chaining no-code example with AWS AppSync, AWS Step Functions and Amazon Bedrock.",
"language": "",
"level": "200",
"framework": "SAM",
"introBox": {
"headline": "How it works",
"text": [
"The provided example demonstrates the deployment of a serverless application using the AWS Serverless Application Model (SAM) template.",
"This serverless application includes an AWS AppSync serverless GraphQL endpoint that is connected to an AWS Step Functions state machine.",
"The state machine is designed to invoke an Express workflow synchronously.",
"It utilizes 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 through the GraphQL 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."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/appsync-stepfunctions-express-sync-bedrock-sam",
"templateURL": "serverless-patterns/appsync-stepfunctions-express-sync-bedrock-sam",
"projectFolder": "appsync-stepfunctions-express-sync-bedrock-sam",
"templateFile": "template.yml"
}
},
"resources": {
"bullets": [
{
"text": "AWS Step Functions Intrinsic functions",
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html"
},
{
"text": "AWS Appsync",
"link": "https://aws.amazon.com/pm/appsync/"
},
{
"text": "Amazon Bedrock",
"link": "https://aws.amazon.com/bedrock/"
},
{
"text": "Synchronous and Asynchronous Express Workflows",
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/concepts-express-synchronous.html"
}
]
},
"deploy": {
"text": [
"sam deploy"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>sam delete</code>"
]
},
"authors": [
{
"name": "Mike Hume",
"image": "https://media.licdn.com/dms/image/D4E03AQEiUfmBiUOw_A/profile-displayphoto-shrink_200_200/0/1718324029612?e=1727308800&v=beta&t=ybhm76l-CP5xcUsHbdq2IaJOlfyycvQ6gNwuCSd3Z0w",
"bio": "AWS Senior Solutions Architect & UKPS Serverless Lead.",
"linkedin": "michael-hume-4663bb64",
"twitter": ""
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions appsync-stepfunctions-express-sync-bedrock-sam/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Execution {
prompt_one: String
status: String
input: String
executionArn: String
startDate: String
stopDate: String
output: String
}

type Mutation {
startWorkflow (
prompt_one: String!
): Execution
}

# Any Query you need for your application
type QueryOutput { output: String! }
type Query { myQuery(input: String!): QueryOutput }
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"Comment": "An example of using Bedrock to chain prompts and their responses together.",
"StartAt": "Invoke model with first prompt",
"States": {
"Invoke model with first prompt": {
"Type": "Task",
"Resource": "arn:aws:states:::bedrock:invokeModel",
"Parameters": {
"ModelId": "cohere.command-text-v14",
"Body": {
"prompt.$": "$.prompt_one",
"max_tokens": 2000
},
"ContentType": "application/json",
"Accept": "*/*"
},
"Next": "Add first result to conversation history",
"ResultPath": "$.result_one",
"ResultSelector": {
"result_one.$": "$.Body.generations[0].text"
}
},
"Add first result to conversation history": {
"Type": "Pass",
"Next": "Invoke model with second prompt",
"Parameters": {
"convo_one.$": "States.Format('{}',$.result_one.result_one)"
},
"ResultPath": "$.convo_one"
},
"Invoke model with second prompt": {
"Type": "Task",
"Resource": "arn:aws:states:::bedrock:invokeModel",
"Parameters": {
"ModelId": "cohere.command-text-v14",
"Body": {
"prompt.$": "States.Format('{}\n{}','Human: Now write a short story based on the following. Assistant:', $.convo_one.convo_one)",
"max_tokens": 1000
},
"ContentType": "application/json",
"Accept": "*/*"
},
"ResultSelector": {
"result_two.$": "$.Body.generations[0].text"
},
"ResultPath": "$.result_two",
"End": true
}
}
}
Loading

0 comments on commit 6670d44

Please sign in to comment.