mirror of https://github.com/hwchase17/langchain
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>pull/12492/head
parent
eb903e211c
commit
689853902e
@ -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.
|
@ -0,0 +1,6 @@
|
||||
from rag_aws_kendra.chain import chain
|
||||
|
||||
if __name__ == "__main__":
|
||||
query = "Does Kendra support table extraction?"
|
||||
|
||||
print(chain.invoke(query))
|
File diff suppressed because it is too large
Load Diff
@ -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,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()
|
||||
)
|
Loading…
Reference in New Issue