I am struggling with passing context to conversational rag chain when using RunnableWithMessageHistory.
I have the following query function:
def query(query_text, prompt, session_id, metadata_context):
# History retrieval test
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
("system", "{context}"),
("system", prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
history_aware_retriever = create_history_aware_retriever(
llm, retriever, contextualize_q_prompt
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", PROMPT_TEMPLATE),
("system", "{context}"),
("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)
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)
try:
logger.info(f"Model: {LLM_MODEL} assigned. Generation of response has started.")
response = conversational_rag_chain.invoke({"input": query_text, "context": metadata_context}, config={"configurable": {"session_id": f"{session_id}"}},)
logger.info(f"Response generated.")
except Exception as e:
return ({'Generation of response failed: ': str(e)})
return response["answer"]
I want to pass my own ‘context’ that is prepared and parsed from retriever. I do not want retriever to be called again but from what I’ve read – retrieving happens by itself if chat_history does not contain the answer.
prompt variable is created:
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE)
prompt = prompt_template.format(context=metadata_context, input=query_text)
As you can see I am trying to put the context everywhere but no success.
The ‘context’ I can see when calling
conversational_rag_chain.invoke({"input": query_text, "context": metadata_context}, config={"configurable": {"session_id": f"{session_id}"}},)
logger.info(f"Response generated.")
is the result of retriever:
Document(metadata={'number_of_reviews': '16', 'price': 18999, 'product_name': 'product', 'rating': '4')
Thanks for any help.