Code Monkey home page Code Monkey logo

aws-private-assistant's Introduction

Open Source Private Assistant on AWS

πŸ‡»πŸ‡ͺπŸ‡¨πŸ‡± Dev.to Linkedin GitHub Twitter Instagram Youtube Linktr


This app is integrated to WhatsApp, enabling you to chat with an LLM on Amazon Bedrock. Send voice notes and receive transcriptions. With just a small tweak in the code, you can send the transcription to the model too.

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.

Digrama parte 1

βœ… AWS Level: Intermediate - 200

Prerequisites:

πŸ’° Cost to complete:

How The App Works

Digrama parte 1

1- Message input:

Digrama parte 1

  1. WhatsApp receives the message: voice/text.
  2. Amazon API Gateway receives the message from the WhatsApp webhook (previously authenticated).
  3. 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.
  4. The DynamoDB table whtsapp-metadata has a DynamoDB streaming configured, which triggers the process_stream Lambda Function.

2 - Message processing:

Text Message:

Digrama parte 1 process_stream Lambda Function sends the text of the message to the lambda function named langchain_agent_text (in the next step we will explore it).

Voice Message:

Digrama parte 1

  1. 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.

Digrama parte 1

  1. 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}

3- LLM Processing:

Digrama parte 1

The agent receives the text and performs the following:

  1. Queries the Amazon DynamoDB table called user_metadata to see if the session has expired. If it is active, it recovers the SessionID, necessary for the next step, if it expires it creates a new session timer.
  2. Queries the Amazon DynamoDB table called session Table to see if there is any previous conversation history.
  3. 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.

    Current conversation:
    {history}

    Human:{input}

    Assistant:
  1. 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.

Let's build!

Step 0: Activate WhatsApp account Facebook Developers

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

Step 1: APP Set Up

βœ… Clone the repo

git clone https://github.com/elizabethfuentes12/aws-private-assistant

βœ… Go to:

cd private-assistant

Step 2: Deploy architecture with CDK.

In private_assistant_stack.py edit this line with the whatsapp Facebook Developer app number:

DISPLAY_PHONE_NUMBER = 'YOU-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.

Digrama parte 1

βœ… 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

Deployment Time

Step 3: WhatsApp Configuration

Edit WhatsApp configuration values in Facebook Developer in AWS Secrets Manager console.

Digrama parte 1

βœ… The verification token is any value, but it must be the same in step 3 and 4.

Step 4: Webhook Configuration

  1. Go to Amazon API Gateway Console
  2. Click on myapi.
  3. Go to Stages -> prod -> /cloudapi -> GET, and copy Invoke URL.

Invoke Url

  1. Configure Webhook in the Facebook developer application.
    • Set Invoke URL.
    • Set verification token.

Digrama parte 1


πŸš€ Keep testing the app, play with the prompt langchain_agent_text Amazon Lambda function and adjust it to your need.

🚨 Did you like this blog? πŸ‘©πŸ»β€πŸ’» Do you have comments?🎀 tell me everythinghere


Β‘Gracias!

πŸ‡»πŸ‡ͺπŸ‡¨πŸ‡± Dev.to Linkedin GitHub Twitter Instagram Youtube Linktr


Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

aws-private-assistant's People

Contributors

elizabethfuentes12 avatar

Stargazers

Santiago Garcia Arango avatar  avatar Fady Nabil avatar Angel Sanchez Tenjo avatar DemiΓ‘n Arancibia avatar  avatar Alexis Araujo avatar Jason Weill avatar Jhonny Pong avatar  avatar louix avatar Enrique Rodriguez avatar Abhishek M Shivalingaiah avatar Gary Jarrel avatar

Watchers

 avatar

Forkers

agiusalexandre

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.