DEV Community

Cover image for Generative AI Serverless - RAG using Bedrock Converse API & Lambda - Chat with your document
Girish Bhatia
Girish Bhatia

Posted on

Generative AI Serverless - RAG using Bedrock Converse API & Lambda - Chat with your document

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

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

Example of Bedrock Converse API with document message

Image DocMsg API

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

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

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

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

Image response2

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

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.

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

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

Top comments (0)