Skip to content

windson/amazon-bedrock-rag

Repository files navigation

RAG with Amazon Bedrock using Titan Embeddings and Amazon OpenSearch Vector Engine

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

Prerequisites

  1. This was tested on Python 3.11.4

  2. 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
  3. Run ./download-beta-sdk.sh to download the beta SDK for using Amazon Bedrock

  4. Install requirements pip install -r requirements.txt

  5. Install terraform to create the OpenSearch cluster

    brew tap hashicorp/tap
    brew install hashicorp/tap/terraform

Usage

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

Cleanup

cd ./terraform 
terraform destroy # Confirm with yes