DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Required Knowledge To Pass AWS Certified Data Analytics Specialty Exam
  • Serverless at Scale
  • Using Identity-Based Policies With Amazon DynamoDB
  • Streamlining AWS Lambda Deployments

Trending

  • Sprint Anti-Patterns
  • How To Optimize Your Agile Process With Project Management Software
  • WebSocket vs. Server-Sent Events: Choosing the Best Real-Time Communication Protocol
  • Understanding Escape Analysis in Go
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Build a Serverless GenAI Solution With Lambda, DynamoDB, LangChain, and Amazon Bedrock

Build a Serverless GenAI Solution With Lambda, DynamoDB, LangChain, and Amazon Bedrock

Explore how DynamoDB is used as the chat history backend along with AWS Lambda Web adapter for response streaming.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Jan. 22, 24 · Tutorial
Like (1)
Save
Tweet
Share
2.5K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous blog, I demonstrated how to use Redis (Elasticache Serverless as an example) as a chat history backend for a Streamlit app using LangChain. It was deployed to EKS and also made use of EKS Pod Identity to manage the application Pod permissions for invoking Amazon Bedrock.

This use-case here is a similar one: a chat application. I will switch back to implementing things in Go using langchaingo (I used Python for the previous one) and continue to use Amazon Bedrock. But there are a few unique things you can explore in this blog post:

  • The chat application is deployed as an AWS Lambda function along with a Function URL.
  • It uses DynamoDB as the chat history store (aka Memory) for each conversation - I extended langchaingo to include this feature.
  • Thanks to the AWS Lambda Web Adapter, the application was built as a (good old) REST/HTTP API using a familiar library (in this case, Gin).
  • And the other nice add-on was to be able to combine Lambda Web Adapter streaming response feature with Amazon Bedrock streaming inference API.

Build a Serverless GenAI Solution With Lambda, DynamoDB, LangChain, and Amazon Bedrock

Deploy Using SAM CLI (Serverless Application Model)

Make sure you have Amazon Bedrock prerequisites taken care of and the SAM CLI installed.

git clone https://github.com/abhirockzz/chatbot-bedrock-dynamodb-lambda-langchain
cd chatbot-bedrock-dynamodb-lambda-langchain


Run the following commands to build the function and deploy the entire app infrastructure (including the Lambda Function, DynamoDB, etc.)

sam build 
sam deploy -g


Once deployed, you should see the Lambda Function URL in your terminal. Open it in a web browser and start conversing with the chatbot!

Serverless AI Chat

Inspect the DynamoDB table to verify that the conversations are being stored (each conversation will end up being a new item in the table with a unique chat_id):

aws dynamodb scan --table-name langchain_chat_history


Scan operation is used for demonstration purposes. Using Scan in production is not recommended.

Quick Peek at the Good Stuff

  • Using DynamoDB as the backend store history: Refer to the GitHub repository if you are interested in the implementation. To summarize, I implemented the required functions of the schema.ChatMessageHistory.
  • Lambda Web Adapter Streaming response + LangChain Streaming: I used the chains.WithStreamingFunc option with the chains.Call call and then let Gin Stream do the heavy lifting of handling the streaming response.

Here is a sneak peek of the implementation (refer to the complete code here):

        _, err = chains.Call(c.Request.Context(), chain, map[string]any{"human_input": message}, chains.WithMaxTokens(8191), chains.WithStreamingFunc(func(ctx context.Context, chunk []byte) error {

            c.Stream(func(w io.Writer) bool {
                fmt.Fprintf(w, (string(chunk)))
                return false
            })

            return nil
        }))


Closing Thoughts

I really like the extensibility of LangChain. While I understand that langchaingo may not be as popular as the original Python version (I hope it will reach there in due time), it's nice to be able to use it as a foundation and build extensions as required. Previously, I had written about how to use the AWS Lambda Go Proxy API to run existing Go applications on AWS Lambda. The AWS Lambda Web Adapter offers similar functionality but it has lots of other benefits, including response streaming and the fact that it is language agnostic.

Oh, and one more thing - I also tried a different approach to building this solution using the API Gateway WebSocket. Let me know if you're interested, and I would be happy to write it up!

If you want to explore how to use Go for Generative AI solutions, you can read up on some of my earlier blogs:

  • Building LangChain applications with Amazon Bedrock and Go - An introduction
  • Serverless Image Generation Application Using Generative AI on AWS
  • Generative AI Apps With Amazon Bedrock: Getting Started for Go Developers
  • Use Amazon Bedrock and LangChain to build an application to chat with web pages

Happy building!

AWS AWS Lambda Amazon DynamoDB generative AI

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Required Knowledge To Pass AWS Certified Data Analytics Specialty Exam
  • Serverless at Scale
  • Using Identity-Based Policies With Amazon DynamoDB
  • Streamlining AWS Lambda Deployments

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: