Coder Social home page Coder Social logo

aws-samples / rag-using-langchain-amazon-bedrock-and-opensearch Goto Github PK

View Code? Open in Web Editor NEW
153.0 3.0 33.0 42 KB

RAG with langchain using Amazon Bedrock and Amazon OpenSearch

License: MIT No Attribution

Python 72.88% HCL 27.12%
aws bedrock embedding-vectors genai generative-ai opensearch vector rag retrieval-augmented-generation

rag-using-langchain-amazon-bedrock-and-opensearch's Introduction

RAG using LangChain with Amazon Bedrock Titan text, and embedding, using OpenSearch vector engine

This sample repository provides a sample code for using RAG (Retrieval augmented generation) method relaying on Amazon Bedrock Titan Embeddings Generation 1 (G1) LLM (Large Language Model), for creating text embedding that will be stored in Amazon OpenSearch with vector engine support for assisting with the prompt engineering task for more accurate response from LLMs.

After we successfully loaded embeddings into OpenSearch, we will then start querying our LLM, by using LangChain. We will ask questions, retrieving similar embedding for a more accurate prompt.

You can use --bedrock-model-id parameter, to seamlessly choose one of the available foundation model in Amazon Bedrock, that defaults to Anthropic Claude v2 and can be replaced to any other model from any other model provider to choose your best performing foundation model.

Anthropic:

  • Claude v2 python ./ask-bedrock-with-rag.py --ask "How will AI will change our every day life?"
  • Claude v1.3 python ./ask-bedrock-with-rag.py --bedrock-model-id anthropic.claude-v1 --ask "How will AI will change our every day life?"
  • Claude Instance v1.2 python ./ask-bedrock-with-rag.py --bedrock-model-id anthropic.claude-instant-v1 --ask "How will AI will change our every day life?"

AI21 Labs:

  • Jurassic-2 Ultra python ./ask-bedrock-with-rag.py --bedrock-model-id ai21.j2-ultra-v1 --ask "How will AI will change our every day life?"
  • Jurassic-2 Mid python ./ask-bedrock-with-rag.py --bedrock-model-id ai21.j2-mid-v1 --ask "How will AI will change our every day life?"

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. Install requirements pip install -r requirements.txt

  4. Install terraform to create the OpenSearch cluster

    brew tap hashicorp/tap
    brew install hashicorp/tap/terraform
  5. Go to the Model Access page and enable the foundation models you want to use.

Steps for using this sample code

  1. In the first step we will launch an OpenSearch cluster using Terraform.

    cd ./terraform
    terraform init
    terraform apply -auto-approve

    This cluster configuration is for testing proposes only, as it's endpoint is public for simplifying the use of this sample code.

  2. Now that we have a running OpenSearch cluster with vector engine support we will start uploading our data that will help us with prompt engineering. For this sample, we will use a data source from Hugging Face embedding-training-data gooaq_pairs, we will download it, and invoke Titan embedding to get a text embedding, that we will store in OpenSearch for next steps.

    python load-data-to-opensearch.py --recreate 1 --early-stop 1

    Optional arguments:

    • --recreate for recreating the index in OpenSearch
    • --early-stop to load only 100 embedded documents into OpenSearch
    • --index to use a different index than the default rag
    • --region in case you are not using the default us-east-1
  3. Now that we have embedded text, into our OpenSearch cluster, we can start querying our LLM model Titan text in Amazon Bedrock with RAG

    python ask-bedrock-with-rag.py --ask "your question here"

    Optional arguments:

    • --index to use a different index than the default rag
    • --region in case you are not using the default us-east-1
    • --bedrock-model-id to choose different models than Anthropic's Claude v2

Cleanup

cd ./terraform
terraform destroy # When prompt for confirmation, type yes, and press enter.

Contributing

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

rag-using-langchain-amazon-bedrock-and-opensearch's People

Contributors

amazon-auto avatar arik-porat avatar omerh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rag-using-langchain-amazon-bedrock-and-opensearch's Issues

Where can I found the embedding vector in the OpenSearch Dashboards?

I am confused at founding embedding vector in OpenSearch Dashboards, after I use the code to bulk the text and embedding vector into the OpenSearch
For further information, I found the chunked text in [Query Workbench], but can not found the embedding vector, I woulf be greatful for anyone's help.

What is username and password

Hi, when I run terraform, it outputs the opensearch domain that I should use and it asks me about username and password. I don't know wherre from to get it. The documentation does not say it explicitely in my opinion. How should I do it?

Maybe I don't understand something here. Do I need already need to be somehow logged in before I run terraform command? On behalf of which AWS user is created this resource?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.