![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
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.
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)
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.