Build a generative AI powered Serverless chat application in Go
Using DynamoDB, langchaingo, AWS Lambda Web adapter and Amazon Bedrock
Pod
permissions for invoking Amazon Bedrock.1
2
git clone https://github.com/build-on-aws/chatbot-bedrock-dynamodb-lambda-langchain
cd chatbot-bedrock-dynamodb-lambda-langchain
1
2
sam build
sam deploy -g
chat_id
):1
aws dynamodb scan --table-name langchain_chat_history
Scan
operation is used for demonstration purposes. Using Scan
in production is not recommended.1
sam delete
- 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 chains.Call call and then let Gin Stream do the heavy-lifting of handling the streaming response.
1
2
3
4
5
6
7
8
9
_, 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
}))
langchaingo
may not be as popular as the original python version (I hope it will reach there in due time 🤞), but 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.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.