Building a Conversational AI Chatbot With AWS Lambda Function and Amazon EFS | by Yi Ai | Towards Data Science

Amazon announced the general availability of AWS Lambda support for Amazon Elastic File System. Amazon EFS is a fully managed, elastic, shared file system and designed to be consumed by other AWS services.
With the release of Amazon EFS for Lambda, we can now easily share data across function invocations. It also opens new capabilities, such as building/importing large libraries and machine learning models directly into Lambda functions. Let’s go over how to build a serverless conversational AI chatbot using Lambda function and EFS.
In this post, we will:
Create an Amazon Elastic File System
Deploy and run a SageMaker notebook instance and Mount EFS to instance.
Download PyTorch libraries and ConvAI pre-trained model to EFS.
Add dialog history DynamoDB table and Gateway endpoint to save & retrieve conversation history.
Deploy a chatbot engine Lambda function and enable EFS for it.
Here’s the architecture diagram:
In this example we will use CloudFormation to create EFS and EFS access point, the configuration is defined as follows:
Note that we will use EFS General Purpose performance mode since it has lower latency than Max I/O.
We will mount EFS on Amazon SageMaker on a SageMaker notebook and Install PyTorch and ConvAI model on EFS.
The notebook instance must have access to the same security group and reside in the same VPC as the EFS file system.
Let’s mount EFS path /py-libs to /home/ec2-user/SageMaker/libs directory:
Then, install PyTorch and simpletransformers to lib/py-libs directory:
!sudo pip --no-cache-dir install torch -t libs/py-libs!sudo pip --no-cache-dir install torchvision -t libs/py-libs!sudo pip --no-cache-dir install simpletransformers -t libs/py-libs
Once we have all packages installed, download pre-trained model provided by Hugging Face, then extract the archive to convai-model directory on EFS.
!sudo wget https://s3.amazonaws.com/models.huggingface.co/transfer-learning-chatbot/gpt_personachat_cache.tar.gz!sudo tar -xvf gpt_personachat_cache.tar.gz -C libs/convai-model!sudo chmod -R g+rw libs/convai-model
We are now ready to talk to the pre-trained model, simply call model.interact().
The pre-trained model provided by Hugging Face performs well out-of-the-box and will likely require less fine-tuning when creating chatbot.
We can see that python packages and model consumed from EFS correctly and we are able to start the conversation with pre-trained model.
Create a DialogHistory table to store dialog history with at least the last utterance from user. We can use sample CloudFormation templates to configure the DynamoDB table.
Please note that We have to create a VPC endpoint for DynamoDB even though the Lambda function is running inside a public subnet of a VPC.
We will use AWS SAM to create Lambda functions and mount EFS access points to Lambda function.
First, create a Lambda function resource, then setup EFS File System for Lambda. Make sure that EFS and Lambda are in the same VPC:
In this section, we will create a Lambda function for communication between users and conversation AI model.
We will contain the following source code in src/api.py :
Note that simpletransformers library allows us to interact with the models locally with input(). To build our chat engine, we need to override the default method interact and sample_sequenc in conv_ai :
We are almost there! Now we have to deploy our bot. Run the following command to deploy:
$sam deploy --capabilities CAPABILITY_NAMED_IAM --guided
From the output above, we can see the chatbot is now deployed.
Now it’s time to test our bot. Go to CloudFormation Resources list in the AWS Management Console to find the Lambda function name and Invoke lambda function using following command:
$aws lambda invoke --function-name "chat-efs-api-HelloFunction-KQSNKF5K0IY8" out --log-type Tail  \--query 'LogResult' --output text |  base64 -d
The output will look like below:
Here is an example of dialog:
>>hi therehow are you?>>good, thank youwhat do you like to do for fun?>>I like reading, yourself?i like to listen to classical music......
It works! As we can see from above screenshot, a chatbot returns response based on input from a user.
However, I am aware of the impact of cold starts on response times. The first request took ~30 secs for cold starts to complete. To prevent cold start in our Lambda functions, we can use Provisioned Concurrency to keep functions warm:
As a result, the latency of warmed-up function is reduced to ~3 seconds:
That’s it! I hope you have found this article useful, The source code for this post can be found in my GitHub repo.