如何在对话应用中添加聊天历史记录

在许多问答应用中,我们希望用户能够进行来回对话,这意味着应用需要某种形式的“记忆”来保存过去的问答,并在当前思考中整合这些记录。在本指南中,我们重点介绍如何将历史消息整合到对话逻辑中。

我们将探索两种方法:

  1. Chain:每次执行检索步骤。
  2. Agent:通过LLM判断是否以及如何执行检索步骤(可能是多个步骤)。

我们将使用来自RAG教程中的由Lilian Weng撰写的《LLM Powered Autonomous Agents》博客作为外部知识源。

技术背景介绍

在对话问答应用中,经常需要用户可以参考前面的对话内容。这种历史记录功能对于提高用户体验和对话的流畅性非常重要。

核心原理解析

我们主要通过创建一个能感知历史的检索器(history-aware retriever)实现这种对话记忆功能。这个检索器接收输入和聊天历史,然后根据这些信息进行内容检索,以提供准确的答案。

代码实现演示

我们将使用LangChain、OpenAI嵌入和Chroma向量存储进行演示,以下是完整的代码实现:

import os
import getpass
import bs4
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_chroma import Chroma
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_community.document_loaders import WebBaseLoader
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

# 检查并设置API密钥
if not os.environ.get("OPENAI_API_KEY"):
    os.environ["OPENAI_API_KEY"] = getpass.getpass(prompt='Enter your OpenAI API key: ')

# 创建LLM实例
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

# 构建检索器
loader = WebBaseLoader(
    web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
    bs_kwargs=dict(
        parse_only=bs4.SoupStrainer(
            class_=("post-content", "post-title", "post-header")
        )
    ),
)
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
retriever = vectorstore.as_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, contextualize_q_prompt
)

# 回答问题
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, say that you "
    "don't know. Use three sentences maximum and keep the "
    "answer concise."
    "\n\n"
    "{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", 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)

# 管理聊天历史
store = {}

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",
)

# 进行对话
response = conversational_rag_chain.invoke(
    {"input": "What is Task Decomposition?"},
    config={"configurable": {"session_id": "abc123"}}
)
print(response["answer"])

response = conversational_rag_chain.invoke(
    {"input": "What are common ways of doing it?"},
    config={"configurable": {"session_id": "abc123"}}
)
print(response["answer"])

# 检查对话历史
from langchain_core.messages import AIMessage

for message in store["abc123"].messages:
    prefix = "AI" if isinstance(message, AIMessage) else "User"
    print(f"{prefix}: {message.content}\n")

应用场景分析

这种方法非常适合需要保持上下文连续性的对话应用,比如客服机器人、交互式学习平台等。

实践建议

  1. 确保对话历史记录的存储和检索性能,以避免对话的延迟。
  2. 定期清理会话历史,以避免资源浪费。
  3. 根据应用需求,选择合适的嵌入和向量存储方案。

如果遇到问题欢迎在评论区交流。

—END—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值