Langchain RAG 的历史和引用

问题描述 投票:0回答:1

我正在尝试使用 langchain 构建 RAG。我想考虑聊天历史记录并能够生成引用。我已经遵循了 Langchain 上的教程,但我很难将历史和引文放在一起。

这是我的代码:

### 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`.
    )

直到这里一切都运行良好,但我找不到在上下文中插入更改的方法,以便我可以使用bind_tools并提取引用,如下所示https://python.langchain.com/docs/use_cases/ Question_answering/引用/#cite-snippets

我尝试了多种方法,但没有找到好的解决方案,因为我对 langchain 没有足够的经验。

python openai-api langchain
1个回答
0
投票

您可以通过以下方式查看

chat_history
和相关
context
(可能是来自矢量数据库的块,如果您在那里提取了一些文档)。

这是您的代码的补充。

from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.chat_message_histories import ChatMessageHistory

store = {}

# Storing unique session_id to keep track of a particular conversation
def get_session_history(session_id: str) -> BaseChatMessageHistory:
    if session_id not in store:
        store[session_id] = ChatMessageHistory()
    return store[session_id]

conversational_rag_chain = RunnableWithMessageHistory(
    rag_chain,
    get_session_history,
    input_messages_key= "input",
    history_messages_key="chat_history",
    output_messages_key="answer",
    )

# 1st Question
conversational_rag_chain.invoke(
    {"input": "Who is the president of Pakistan?"},
    config={
        "configurable": {"session_id": "abc123"}
    },  # constructs a key "abc123" in `store`.
)["answer"]

# 2nd Question
ans = conversational_rag_chain.invoke(
    {"input": "When was he elected?"},
    config={"configurable": {"session_id": "abc123"}},
)
# context/relevant docs from vectordb
print(f"{ans["answer"]} and  context: \n \n {ans["context"]}") 
© www.soinside.com 2019 - 2024. All rights reserved.