Build a Q&A Application with Amazon Bedrock and Amazon Titan
In the previous tutorial, we created an Amazon OpenSearch Serverless instance to store and retrieve the text embeddings generated from the Kaggle Academy Awards dataset.
This guide focuses on building a question-answering application based on Retrieval Augmented Generation (RAG). We will continue to use the same environment and the Jupyter Notebook we launched in the previous tutorial.
Step 1 – Build the Context Through Semantic Search
Our goal is to retrieve the top matches based on the prompt and then concatenate the text to create the context.
Let’s start with a prompt that will be asked by the user and convert that into vector embeddings by sending it to the Titan Embeddings models.
prompt='who won the award for best music?' vector=text_embedding(prompt)
Based on this vector, we will now perform a semantic search.
response=search_index(vector) response['hits']['hits']

Let’s create the context by concatenating the value of the nominee_text element.
data=response['hits']['hits']
context = ''
for item in data:
context += item['_source']['nominee_text'] + '\n'
print(context)

Step 2 – Augmenting the Prompt with Context and Invoking the Titan Model
We will now create an augmented prompt from the context that includes the context and the original prompt.
augmented_prompt=f'Context - {context}\nBased on the above context, answer this question - {prompt}'
With the final prompt in place, let’s invoke the model.
config={
"maxTokenCount": 1000,
"stopSequences": [],
"temperature":0.1,
"topP":1
}
body = json.dumps({'inputText': augmented_prompt,'textGenerationConfig':config})
response = bedrock.invoke_model(
modelId='amazon.titan-tg1-large',
body=body
)
response_body = json.loads(response.get('body').read())
print(response_body.get('results')[0].get('outputText'))

You can change the prompt and run this to see accurate responses from Titan.
For the prompt, “Which character did Brendan Fraser play in the film The Whale?” you get the below response, which is correct.

This concludes the tutorial on implementing RAG with Amazon Bedrock, Amazon Titan and Amazon OpenSearch Serverless.