{"id":42307969,"url":"https://github.com/aws-samples/rag-using-langchain-amazon-bedrock-and-opensearch","last_synced_at":"2026-03-03T01:01:28.625Z","repository":{"id":189016907,"uuid":"679864065","full_name":"aws-samples/rag-using-langchain-amazon-bedrock-and-opensearch","owner":"aws-samples","description":"RAG with langchain using Amazon Bedrock and Amazon OpenSearch","archived":false,"fork":false,"pushed_at":"2025-01-07T15:04:08.000Z","size":51,"stargazers_count":214,"open_issues_count":0,"forks_count":42,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-06-05T08:20:32.471Z","etag":null,"topics":["aws","bedrock","embedding-vectors","genai","generative-ai","opensearch","rag","retrieval-augmented-generation","vector"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit-0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aws-samples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-17T19:40:16.000Z","updated_at":"2025-04-23T14:15:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"b26b340a-49d6-405a-9298-9502fe8fe738","html_url":"https://github.com/aws-samples/rag-using-langchain-amazon-bedrock-and-opensearch","commit_stats":null,"previous_names":["aws-samples/rag-using-langchain-amazon-bedrock-and-opensearch"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aws-samples/rag-using-langchain-amazon-bedrock-and-opensearch","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aws-samples","download_url":"https://codeload.github.com/aws-samples/rag-using-langchain-amazon-bedrock-and-opensearch/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30028228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-03T00:31:48.536Z","status":"ssl_error","status_checked_at":"2026-03-03T00:30:56.176Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","bedrock","embedding-vectors","genai","generative-ai","opensearch","rag","retrieval-augmented-generation","vector"],"created_at":"2026-01-27T11:12:46.287Z","updated_at":"2026-03-03T01:01:28.597Z","avatar_url":"https://github.com/aws-samples.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# RAG using LangChain with Amazon Bedrock Titan text, and embedding, using OpenSearch vector engine\n\nThis sample repository provides a sample code for using RAG (Retrieval augmented generation) method relaying on [Amazon Bedrock](https://aws.amazon.com/bedrock/) [Titan Embeddings Generation 1 (G1)](https://aws.amazon.com/bedrock/titan/) LLM (Large Language Model), for creating text embedding that will be stored in [Amazon OpenSearch](https://aws.amazon.com/opensearch-service/) with [vector engine support](https://aws.amazon.com/about-aws/whats-new/2023/07/vector-engine-amazon-opensearch-serverless-preview/) for assisting with the prompt engineering task for more accurate response from LLMs.\n\nAfter we successfully loaded embeddings into OpenSearch, we will then start querying our LLM, by using [LangChain](https://www.langchain.com/). We will ask questions, retrieving similar embedding for a more accurate prompt.\n\nYou can use `--bedrock-model-id` parameter, to seamlessly choose one of the available foundation model in Amazon Bedrock, that defaults to [Anthropic Claude v2](https://aws.amazon.com/bedrock/claude/) and can be replaced to any other model from any other model provider to choose your best performing foundation model.\n\nAnthropic:\n\n- Claude v2 `python ./ask-bedrock-with-rag.py --ask \"How will AI will change our every day life?\"`\n- 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?\"`\n- 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?\"`\n\nAI21 Labs:\n\n- 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?\"`\n- 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?\"`\n\n## Prerequisites\n\n1. This was tested on Python 3.11.4\n2. It is advise to work on a clean environment, use `virtualenv` or any other virtual environment manager.\n\n    ```bash\n    pip install virtualenv\n    python -m virtualenv venv\n    source ./venv/bin/activate\n    ```\n\n3. Install requirements `pip install -r requirements.txt`\n4. Install [terraform](https://developer.hashicorp.com/terraform/downloads?product_intent=terraform) to create the OpenSearch cluster\n\n    ```bash\n    brew tap hashicorp/tap\n    brew install hashicorp/tap/terraform\n    ```\n\n5. Go to the Model Access [page](https://us-east-1.console.aws.amazon.com/bedrock/home?region=us-east-1#/modelaccess) and enable the foundation models you want to use.\n\n## Steps for using this sample code\n\n1. In the first step we will launch an OpenSearch cluster using Terraform.\n\n    ```bash\n    cd ./terraform\n    terraform init\n    terraform apply -auto-approve\n    ```\n\n    \u003e\u003eThis cluster configuration is for testing proposes only, as it's endpoint is public for simplifying the use of this sample code.\n\n2. 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](https://huggingface.co) [embedding-training-data](https://huggingface.co/datasets/sentence-transformers/embedding-training-data) [gooaq_pairs](https://huggingface.co/datasets/sentence-transformers/embedding-training-data/resolve/main/gooaq_pairs.jsonl.gz), we will download it, and invoke Titan embedding to get a text embedding, that we will store in OpenSearch for next steps.\n\n    ```bash\n    python load-data-to-opensearch.py --recreate 1 --early-stop 1\n    ```\n\n    \u003e\u003eOptional arguments:\n    \u003e\u003e- `--recreate` for recreating the index in OpenSearch\n    \u003e\u003e- `--early-stop` to load only 100 embedded documents into OpenSearch\n    \u003e\u003e- `--index` to use a different index than the default **rag**\n    \u003e\u003e- `--region` in case you are not using the default **us-east-1**\n    \u003e\u003e- `--multi-tenant` to use multi tenancy, will load data with tenant IDs (1-5)\n    \n\n3. Now that we have embedded text, into our OpenSearch cluster, we can start querying our LLM model Titan text in Amazon Bedrock with RAG\n\n    ```bash\n    python ask-bedrock-with-rag.py --ask \"your question here\"\n    ```\n\n    \u003e\u003eOptional arguments:\n    \u003e\u003e- `--index` to use a different index than the default **rag**\n    \u003e\u003e- `--region` in case you are not using the default **us-east-1**\n    \u003e\u003e- `--bedrock-model-id` to choose different models than Anthropic's Claude v2\n    \u003e\u003e- `--tenant-id` to filter only a specific tenant ID\n\n\n### Cleanup\n\n```bash\ncd ./terraform\nterraform destroy # When prompt for confirmation, type yes, and press enter.\n```\n\n## Contributing\n\nSee [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information.\n\n## License\n\nThis library is licensed under the MIT-0 License. See the LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faws-samples%2Frag-using-langchain-amazon-bedrock-and-opensearch/lists"}