Generative AI Serverless - RAG using Bedrock Converse API & Lambda - Chat with your document
Tags: aws, generativeai, bedrockconverseapi, amazonbedrock
Published: 2024-07-11 20:04:41


Generative AI - Has Generative AI captured your imagination to the extent it has for me?
 
Generative AI is indeed fascinating! The advancements in foundation models have opened up incredible possibilities. Who would have imagined that technology would evolve to the point where you can generate content summaries from transcripts, have chatbots that can answer questions on any subject without requiring any coding on your part, or even create custom images based solely on your imagination by simply providing a prompt to a Generative AI service and foundation model? It's truly remarkable to witness the power and potential of Generative AI unfold.
 
In this article, I am going to show you how to build a serverless GenAI RAG solution to implement a **document chat feature using Amazon Bedrock Converse API and Lambda.**
 
While Bedrock already provided a 'Chat with your Document' feature, with the introduction of Converse API, a DocumentBlock has been added so that user can provide a document for contextual conversation and can ask questions based on the data in this document.  Document can be hosted in S3 bucket or can be provided to function at runtime. 
 
Use of DocumentBlock in Amazon Bedrock's Converse API can be considered a form of RAG for generative AI since it leverage the data provided in the document to enhance the generation of contextual responses.
 
RAG, which stands for Retrieval Augmented Generation, is becoming increasingly popular in the world of Generative AI. It allows organizations to overcome the limitations of LLMs and utilize contextual data for their Generative AI solutions.
 
I will use the recently released Anthropic Sonnet foundation model and invoke it via the Amazon Bedrock Converse using Lambda and API. 
 
There are many use cases where generative AI chat with your document function can help increase productivity. Few examples will be technical support extracting info from user manual for quick resolution of questions from the customers, or HR answering questions based on policy documents or developer using technical documentation to get info about specific function or a call center team addressing inquiries from customers quickly by chatting with product documentation. 
 
Let's look at our **use cases**:
 
•	MyBankGB, a fictitious bank, offers various credit cards to consumers. The document "MyBankGB Credit Card Offerings.txt" contains detailed information about all the features and details of the credit cards offered by the bank.
 
•	MyBankGB is interested in implementing a Generative AI solution using the "Chat with your document" feature of Amazon Bedrock. 
 
•	This solution will enable the call center team to quickly access information about the card features and efficiently address customer inquiries.
 
•	The solution needs to be API-based so that it can be invoked via different applications.
 
Here is the **architecture** diagram for our use case.

![Image Architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg2ef4xqxjethv7wcrlf.png)

Let's see the steps to create this solution using Amazon Bedrock Converse, Lambda and API.
 
**Review AWS Bedrock Converse DocumentBlock and types supported**
 
DocumentBlock includes a document for the message to be passed to Converse API
Valid document format are: PDF, txt, doc, docx, xls, xlsx, html, md
 
**Example of document message**

![Image Doc Msg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubyfdt5n0aemudeanzek.png)

**Example of Bedrock Converse API with document message**


![Image DocMsg API](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iypijojr48z2x37q4zop.png)

**Request Model Access**
 
Before you can use the model, you must request access to the model. We need to select Anthropic Sonnet model.
 
**Create a SAM template**
 
I will create a SAM template for the lambda function that will contain the code to invoke Bedrock Converse API along with required parameters and a prompt for the RAG. Lambda function can be created without the SAM template however, I prefer to use Infra as Code approach since that allow for easy recreation of cloud resources. Here is the SAM template for the lambda function.


![Image SAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/myp05q9ujuywkn6k1431.png)

**Create a Lambda Function**
 
The Lambda function serves as the core of this automated solution. It contains the code necessary to fulfill the business requirement of creating an API for RAG based generative AI solution. This Lambda function accepts a prompt, which is then forwarded to the Bedrock Converse API to generate a response using the Anthropic Sonnet foundation model. Now, Let’s look at the code behind it.


![Image Lambda](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umiymr8flnrjlmcuq1io.png)

**Build function locally using AWS SAM**
 
Next build and validate function using AWS SAM before deploying the lambda function in AWS cloud. Few SAM commands used are: 
 
•	SAM Build
•	SAM deploy
 
**Validate the GenAI Model response using a prompt**
 
Prompt engineering is an essential component of any Generative AI solution. It is both art and science, as crafting an effective prompt is crucial for obtaining the desired response from the foundation model. Often, it requires multiple attempts and adjustments to the prompt to achieve the desired outcome from the Generative AI model.
 
Given that I'm deploying the solution to AWS API Gateway, I'll have an API endpoint post-deployment. I plan to utilize Postman for passing the prompt in the request and reviewing the response. Additionally, I can opt to post the response to an AWS S3 bucket for later review.
 
 
**Prompt:** Which card is best for the dining?

![Image response1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwgao204ba86f6rue6lx.png)


**Prompt:** List all credit cards offered by MyBankGB. Also list annual fee for each card.

![Image response2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whbec0s4yuoaw07sw14t.png)

**Prompt:** A customer is looking for a travel card with no foreign transaction fee. Suggest the best card for the customer. Also, list three benefits provided by this card.

![Image response3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xsro47aa1lpe9hq5ipob.png)

Based on the prompt, requested info is returned using the augmented info provided in the document.
 
With these steps, a serverless GenAI solution has been successfully completed to implement a chat with your document solution using Amazon Bedrock Converse, Lambda, and API. Python/Boto3 were utilized to invoke the Bedrock API with Anthropic Sonnet.
 
As GenAI solutions keep improving, they will change how we work and bring real benefits to many industries. This workshop shows how powerful AI can be in solving real-world problems and creating new opportunities for innovation.
 
Thanks for reading!
 
Click here to get to YouTube video for this solution.

{% embed https://www.youtube.com/watch?v=3tL9-DsQaAo %}

https://www.youtube.com/watch?v=3tL9-DsQaAo

𝒢𝒾𝓇𝒾𝓈𝒽 ℬ𝒽𝒶𝓉𝒾𝒶
𝘈𝘞𝘚 𝘊𝘦𝘳𝘵𝘪𝘧𝘪𝘦𝘥 𝘚𝘰𝘭𝘶𝘵𝘪𝘰𝘯 𝘈𝘳𝘤𝘩𝘪𝘵𝘦𝘤𝘵 & 𝘋𝘦𝘷𝘦𝘭𝘰𝘱𝘦𝘳 𝘈𝘴𝘴𝘰𝘤𝘪𝘢𝘵𝘦
𝘊𝘭𝘰𝘶𝘥 𝘛𝘦𝘤𝘩𝘯𝘰𝘭𝘰𝘨𝘺 𝘌𝘯𝘵𝘩𝘶𝘴𝘪𝘢𝘴𝘵