![Build GenAI Apps with Golang and Amazon Bedrock](https://assets.community.aws/a/2e9wn0ODaiS6p6tHLyHJvMIi4F6/vide.webp)
Build GenAI Apps with Golang and Amazon Bedrock
While many production code written in Golang, Java, etc., 90% learning material on building GenAI apps now is in Python and JavaScript. So this note shows how to get started with Amazon Bedrock in Golang through implementing few basic examples without using frameworks like Langchain or Streamlit.
Published Mar 25, 2024
While many production code written in Golang, Java, etc., 90% learning material on building GenAI apps now is in Python and JavaScript. So this note shows how to get started with Amazon Bedrock in Golang through implementing few basic examples
- Simple chat and prompt
- Query vector database (OpenSearch)
- Simple image analyzing
In addition, it implement these features using only basic concepts and without relying on framework like LangChain, Streamlit, or React. This is good for leaners to understand a bit deeper.
- basic stream response
- basic css and javascript
More details
main.go implement a http server and route request to handlers. bedrock.go and aoss.go are functions to invoke Amazon Bedrock and Amazon OpenSearch Serverless (AOSS), respectively. A static folder contains simple frontend with javascript.
Please note that:
To use AOSS, you need create a OpenSearch collection and provide its URL endpoint in constants.go. In addition, you need to setup data access in the AOSS for the running time environment (EC2 profile, ECS taks role, Lambda role, .etc)
First it is good to create some data structs according to Amazon Bedrock Claude3 API format
Then convert the payload to bytes and invoke Bedrock client
Finally, parse the streaming response and decode to text. When deploy on a http server, we need to modify the code a bit to stream each chunk of response to client.
Similarly, for image analyzing using Amazon Bedrock Claude3, we need to create a correct request format. It is possible without explicitly define structs as above and using interface{}
Then invoke Amazon Bedrock Client like below, and similar for streaming response as previous example.
- create OpenSearch client
- convert user question to embedding vector
- send query or request to OpenSearch
A OpenSearch and Bedrock client can be initialized as below
Create a function to convert text to vector by invoking Amazon Bedrock Titan model.
Then send request or query to AOSS