Langchain - 带记忆和自定义提示的对话检索链

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

我正在尝试创建一个ConversationalRetrievalChain来根据pdf文件提供的特定上下文进行回答。 我可以获得很好的答案。问题是内存不工作。

代码:

template2 = """
    Your name is Bot.
    You are a chatbot specialized in human resources. 
    Use the following context (delimited by <ctx></ctx>) to answer the questions.
    If there is any history of previous conversations, use it to answer (delimited by <hs></hs>)
    If you don't know the answer just answer that you don't know. 
    ------
    <ctx>
    {context}
    </ctx>
    ------
    <hs>
    {chat_history}
    </hs>
    ------
    Question:
    {question} 
    """
    
    
prompt2 = PromptTemplate(
                template=template2, 
                input_variables=["context", "chat_history", "question"])


def querying_V1(query, chat_history,prompt):
     memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, output_key='answer')

    conv_chain = ConversationalRetrievalChain.from_llm(
          llm=llm,
          chain_type="stuff",
          retriever=vectorstore.as_retriever(search_kwargs={"k": 2}), 
          memory=memory,
          # condense_question_prompt=prompt,
          combine_docs_chain_kwargs={"prompt": prompt},
          output_key='answer',
          # return_source_documents=True,
          get_chat_history=lambda h : h,
          verbose = False
    )

    result = conv_chain({"question": query, "chat_history": chat_history})
    return result["answer"].strip()

while True:
    query = input("Prompt: ")
    if query == "q":
         sys.exit()
    result = querying_V1(query,chat_history,prompt2)
    print("\n" + result)
    chat_history.append((query, result))

这可以根据 vectorstore 中提供的上下文回答问题。但如果我问 “我提出的最后一个问题是什么?”,结果是 “我不知道”

我读过这里 为什么 langchain ConversationalRetrievalChain 不记得聊天记录,即使我将其添加到 chat_history 参数中? 如果在 while 循环的每次迭代中创建 ConversationalRetrievalChain 对象,则新内存将覆盖前一个内存。

如果我在函数外部定义

memory
conv_chain
并调用
conv_chain
作为输入:

def querying_V2(query : str, conv_chain: object, chat_history):
    result = conv_chain({"question": query, "chat_history": chat_history})
    return result["answer"].strip()

内存可以工作,但似乎忘记了提示中传递的上下文...

我已经尝试将

result
更改为
result = conv_chain({"question": query)
以及基于类似问题的其他一些建议。但直到现在,我还无法让 ConversationalRetrievalChain 根据提示模板中提供的上下文以及 chat_history 进行应答。

python artificial-intelligence chatbot langchain large-language-model
1个回答
0
投票

我知道我迟到了,但是看看你的代码我可以知道问题是什么,也许这会帮助其他人。在您的代码中,内存初始化作为该特定函数的局部变量进行。因此,每次调用“querying_V1”函数时,都会初始化一个新的内存对象(ConversationBufferMemory)。如果您使用 Streamlit api,以下更改将帮助您解决问题:

def querying_V1(query, chat_history,prompt):
     if "memory" not in st.session_state:
          st.session_state.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, output_key='answer')

     conv_chain = ConversationalRetrievalChain.from_llm(
          llm=llm,
          chain_type="stuff",
          retriever=vectorstore.as_retriever(search_kwargs={"k": 2}), 
          memory=st.session_state.memory,
          # condense_question_prompt=prompt,
          combine_docs_chain_kwargs={"prompt": prompt},
          output_key='answer',
          # return_source_documents=True,
          get_chat_history=lambda h : h,
          verbose = False
    )
© www.soinside.com 2019 - 2024. All rights reserved.