AWS Logo
Menu
Build generative AI applications in Go using Amazon Titan Text Premier model

Build generative AI applications in Go using Amazon Titan Text Premier model

Use Amazon Titan Text Premier model with the langchaingo package

Abhishek Gupta
Amazon Employee
Published Sep 3, 2024
In this blog I will walk you through how to use the Amazon Titan Text Premier model in your Go applications with langchaingo which is a Go port of langchain (originally written for Python and JS/TS).
Amazon Titan Text Premier is an advanced LLM within the Amazon Titan Text family. It is useful for a wide range of tasks including RAG, agents, chat, chain of thought, open-ended text generation, brainstorming, summarization, code generation, table creation, data formatting, paraphrasing, rewriting, extraction, and Q&A. Titan Text Premier is also optimized for integration with Agents and Knowledge Bases for Amazon Bedrock.

Getting started with Titan Text Premier and langchaingo

Let's start with an example.
Refer to Before You Begin section in this blog post to complete the prerequisites for running the examples. This includes installing Go, configuring Amazon Bedrock access and providing necessary IAM permissions.
You can refer to the complete code here. To run the example:
I got this below response for the prompt "Explain AI in 100 words or less", but it may be different in your case:
Here is a quick walk through of the code:
We start by getting instantiating bedrockruntime.Client:
We use that to create the langchaingo llm.Model instance - note that modelID we specify is that of Titan Text Premier which is amazon.titan-text-premier-v1:0.
We create a llms.MessageContent and the LLM invocation is done by llm.GenerateContent. Notice how you don't have to think about Titan Text Premier specific request/response payload - that's abstracted by langchaingo:

Chat with your documents

This is a pretty common scenario as well. langchaingo support a bunch of types including text, PDF, HTML (and even Notion!).
You can refer to the complete code here. To run this example:
The example uses this page from the Amazon Bedrock User guide as the source document (HTML) but feel free to use any other source:
You should be prompted to enter your question:
I tried these questions and got pretty accurate responses:
By the way, the Chat with your document feature is available natively in Amazon Bedrock as well.
Let's walk through the code real quick. We start by loading the content from the source URL:
Then, we begin the conversation, using a simple for loop:
The chain that we use is created with a custom prompt (based on this guideline) - we override the default behavior in langchaingo:
Now for the last example - another popular use case.

RAG - Retrieval Augmented Generation

I have previously covered how to use RAG in your Go applications. This time we will use:
  • Titan Text Premier as the LLM,
  • Titan Embeddings G1 as the embedding model,
  • PostgreSQL as the vector store (running locally using Docker) using pgvector extension.
Start the Docker container:
Activate pgvector extension by logging into PostgreSQL (using psql) from a different terminal:
You can refer to the complete code here. To run this example:
The example uses Amazon Bedrock Studio page as the source document (HTML) but feel free to use any other source:
You should see an output, and prompted to enter your questions. I tried these:
As usual, let's see what's going on. Data is loading is done in a similar way as before, and same goes for the conversation (for loop):
The RAG part is slightly different. We use a RetrievelQA chain with a custom prompt (similar to one used by Knowledge Bases for Amazon Bedrock):

Conclusion

I covered Amazon Titan Text Premier, which one of multiple text generation models in the Titan family. In addition to text generation, Amazon Titan also has embeddings models (text and multimodal), and image generation. You can learn more by exploring all of these in the Amazon Bedrock documentation. Happy building!
 

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.

Comments