Вот мой код:
Код: Выделить всё
### Contextualize question ### This is used to forumlate the right question to the retriever
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."""
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, retriever, contextualize_q_prompt
)
### Answer question ### This is used to answer the question based on the context
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 five sentences maximum and keep the answer concise.\
Here it is the context: {context}"""
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", qa_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}"),
]
)
question_answer_chain = create_stuff_documents_chain(llm__main, qa_prompt)
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)### Statefully manage chat history ###
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = session_history #ChatMessageHistory() #hack found for now as the RunnableWithMessageHistory requires a function. To be changed when we'll handle multiple sessions
return store[session_id]
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain_2,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer",
)
res = conversational_rag_chain.invoke(
{"input": query},
config={
"configurable": {"session_id": "abc123"}
}, # constructs a key "abc123" in `store`.
)
Я пробовал несколько подходов, но не нашел хороших решений, так как у меня их недостаточно. опыт работы с langchain.
Подробнее здесь: https://stackoverflow.com/questions/784 ... -citations