如何使用langchain创建多用户聊天机器人

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

希望你做得很好。我根据以下 langchain 文档准备了一个聊天机器人:

Langchain 聊天机器人文档

在上面的langchain文档中,提示模板有两个输入变量——历史记录和人工输入。

我有 UserID、SessionID 变量。我将 UserID、SessionID、UserMessage、LLM-Response 存储在 csv 文件中。我使用 python pandas 模块读取 csv 并过滤给定 UserID 和 SessionID 的数据帧,并为该特定用户会话准备聊天历史记录。我将此聊天历史记录作为“历史记录”输入传递给 langchain 提示模板(在上面的链接中进行了讨论)。当我设置 verbose=true 时,langchain 会在控制台上为每个 API 调用打印提示模板。我已经开始与第一个用户和第一个会话进行对话,并逐个发送 3 个 human_input。后来我开始了第二个用户会话(现在会话ID和用户ID已更改)。在控制台上观察该提示模板后,我发现 langchain 不仅获取了第二个用户会话的聊天历史记录,而且还获取了上一个用户会话的一些聊天历史记录,即使我已经编写了正确的内容为给定用户会话准备聊天历史记录的代码。获取聊天记录的代码如下:

# get chat_history
def get_chat_history(user_id,session_id,user_query):
    chat_history = "You're a chatbot based on a large language model trained by OpenAI. The text followed by Human: will be user input and your response should be followed by AI: as shown below.\n"
    chat_data = pd.read_csv("DB.csv")
    for index in chat_data.index:
        if ((chat_data['user_id'][index] == user_id) and (chat_data['session_id'][index] == session_id)):
            chat_history += "Human: " + chat_data['user_query'][index] + "\n" + "AI: " + chat_data['gpt_response'][index] + "\n"
    chat_history += "Human: " + user_query + "\n" + "AI: "
    return chat_history

如何教 langchain 在提示中仅考虑给定的用户会话聊天历史记录。请帮忙

chatbot openai-api chatgpt-api langchain llm
2个回答
1
投票

在构建对 openai 的调用时,没有确切地看到您使用的代码(即您使用的是 ConversationChain 还是 LLMChain?最重要的是,您是否像您链接的示例中那样使用 ConversationBufferMemory?)

我询问 ConversationChain (如果您不向其传递内存参数,默认情况下会为您初始化 ConversationMemory)和 ConversationBufferMemory 的原因是因为它听起来很像您所看到的缓冲区,但它不是清除...基本上,它们共享相同的缓冲区或 MessageHistory。

抱歉,如果没有看到您的eac,我无法为您提供更多帮助


0
投票

您需要将每个用户的聊天历史记录存储在数据库中,然后针对当前用户的用户 ID 运行主链,并通过该用户 ID 从数据库中获取历史记录。

这里有一个教程,说明如何使用 Firestore 执行此操作,但您可以交换到任何其他支持的数据库。

https://wnmurphy.com/creating-a-versatile-multi-prompt-chatbot-with-memory-and-a-data-store-in-langchain/

© www.soinside.com 2019 - 2024. All rights reserved.