AmazonkendraRetriver #28739
Replies: 1 comment 7 replies
-
I found a similar discussion that might be relevant to your issue: AmazonkendraRetriver, which is currently unsolved [1]. To ensure that all top 10 documents retrieved from Amazon Kendra are used by the LLM to summarize the question, you should verify that the Here's a brief overview of how
Ensure that your To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
-
from langchain_community.retrievers import AmazonKendraRetriever
import orjson
import boto3
from langchain.chains import RetrievalQA ,ConversationalRetrievalChain
from langchain.prompts import PromptTemplate
from langchain_community.llms import OpenAI
from langchain_openai import AzureChatOpenAI
import json
from openai import AzureOpenAI
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.messages import HumanMessage
from langchain.chains import create_history_aware_retriever
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
import os
from new_promts import prompt1
store = {}
def lambda_handler(event, context):
region = os.environ["REGION"]
kendra_index_id = os.environ["KENDRA_INDEX"]
print("Event: ",event)
#llm = OpenAI(batch_size=5, temperature=0, max_tokens=300)
The base URL for your Azure OpenAI resource. e.g. "https://.openai.azure.com"
#azure_endpoint="https://api.geneai.thermofisher.com/dev"
#gpt 4 config
azure_endpoint = os.environ["azure_endpoint"]
API version e.g. "2023-07-01-preview"
#openai_api_version="2024-03-01-preview"
openai_api_version="2024-03-01-preview"
The name of your Azure OpenAI deployment chat model. e.g. "gpt-35-turbo-0613"
#deployment_name="gpt-35-turbo"
deployment_name="gpt-4o"
The API key for your Azure OpenAI resource.
openai_api_key = os.environ["openai_api_key"]
This is set to
azure
openai_api_type="azure"
llm = AzureChatOpenAI(
azure_endpoint=azure_endpoint,
openai_api_version=openai_api_version,
deployment_name=deployment_name,
openai_api_key=openai_api_key,
openai_api_type=openai_api_type,
)
#retriever = AmazonKendraRetriever(index_id=kendra_index_id, region_name=region)
data_source_id = '1bf6a354-7aef-471e-b5b9-9ce733e9d085'
retriever = AmazonKendraRetriever(index_id=kendra_index_id, top_k=10,
attribute_filter = {'AndAllFilters':
[
{"EqualsTo": {"Key": "_language_code","Value": {"StringValue": "en"}}},
{"EqualsTo": {"Key": "_data_source_id","Value": {"StringValue": data_source_id}}}
contextualize_q_system_prompt = """Given a chat history and the latest user question
which might reference context in the chat history, formulate a standalone question
which can be understood without the chat history. Do NOT answer the question,
just reformulate it if needed and otherwise return it as is."""
#prompt_template = prompt_template1
#prompt_template= prompmt3
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt )
'''qa_system_prompt = """You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know.
Use three sentences maximum and keep the answer concise.\
'''
print("Context: ",context)
qa_system_prompt = prompt1
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", qa_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
query = event.get("query", "SELECT 1")
sessionId = event.get("sessionId", "SELECT 1")
#sessionId
question = query
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)
ai_msg_1 = conversational_rag_chain.invoke(
{"input": question},
config={
"configurable": {"session_id": sessionId}
},
)["answer"]
print("ai_msg_1 :",ai_msg_1)
#print("ai_msg_2 :",ai_msg_2)
print("********",store[sessionId].messages)
Extract retrieved documents from the retriever
print(retriever)
retrieved_docs = retriever.invoke(question)
print("Fetched Documents:")
for i, doc in enumerate(retrieved_docs):
print(f"Document {i + 1}:")
print(f"Page Content: {doc.page_content}")
print(f"Metadata: {doc.metadata}")
return ai_msg_1
'''
chat_history = []
query = event.get("query", "SELECT 1")
question = query
ai_msg_1 = rag_chain.invoke({"input": question, "chat_history": chat_history})
chat_history.extend([HumanMessage(content=question), ai_msg_1["answer"]])
print("ai_msg_1: ",ai_msg_1["answer"])
for document in ai_msg_1["context"]:
print("Retrived document: ",document)
print()
print(chat_history)
'''
#return "success"
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = ChatMessageHistory()
return store[session_id]
can you please help me to pass all the documents
Beta Was this translation helpful? Give feedback.
All reactions