Saturday, July 6, 2024
HomeArtificial Intelligence (AI) NewsBuilding a multilingual personal calendar assistant with Amazon Bedrock and AWS Step...

Building a multilingual personal calendar assistant with Amazon Bedrock and AWS Step Functions

Expatriates and people living outside their home countries deal with a lot of emails written in different languages ​​every day. They often struggle with the language barrier when it comes to setting reminders for events such as business meetings or client conferences. To solve this problem, this post shows how to apply AWS services such as Amazon Bedrock, AWS Step Functions, and Amazon Simple Email Service (Amazon SES) to build a fully automated multilingual calendar artificial intelligence (AI) assistant that can understand incoming messages, translate them into your preferred language, and automatically set calendar reminders.

Amazon Bedrock is a fully managed service that makes foundational models (FMs) from leading AI startups and Amazon available via APIs. You can choose from a wide range of FMs to find the model that best suits your use case. With Amazon Bedrock, you can get started quickly, privately customize your FMs with your own data, and easily integrate and deploy them into your applications using AWS tools without having to manage any infrastructure.

AWS Step Functions is a visual workflow service that helps developers build distributed applications, automate processes, orchestrate microservices, and create data and machine learning (ML) pipelines. It can coordinate multiple steps in a pipeline. Steps are AWS Lambda functions that generate prompts, parse the output of underlying models, or send email reminders using Amazon SES. Step Functions can interact with over 220 AWS services, including optimized integration with Amazon Bedrock. Step Functions pipelines can contain loops, map jobs, parallel jobs, conditions, and human interactions, making them useful for AI-human interaction scenarios.

In this post, we show you how to quickly combine the flexibility and power of both Amazon Bedrock FM and Step Functions to build a generative AI application in just a few steps. You can reuse the same design patterns to implement more generative AI applications with less effort. Both Amazon Bedrock and Step Functions are serverless, so you don’t have to think about managing and scaling infrastructure.

The source code and deployment instructions are available in our Github repository.

Solution overview

Figure 1: Solution architecture

Figure 1: Solution architecture

As shown in Figure 1, the workflow starts with Amazon API Gateway and then passes through various steps in a Step Functions state machine. Notice how the original message flows through the pipeline and is changed. First, the message is appended to a prompt. Then, it is transformed into structured JSON by the underlying model. Finally, an action is taken using this structured JSON.

  1. The original message (the Norwegian example) is sent to a Step Functions state machine using API Gateway.
  2. The Lambda function generates a prompt that contains the system instructions, the original message, and any other required information such as the current date and time. (Below is a prompt generated from a sample message.)
    • Sometimes the original message doesn’t specify an exact date, but implies a date based on the current context, such as “Please reply by this Friday,” so the function inserts the current date into the prompt to help the model interpret the correct date. this Friday.
  3. It invokes Bedrock FM to perform the next task as indicated in the prompt, and passes the output to the parser in the next step.
    • Translate and summarize the original message into English.
    • Extract event information such as subject, location, and time from the original message.
    • Generate an action plan list for the event: Currently, the instructions just ask FM to generate an action plan for sending a calendar reminder email for attending the event.
  4. Parse the FM output to ensure it has a valid schema. (The following is a sample message parsed:)
    • Although Amazon Bedrock’s Anthropic Claude can control the output format to generate JSON, it may still produce results like “this is the json {…}”. To make it more robust, we will augment this pipeline by implementing an output parser that ensures schema compliance.
  5. Iterate through the action plans list and perform step 6 for each item. All action items follow the same schema.
    {
              "tool_name": "create-calendar-reminder",
              "parameters": {
                "body": "Jeff, the CEO, invites employees to ...",
                "raw_body": "Kjære ansatte,\n\nVi ..",
                "subject": "Winter fun and team building event",
                "start_datetime": "2024-05-30T10:00:00Z",
                "end_datetime": "2024-05-30T23:00:00Z",
                "location": "Holmenkollbakken, Oslo"
              }
    }

  6. Choose the right tool for the job:
    • if, tool_name equal create-calendar-reminderIt then runs subflow A, which uses a Lambda function to send a calendar reminder email.
    • To support other jobs in the future, you can extend the prompt to create different action plans (assign different values) tool_name), perform the appropriate actions outlined in subflow B.
  7. end.

Prerequisites

To run this solution, you need the following prerequisites:

Deployment and Testing

Using the AWS Cloud Development Kit (AWS CDK), you can deploy the full stack in a single command line by following the deployment instructions in the Github repository. The deployment outputs the API Gateway endpoint URL and API key.

Use a tool such as curl to send messages to API Gateway in different languages ​​for testing.

apigw=(THE_VALUE_OF_GenaiCalendarAgentStack.APIUrl)
apikey=(THE_VALUE_OF_GenaiCalendarAgentStack.GeneratedAPIKeyValue)
curl -v $apigw --header "Content-Type: application/json" --header "x-api-key:$apikey" -d @./doc/sample-inputs/norsk1.json 

Within a minute or two, an email invitation will be sent from the sender’s email address to the recipient, as shown in Figure 2.

Figure 2: Email generated by the solution

Figure 2: Email generated by the solution

cleaning

To avoid incurring future charges, delete the resources by running the following command at the root path of your source code:

$ cdk destroy

Future expansion of the solution

In its current implementation, the solution only sends calendar reminder emails and the prompts simply tell the underlying model to generate action items. tool_name equal create-calendar-reminderThe solution can be extended to support even more actions, for example, if the event takes place in July (summer vacation for many people), automatically send an email to the event originator to politely decline.

  1. Change the prompt instructions: If the event date is in July, create an action item, tool_name To send-decline-mail.
  2. Create a new subflow C, similar to subflow A. tool_name Match send-decline-mail:
    1. It calls Amazon Bedrock FM to generate email content explaining that it is July (summer vacation) and therefore you cannot attend the event.
    2. Invoke a Lambda function to send a rejection email with the generated content.

Additionally, you can try different underlying models such as Meta Llma 3 and Mistral AI on Amazon Bedrock to improve performance and reduce costs, and you can also explore agents on Amazon Bedrock that can orchestrate and execute multi-step tasks.

Conclusion

In this post, we described a solution pattern for using generative AI within your workflows. With the flexibility and capabilities offered by both Amazon Bedrock FM and AWS Step Functions, you can build a powerful generative AI assistant in just a few steps. This assistant can streamline processes, increase productivity, and handle a wide variety of tasks efficiently. You can easily change or upgrade capacity without suffering from the operational overhead of a managed service.

You can find the source code for the solution in our Github repository and follow the deployment instructions to deploy your own multilingual Calendar Assistant.

For more information, see the following resources:


About the Author

Feng Lu is a Senior Solutions Architect at AWS with 20 years of professional experience. He is passionate about helping organizations build scalable, flexible, and resilient architectures to address their business challenges. His current focus is on leveraging Artificial Intelligence (AI) and Internet of Things (IoT) technologies to bring greater intelligence and efficiency to physical environments.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments

error: Content is protected !!