This code sample demonstrates using RAG (Retrieval Augmented Generation) with Amazon Bedrock's Titan embeddings and a large language model (LLM). It creates text embeddings stored in Amazon OpenSearch with vector engine support. This enables prompt engineering for more accurate LLM responses.
This sample does the following
- Creates embeddings using Titan embeddings LLM
- Saves embeddings to Amazon OpenSearch
- Using LangChain, input Query from you (user) is queried for retrieval from vectorized data from Amazon OpenSearch
- Retrieval from OpenSearch matching the question is fed to Amazon Titan Text LLM as context
- Amazon Titan Text augments the response based on the User Query
-
This was tested on Python 3.11.4
-
It is advise to work on a clean environment, use
virtualenv
or any other virtual environment manager.pip install virtualenv python -m virtualenv venv source ./venv/bin/activate
-
Run
./download-beta-sdk.sh
to download the beta SDK for using Amazon Bedrock -
Install requirements
pip install -r requirements.txt
-
Install terraform to create the OpenSearch cluster
brew tap hashicorp/tap brew install hashicorp/tap/terraform
First, deploy an OpenSearch cluster with Terraform:
cd ./terraform
terraform init
terraform apply -auto-approve
Using Sagemaker jupyter notebook rag1.ipynb and then rag2.ipynb
cd ./terraform
terraform destroy # Confirm with yes