Generative AI Serverless - RAG using Bedrock Converse API & Lambda - Chat with your document
RAG using Bedrock Converse API, DocumnetBlock, Lambda via Anthropic Sonnet model
Published Jul 11, 2024
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.
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
Example of Bedrock Converse API with document message
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.
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.
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 Generation 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?
Prompt: List all credit cards offered by MyBankGB. Also list annual fee for each card.
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.
Β 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.
Β
Β
π’πΎππΎππ½ β¬π½πΆππΎπΆ
πππ ππ¦π³π΅πͺπ§πͺπ¦π₯ ππ°ππΆπ΅πͺπ°π― ππ³π€π©πͺπ΅π¦π€π΅ & ππ¦π·π¦ππ°π±π¦π³ ππ΄π΄π°π€πͺπ’π΅π¦
πππ°πΆπ₯ ππ¦π€π©π―π°ππ°π¨πΊ ππ―π΅π©πΆπ΄πͺπ’π΄π΅
Β
πππ ππ¦π³π΅πͺπ§πͺπ¦π₯ ππ°ππΆπ΅πͺπ°π― ππ³π€π©πͺπ΅π¦π€π΅ & ππ¦π·π¦ππ°π±π¦π³ ππ΄π΄π°π€πͺπ’π΅π¦
πππ°πΆπ₯ ππ¦π€π©π―π°ππ°π¨πΊ ππ―π΅π©πΆπ΄πͺπ’π΄π΅
Β