-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new workflow #384
Merged
Merged
Add new workflow #384
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Message categorization with Amazon Bedrock and Amazon SQS | ||
|
||
This workflow demonstrates how you can use Amazon Bedrock to categorize messages and then send them to an Amazon SQS queue for further processing. The AWS Serverless Application Model (SAM) template deploys an AWS Step Functions state machine which defines the workflow. | ||
|
||
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 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 or role that you use must have sufficient permissions to make the necessary AWS service calls and manage AWS resources. | ||
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.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! This example uses Amazon Bedrock foundation model anthropic.claude-3-haiku-20240307-v1:0. To change the model used in the example, you will need to adjust the BedrockAccess policy attached to the Step Functions state machine. Also note that different models take inputs in different formats. To use another model, you will also need to adjust the input parameters sent to the model in the state machine `invokeModel` step. | ||
|
||
## 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/step-functions-workflows-collection | ||
``` | ||
|
||
2. Change directory to the pattern directory: | ||
|
||
``` | ||
cd categorization-state-machine | ||
``` | ||
|
||
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 | ||
- Enter the desired AWS Region | ||
- Allow SAM CLI to create IAM roles with the required permissions. | ||
|
||
Once you have run `sam deploy --guided` once and saved arguments to a configuration file (for exmaple `samconfig.toml`), you can use `sam deploy` in future to use these defaults. | ||
|
||
7. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. | ||
|
||
## How It Works | ||
|
||
In this example, the state machine is invoked with a JSON payload, for example | ||
|
||
``` | ||
{ | ||
"message": "My unicorn is of the wrong colour" | ||
} | ||
``` | ||
|
||
Bedrock will then categorize the message and send it to the correct SQS queue for further processing (additional processing is not a part of this example). | ||
|
||
## Architecture | ||
|
||
 | ||
|
||
## Testing | ||
|
||
Manually invoke the Step Functions state machine either from the console or using the CLI. A successful execution will send the message to correct SQS queue that you can then use to further process the messages in your application. | ||
|
||
With an example message | ||
|
||
``` | ||
{ | ||
"message": "I need to update the billing address for my monthly unicorn rental subscription since I recently moved to a new stable. Could you please change my address from 123 Rainbow Lane to 456 Glitter Grove?" | ||
} | ||
``` | ||
|
||
after the execution you will see the same message in your Billing SQS queue. | ||
|
||
## Cleanup | ||
|
||
1. Delete the stack | ||
|
||
``` | ||
sam delete | ||
``` | ||
|
||
2. Confirm the stack has been deleted | ||
|
||
``` | ||
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'YOUR_STACK_NAME')].StackStatus" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
"title": "Message categorization with Amazon Bedrock", | ||
"description": "Message categorizing with AWS Step Functions, Amazon Bedrock and Amazon Simple Queue Service.", | ||
"language": "", | ||
"simplicity": "1 - Fundamental", | ||
"type": "Express", | ||
"diagram":"/resources/statemachine.png", | ||
"level": "200", | ||
"framework": "SAM", | ||
"services": [ | ||
"Bedrock", | ||
"SQS" | ||
], | ||
"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 Express state machine is designed to categorize messages into queues for further processing based on business needs.", | ||
"It uses the input message from the user to invoke an language model through Amazon Bedrock which will return a category for the message.", | ||
"The output from the model is returned to Step Functions which will further route the message to a correct SQS queue for further processing.", | ||
"If no queue is found, Step Functions will send the message to a default queue.", | ||
"The SQS queue processing is not included as part of this example but showcases the capabilities for message routing based on LLM categorization." | ||
] | ||
}, | ||
"gitHub": { | ||
"template": { | ||
"repoURL": "https://github.com/aws-samples/step-functions-workflows-collection/tree/main/categorization-state-machine/", | ||
"templateDir": "categorization-state-machine", | ||
"templateFile": "template.yml", | ||
"ASL": "statemachine/stateMachine.asl.json" | ||
}, | ||
"payloads": [ | ||
{ | ||
"headline": "", | ||
"payloadURL": "" | ||
} | ||
] | ||
}, | ||
"resources": { | ||
"bullets": [ | ||
{ | ||
"text": "Amazon Bedrock", | ||
"link": "https://aws.amazon.com/bedrock/" | ||
}, | ||
{ | ||
"text": "Amazon Simple Queue Service", | ||
"link": "https://aws.amazon.com/sqs/" | ||
}, | ||
{ | ||
"text": "AWS Step Functions Intrinsic functions", | ||
"link": "https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html" | ||
}, | ||
{ | ||
"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": "Carita Niskanen", | ||
"image": "https://media.licdn.com/dms/image/v2/C4E03AQFT4dda8F1j9g/profile-displayphoto-shrink_400_400/profile-displayphoto-shrink_400_400/0/1516230337269?e=1736985600&v=beta&t=f0NYSSLgAqzh_AuK6a6t-SMGB_JNAQ71ruwYC-0lsHA", | ||
"bio": "Carita is a Solutions Architect at Amazon Web Services (AWS) based in Helsinki, Finland.", | ||
"linkedin": "carita-niskanen-274258", | ||
"twitter": "" | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions
87
categorization-state-machine/statemachine/stateMachine.asl.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{ | ||
"StartAt": "InvokeModel", | ||
"States": { | ||
"InvokeModel": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::bedrock:invokeModel", | ||
"Parameters": { | ||
"ModelId": "anthropic.claude-3-haiku-20240307-v1:0", | ||
"ContentType": "application/json", | ||
"Accept": "application/json", | ||
"Body": { | ||
"anthropic_version": "bedrock-2023-05-31", | ||
"max_tokens": 2000, | ||
"messages": [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "text", | ||
"text.$": "States.JsonToString($)" | ||
}, | ||
{ | ||
"type": "text", | ||
"text": "You are a text categorizing program. Given the list of categories [\"COMPLAINT\",\"BILLING\"], categorize the text into one of these classes (\"Category\"). Give the answer with exactly one class and no additional text. If you cannot categorize the text, please use [\"UNKNOWN\"] category. Do not try to make up a category." | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"ResultSelector": { | ||
"category.$": "$.Body.content[0].text" | ||
}, | ||
"ResultPath": "$.result", | ||
"Next": "Choice" | ||
}, | ||
"Choice": { | ||
"Type": "Choice", | ||
"Choices": [ | ||
{ | ||
"Variable": "$.result.category", | ||
"StringEquals": "COMPLAINT", | ||
"Next": "SQS Complaints" | ||
}, | ||
{ | ||
"Variable": "$.result.category", | ||
"StringEquals": "BILLING", | ||
"Next": "SQS Billing" | ||
} | ||
], | ||
"Default": "SQS Default" | ||
}, | ||
"SQS Complaints": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::sqs:sendMessage", | ||
"Parameters": { | ||
"MessageBody": { | ||
"message.$": "$.message" | ||
}, | ||
"QueueUrl": "${complaintsSubstitution}" | ||
}, | ||
"End": true | ||
}, | ||
"SQS Billing": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::sqs:sendMessage", | ||
"Parameters": { | ||
"MessageBody": { | ||
"message.$": "$.message" | ||
}, | ||
"QueueUrl": "${billingSubstitution}" | ||
}, | ||
"End": true | ||
}, | ||
"SQS Default": { | ||
"Type": "Task", | ||
"Resource": "arn:aws:states:::sqs:sendMessage", | ||
"Parameters": { | ||
"MessageBody": { | ||
"message.$": "$.message" | ||
}, | ||
"QueueUrl": "${defaultQueueSubstitution}" | ||
}, | ||
"End": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: Message categorization with AWS Step Functions, Amazon Bedrock and Amazon SQS. | ||
|
||
Resources: | ||
StateMachine: | ||
Type: AWS::Serverless::StateMachine | ||
Properties: | ||
Logging: | ||
Level: ALL | ||
IncludeExecutionData: true | ||
Destinations: | ||
- CloudWatchLogsLogGroup: | ||
LogGroupArn: !GetAtt StateMachineLogGroup.Arn | ||
Role: !GetAtt StatesMachineExecutionRole.Arn | ||
Type: EXPRESS | ||
DefinitionSubstitutions: | ||
complaintsSubstitution: !Ref ComplaintsQueue | ||
billingSubstitution: !Ref BillingQueue | ||
defaultQueueSubstitution: !Ref DefaultQueue | ||
DefinitionUri: statemachine/stateMachine.asl.json | ||
|
||
StatesMachineExecutionRole: | ||
Type: AWS::IAM::Role | ||
Properties: | ||
AssumeRolePolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Principal: | ||
Service: | ||
- !Sub states.${AWS::Region}.amazonaws.com | ||
Action: sts:AssumeRole | ||
Path: / | ||
Policies: | ||
- PolicyName: SQS | ||
PolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Action: | ||
- sqs:SendMessage | ||
- sqs:ReceiveMessage | ||
- sqs:DeleteMessage | ||
- sqs:GetQueueAttributes | ||
Resource: | ||
- !GetAtt DefaultQueue.Arn | ||
- !GetAtt ComplaintsQueue.Arn | ||
- !GetAtt BillingQueue.Arn | ||
- PolicyName: CWLogs | ||
PolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Action: | ||
- logs:CreateLogDelivery | ||
- logs:CreateLogStream | ||
- logs:GetLogDelivery | ||
- logs:UpdateLogDelivery | ||
- logs:DeleteLogDelivery | ||
- logs:ListLogDeliveries | ||
- logs:PutLogEvents | ||
- logs:PutResourcePolicy | ||
- logs:DescribeResourcePolicies | ||
- logs:DescribeLogGroups | ||
Resource: '*' | ||
- PolicyName: BedrockAccess | ||
PolicyDocument: | ||
Version: '2012-10-17' | ||
Statement: | ||
- Effect: Allow | ||
Action: | ||
- bedrock:InvokeModel | ||
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/anthropic.claude-3-haiku-20240307-v1:0 | ||
|
||
StateMachineLogGroup: | ||
Type: AWS::Logs::LogGroup | ||
Properties: | ||
LogGroupName: !Sub /aws/vendedlogs/states/${AWS::StackName}-StateMachine-Logs | ||
|
||
DefaultQueue: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
MessageRetentionPeriod: 43200 | ||
RedrivePolicy: | ||
deadLetterTargetArn: !GetAtt DefaultDLQ.Arn | ||
maxReceiveCount: 5 | ||
|
||
DefaultDLQ: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
MessageRetentionPeriod: 43200 | ||
|
||
ComplaintsQueue: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
RedrivePolicy: | ||
deadLetterTargetArn: !GetAtt ComplaintsDLQ.Arn | ||
maxReceiveCount: 5 | ||
MessageRetentionPeriod: 43200 | ||
|
||
ComplaintsDLQ: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
MessageRetentionPeriod: 43200 | ||
|
||
BillingQueue: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
RedrivePolicy: | ||
deadLetterTargetArn: !GetAtt BillingDLQ.Arn | ||
maxReceiveCount: 5 | ||
MessageRetentionPeriod: 43200 | ||
|
||
BillingDLQ: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
MessageRetentionPeriod: 43200 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add an example and the expected outcome