generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathkendra_retriever_open_ai.py
More file actions
53 lines (45 loc) · 1.67 KB
/
kendra_retriever_open_ai.py
File metadata and controls
53 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from langchain.retrievers import AmazonKendraRetriever
from langchain.chains import RetrievalQA
from langchain import OpenAI
from langchain.prompts import PromptTemplate
import os
def build_chain():
region = os.environ["AWS_REGION"]
kendra_index_id = os.environ["KENDRA_INDEX_ID"]
llm = OpenAI(batch_size=5, temperature=0, max_tokens=300)
retriever = AmazonKendraRetriever(index_id=kendra_index_id,region_name=region)
prompt_template = """
The following is a friendly conversation between a human and an AI.
The AI is talkative and provides lots of specific details from its context.
If the AI does not know the answer to a question, it truthfully says it
does not know.
{context}
Instruction: Based on the above documents, provide a detailed answer for, {question} Answer "don't know"
if not present in the document.
Solution:"""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain_type_kwargs = {"prompt": PROMPT}
return RetrievalQA.from_chain_type(
llm,
chain_type="stuff",
retriever=retriever,
chain_type_kwargs=chain_type_kwargs,
return_source_documents=True
)
def run_chain(chain, prompt: str, history=[]):
result = chain(prompt)
# To make it compatible with chat samples
return {
"answer": result['result'],
"source_documents": result['source_documents']
}
if __name__ == "__main__":
chain = build_chain()
result = run_chain(chain, "What's SageMaker?")
print(result['answer'])
if 'source_documents' in result:
print('Sources:')
for d in result['source_documents']:
print(d.metadata['source'])