mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-16 17:53:37 +00:00
Added a rag template for Kendra (#12470)
## Description Adds a rag template for Amazon Kendra with Bedrock. --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
parent
eb903e211c
commit
689853902e
48
templates/rag-aws-bedrock/poetry.lock
generated
48
templates/rag-aws-bedrock/poetry.lock
generated
@ -1,4 +1,4 @@
|
||||
# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
|
||||
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "aiohttp"
|
||||
@ -839,28 +839,6 @@ files = [
|
||||
{file = "numpy-1.24.4.tar.gz", hash = "sha256:80f5e3a4e498641401868df4208b74581206afbee7cf7b8329daae82676d9463"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "openai"
|
||||
version = "0.28.1"
|
||||
description = "Python client library for the OpenAI API"
|
||||
optional = false
|
||||
python-versions = ">=3.7.1"
|
||||
files = [
|
||||
{file = "openai-0.28.1-py3-none-any.whl", hash = "sha256:d18690f9e3d31eedb66b57b88c2165d760b24ea0a01f150dd3f068155088ce68"},
|
||||
{file = "openai-0.28.1.tar.gz", hash = "sha256:4be1dad329a65b4ce1a660fe6d5431b438f429b5855c883435f0f7fcb6d2dcc8"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
aiohttp = "*"
|
||||
requests = ">=2.20"
|
||||
tqdm = "*"
|
||||
|
||||
[package.extras]
|
||||
datalib = ["numpy", "openpyxl (>=3.0.7)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)"]
|
||||
dev = ["black (>=21.6b0,<22.0)", "pytest (==6.*)", "pytest-asyncio", "pytest-mock"]
|
||||
embeddings = ["matplotlib", "numpy", "openpyxl (>=3.0.7)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)", "plotly", "scikit-learn (>=1.0.2)", "scipy", "tenacity (>=8.0.1)"]
|
||||
wandb = ["numpy", "openpyxl (>=3.0.7)", "pandas (>=1.2.3)", "pandas-stubs (>=1.1.0.11)", "wandb"]
|
||||
|
||||
[[package]]
|
||||
name = "packaging"
|
||||
version = "23.2"
|
||||
@ -1323,7 +1301,7 @@ files = [
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
greenlet = {version = "!=0.4.17", markers = "platform_machine == \"win32\" or platform_machine == \"WIN32\" or platform_machine == \"AMD64\" or platform_machine == \"amd64\" or platform_machine == \"x86_64\" or platform_machine == \"ppc64le\" or platform_machine == \"aarch64\""}
|
||||
greenlet = {version = "!=0.4.17", markers = "platform_machine == \"aarch64\" or platform_machine == \"ppc64le\" or platform_machine == \"x86_64\" or platform_machine == \"amd64\" or platform_machine == \"AMD64\" or platform_machine == \"win32\" or platform_machine == \"WIN32\""}
|
||||
typing-extensions = ">=4.2.0"
|
||||
|
||||
[package.extras]
|
||||
@ -1409,26 +1387,6 @@ requests = ">=2.26.0"
|
||||
[package.extras]
|
||||
blobfile = ["blobfile (>=2)"]
|
||||
|
||||
[[package]]
|
||||
name = "tqdm"
|
||||
version = "4.66.1"
|
||||
description = "Fast, Extensible Progress Meter"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
files = [
|
||||
{file = "tqdm-4.66.1-py3-none-any.whl", hash = "sha256:d302b3c5b53d47bce91fea46679d9c3c6508cf6332229aa1e7d8653723793386"},
|
||||
{file = "tqdm-4.66.1.tar.gz", hash = "sha256:d88e651f9db8d8551a62556d3cff9e3034274ca5d66e93197cf2490e2dcb69c7"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
|
||||
|
||||
[package.extras]
|
||||
dev = ["pytest (>=6)", "pytest-cov", "pytest-timeout", "pytest-xdist"]
|
||||
notebook = ["ipywidgets (>=6)"]
|
||||
slack = ["slack-sdk"]
|
||||
telegram = ["requests"]
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.8.0"
|
||||
@ -1578,4 +1536,4 @@ multidict = ">=4.0"
|
||||
[metadata]
|
||||
lock-version = "2.0"
|
||||
python-versions = ">=3.8.1,<4.0"
|
||||
content-hash = "d6fa65710b7733cde1cc8215452c856efcbfeb174481b97009239f8e6c5ed5c6"
|
||||
content-hash = "0bb560b6b7caa0c0d7941ccdeac8d1e80e3fae6a6dade815f7ed442c0e5108ed"
|
||||
|
@ -8,7 +8,6 @@ readme = "README.md"
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.8.1,<4.0"
|
||||
langchain = ">=0.0.313, <0.1"
|
||||
openai = ">=0.28.1"
|
||||
tiktoken = ">=0.5.1"
|
||||
faiss-cpu = ">=1.7.4"
|
||||
boto3 = ">=1.28.57"
|
||||
|
21
templates/rag-aws-kendra/LICENSE
Normal file
21
templates/rag-aws-kendra/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 LangChain, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
21
templates/rag-aws-kendra/README.md
Normal file
21
templates/rag-aws-kendra/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
# RAG AWS Kendra
|
||||
|
||||
[Amazon Kendra](https://aws.amazon.com/kendra/) is an intelligent search service powered by machine learning (ML).
|
||||
Here we will use `Anthropic Claude` for text generation and `Amazon Kendra` for retrieving documents. Together, with these two services, this application uses a Retrieval chain to answer questions from your documents.
|
||||
|
||||
(See [this page](https://aws.amazon.com/blogs/machine-learning/quickly-build-high-accuracy-generative-ai-applications-on-enterprise-data-using-amazon-kendra-langchain-and-large-language-models/) for additional context on building RAG applications with Amazon Kendra.)
|
||||
|
||||
Code here uses the `boto3` library to connect with the Bedrock service. See [this page](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration) for setting up and configuring boto3 to work with an AWS account.
|
||||
|
||||
## Kendra Index
|
||||
|
||||
You will need a Kendra Index setup before using this template. For setting up a sample index, you can use this [Cloudformation template](https://github.com/aws-samples/amazon-kendra-langchain-extensions/blob/main/kendra_retriever_samples/kendra-docs-index.yaml) to create the index. This template includes sample data containing AWS online documentation for Amazon Kendra, Amazon Lex, and Amazon SageMaker. Alternately, if you have an Amazon Kendra index and have indexed your own dataset, you can use that. Launching the stack requires about 30 minutes followed by about 15 minutes to synchronize it and ingest the data in the index. Therefore, wait for about 45 minutes after launching the stack. Note the Index ID and AWS Region on the stack’s Outputs tab.
|
||||
|
||||
## Environment variables
|
||||
|
||||
The code assumes that you are working with the `default` AWS profile and `us-east-1` region. If not, specify these environment variables to reflect the correct region and AWS profile.
|
||||
|
||||
* `AWS_DEFAULT_REGION`
|
||||
* `AWS_PROFILE`
|
||||
|
||||
This code also requires specifying the `KENDRA_INDEX_ID` env variable which should have the Index ID of the Kendra index. Note that the Index ID is a 36 character alphanumeric value that can be found in the index detail page.
|
6
templates/rag-aws-kendra/main.py
Normal file
6
templates/rag-aws-kendra/main.py
Normal file
@ -0,0 +1,6 @@
|
||||
from rag_aws_kendra.chain import chain
|
||||
|
||||
if __name__ == "__main__":
|
||||
query = "Does Kendra support table extraction?"
|
||||
|
||||
print(chain.invoke(query))
|
1737
templates/rag-aws-kendra/poetry.lock
generated
Normal file
1737
templates/rag-aws-kendra/poetry.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
templates/rag-aws-kendra/pyproject.toml
Normal file
26
templates/rag-aws-kendra/pyproject.toml
Normal file
@ -0,0 +1,26 @@
|
||||
[tool.poetry]
|
||||
name = "rag_aws_kendra"
|
||||
version = "0.0.1"
|
||||
description = ""
|
||||
authors = []
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.8.1,<4.0"
|
||||
langchain = ">=0.0.313, <0.1"
|
||||
tiktoken = ">=0.5.1"
|
||||
boto3 = ">=1.28.57"
|
||||
awscli = ">=1.29.57"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
langchain-cli = ">=0.0.4"
|
||||
fastapi = "^0.104.0"
|
||||
sse-starlette = "^1.6.5"
|
||||
|
||||
[tool.langserve]
|
||||
export_module = "rag_aws_kendra.chain"
|
||||
export_attr = "chain"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
0
templates/rag-aws-kendra/rag_aws_kendra/__init__.py
Normal file
0
templates/rag-aws-kendra/rag_aws_kendra/__init__.py
Normal file
49
templates/rag-aws-kendra/rag_aws_kendra/chain.py
Normal file
49
templates/rag-aws-kendra/rag_aws_kendra/chain.py
Normal file
@ -0,0 +1,49 @@
|
||||
import os
|
||||
|
||||
from langchain.llms.bedrock import Bedrock
|
||||
from langchain.prompts import ChatPromptTemplate
|
||||
from langchain.retrievers import AmazonKendraRetriever
|
||||
from langchain.schema.output_parser import StrOutputParser
|
||||
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
|
||||
|
||||
# Get region and profile from env
|
||||
region = os.environ.get("AWS_DEFAULT_REGION", "us-east-1")
|
||||
profile = os.environ.get("AWS_PROFILE", "default")
|
||||
kendra_index = os.environ.get("KENDRA_INDEX_ID", None)
|
||||
|
||||
if not kendra_index:
|
||||
raise ValueError(
|
||||
"No value provided in env variable 'KENDRA_INDEX_ID'. "
|
||||
"A Kendra index is required to run this application."
|
||||
)
|
||||
|
||||
# Set LLM and embeddings
|
||||
model = Bedrock(
|
||||
model_id="anthropic.claude-v2",
|
||||
region_name=region,
|
||||
credentials_profile_name=profile,
|
||||
model_kwargs={'max_tokens_to_sample': 200}
|
||||
)
|
||||
|
||||
# Create Kendra retriever
|
||||
retriever = AmazonKendraRetriever(
|
||||
index_id=kendra_index,
|
||||
top_k=5,
|
||||
region_name=region
|
||||
)
|
||||
|
||||
# RAG prompt
|
||||
template = """Answer the question based only on the following context:
|
||||
{context}
|
||||
Question: {question}
|
||||
"""
|
||||
prompt = ChatPromptTemplate.from_template(template)
|
||||
|
||||
|
||||
# RAG
|
||||
chain = (
|
||||
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
|
||||
| prompt
|
||||
| model
|
||||
| StrOutputParser()
|
||||
)
|
0
templates/rag-aws-kendra/tests/__init__.py
Normal file
0
templates/rag-aws-kendra/tests/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user