With this WhatsApp app, you can chat in any language with a Large language models (LLM) on Amazon Bedrock. Send voice notes and receive transcriptions. By making a minor change in the code, you can also send the transcription to the model.
Your data will be securely stored in your AWS account and will not be shared or used for model training. It is not recommended to share private information because the security of data with WhatsApp is not guaranteed.
** UPDATE: Power with Anthropic's Claude 3.5
✅ AWS Level: Advanced - 300
Prerequisites:
💰 Cost to complete:
- Amazon Bedrock Pricing
- Amazon Lambda Pricing
- Amazon Transcribe Pricing
- Amazon DynamoDB Pricing
- Amazon APIGateway Pricing
- Whatsapp pricing
- WhatsApp receives the message: voice/text/image.
- Amazon API Gateway receives the message from the WhatsApp webhook (previously authenticated).
- Then, an AWS Lambda Functions named whatsapp_in processes the message and sends it to an Amazon DynamoDB table named whatsapp-metadata to store it.
- The DynamoDB table whtsapp-metadata has a DynamoDB streaming configured, which triggers the process_stream Lambda Function.
process_stream Lambda Function sends the text of the message to a Lambda Function .
In this application are 2 Lambda Functions that can fulfill this function, one that uses LangChain to handle the conversations and Amazon Bedrock to invoke the LLM, named langchain_agent_text, another that uses the Amazon Bedrock API call directly named agent_text_v3 with Claude 3 Sonnet , which one to use is up to you.
- The audio_job_transcriptor Lambda Function is triggered. This Lambda Function downloads the WhatsApp audio from the link in the message in an Amazon S3 bucket, using Whatsapp Token authentication, then converts the audio to text using the Amazon Transcribe start_transcription_job API, which leaves the transcript file in an Output Amazon S3 bucket.
Function that invokes audio_job_transcriptor looks like this:
def start_job_transciptor (jobName,s3Path_in,OutputKey,codec):
response = transcribe_client.start_transcription_job(
TranscriptionJobName=jobName,
IdentifyLanguage=True,
MediaFormat=codec,
Media={
'MediaFileUri': s3Path_in
},
OutputBucketName = BucketName,
OutputKey=OutputKey
)
💡 Notice that the IdentifyLanguage parameter is configured to True. Amazon Transcribe can determine the primary language in the audio.
- The transcriber_done Lambda Function is triggered with an Amazon S3 Event Notification put item once the Transcribe Job is complete. It extracts the transcript from the Output S3 bucket and sends it to whatsapp_out Lambda Function to respond to WhatsApp.
✅ You have the option to uncomment the code in the transcriber_done Lambda Function and send the voice note transcription to langchain_agent_text Lambda Function.
try:
response_3 = lambda_client.invoke(
FunctionName = LAMBDA_AGENT_TEXT,
InvocationType = 'Event' ,#'RequestResponse',
Payload = json.dumps({
'whats_message': text,
'whats_token': whats_token,
'phone': phone,
'phone_id': phone_id,
'messages_id': messages_id
})
)
print(f'\nRespuesta:{response_3}')
return response_3
except ClientError as e:
err = e.response
error = err
print(err.get("Error", {}).get("Code"))
return f"Un error invocando {LAMBDA_AGENT_TEXT}
process_stream Lambda Function sends the text of the message to a Lambda Function named agent_image_v3.
The agent receives the text and performs the following:
- Queries the Amazon DynamoDB table called
user_metadata
to see if thesession
has expired. If it is active, it recovers theSessionID
, necessary for the next step, if it expires it creates a new session timer. In Lambda Function named langchain_agent_textthe chat history is managed with the Lanchain memory class, in the Lambdas Functions agent_text_v3 and agent_image_v3 it is solved with a Json array that is fed with the history of the conversation. - Queries the Amazon DynamoDB table called session Table to see if there is any previous conversation history.
- Consult the LLM through Amazon Bedrock using the following prompt:
The following is a friendly conversation between a human and an AI.
The AI is talkative and provides lots of specific details from its context.
If the AI does not know the answer to a question, it truthfully says it does not know.
Always reply in the original user language.
- Send the response to WhatsApp through
whatsapp_out
the Lambda Function.
💡 The phrase "Always reply in the original user language" ensures that it always responds in the original language and the multilingual capacity is provided by Anthropic Claude, which is the model used in this application.
1- Get Started with the New WhatsApp Business Platform
2- How To Generate a Permanent Access Token — WhatsApp API
3- Get started with the Messenger API for Instagram
✅ Clone the repo
git clone https://github.com/build-on-aws/building-gen-ai-whatsapp-assistant-with-amazon-bedrock-and-python
✅ Go to:
cd private-assistant
In private_assistant_stack.py edit this line with the whatsapp Facebook Developer app number:
DISPLAY_PHONE_NUMBER = 'YOUR-WHATSAPP-NUMBER'
This agent manages conversation memory, and you must set the session time here in this line:
if diferencia > 240: #session time in seg
Tip: Kenton Blacutt, an AWS Associate Cloud App Developer, collaborated with Langchain, creating the Amazon Dynamodb based memory class that allows us to store the history of a langchain agent in an Amazon DynamoDB.
To use the Lambda Function langchain_agent_text: change the LAMBDA_AGENT_TEXT
environment variable in Lambda Function process_stream in private_assistant_stack:
#Line 77
Fn.process_stream.add_environment(key='ENV_LAMBDA_AGENT_TEXT', value=Fn.langchain_agent_text.function_name)
-
Configure the AWS Command Line Interface
-
Deploy architecture with CDK Follow steps:
✅ Create The Virtual Environment: by following the steps in the README
python3 -m venv .venv
source .venv/bin/activate
for windows:
.venv\Scripts\activate.bat
✅ Install The Requirements:
pip install -r requirements.txt
✅ Synthesize The Cloudformation Template With The Following Command:
cdk synth
✅🚀 The Deployment:
cdk deploy
Edit WhatsApp configuration values in Facebook Developer in AWS Secrets Manager console.
✅ The verification token is any value, but it must be the same in step 3 and 4.
- Go to Amazon API Gateway Console
- Click on
myapi
. - Go to Stages -> prod -> /cloudapi -> GET, and copy Invoke URL.
- Configure Webhook in the Facebook developer application.
- Set Invoke URL.
- Set verification token.
✅ Chat and ask follow-up questions. Test the multi-language skills.
✅ Send and transcribe voice notes. Test the app's capabilities for transcribing multiple languages.
✅ Send photos and test the app's capabilities to describe and identify what's in the images. Play with prompts
🚀 Keep testing the app, play with the prompt langchain_agent_text Amazon Lambda function and adjust it to your need.
If you finish testing and want to clean the application, you just have to follow these two steps:
- Delete the files from the Amazon S3 bucket created in the deployment.
- Run this command in your terminal:
cdk destroy
In this tutorial, you deployed a serverless WhatsApp application that allows users to interact with an LLM through Amazon Bedrock. This architecture uses API Gateway as a connection between WhatsApp and the application. Amazon Lambda functions process code to handle conversations. Amazon DynamoDB tables manage and store message information, session details, and conversation history.
You now have the essential code to improve the application. One option moving forward is to incorporate Retrieval-Augmented Generation (RAG) to generate more sophisticated responses depending on the context.
To handle customer service scenarios, the application could connect to Amazon Connect and transfer calls to an agent if the LLM cannot resolve an issue.
With further development, this serverless architecture demonstrates how conversational AI can power engaging and useful chat experiences on popular messaging platforms.
🚨 Did you like this blog? 👩🏻💻 Do you have comments?🎤 tell me every thinghere
- Get started with Amazon Connect
- Elevating Customer Support With a Whatsapp Assistant.
- RAG with history memory agents using Amazon Bedrock, Amazon Kendra, Amazon Lambda Function, and Amazon DynamoDB
- How To Choose Your LLM
- Working With Your Live Data Using LangChain
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.