如何使用Langchain创建一个多用户聊天机器人

huangapple go评论93阅读模式
英文:

How to create a multi-user chatbot with langchain

问题

希望你一切都好。我已经根据以下的 Langchain 文档准备了一个基于聊天机器人:

Langchain 聊天机器人文档

在上述的 Langchain 文档中,提示模板包含了两个输入变量 - history 和 human input。

我有变量 UserID 和 SessionID。我将 UserID、SessionID、UserMessage 和 LLM-Response 存储在一个 CSV 文件中。我使用 Python 的 pandas 模块读取了 CSV,并筛选了给定的 UserID 和 SessionID 的数据框,为该特定用户会话准备了聊天历史。我将这个聊天历史作为 'history' 输入传递给 Langchain 提示模板(在上面的链接中讨论过)。由于我设置了 verbose=true,Langchain 在每次 API 调用时都会在控制台上打印提示模板。我已经开始了第一个用户和第一个会话的对话,并逐个发送了 3 个 human_inputs。然后我开始了第二个用户会话(现在会话 ID 和用户 ID 已经更改)。观察控制台上的提示模板后,我注意到 Langchain 不仅获取了第二个用户会话的聊天历史,还获取了之前用户会话的一部分聊天历史,尽管我已经编写了正确的代码来为给定的用户会话准备聊天历史。获取聊天历史的代码如下:

# 获取聊天历史
def get_chat_history(user_id, session_id, user_query):
    chat_history = "您是一个基于 OpenAI 训练的大型语言模型的聊天机器人。文本后跟 'Human:' 将是用户输入,您的回复应如下 'AI:' 所示。\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 仅考虑给定的用户会话聊天历史在其提示中。请帮助

英文:

Hope you are doing good. I’ve prepared a chatbot based on the below langchain documentation:

Langchain chatbot documentation

In the above langchain documenation, the prompt template has two input variables - history and human input.

I’ve variables for UserID, SessionID. I’m storing UserID, SessionID, UserMessage, LLM-Response in a csv file. I used python pandas module to read the csv and filtered the data frame for given UserID and SessionID and prepared the chat-history for that specific user session. I’m passing this chat-history as the ‘history’ input to the langchain prompt template(which was discussed in the above link). As I set verbose=true, the langchain was printing the prompt template on the console for every API call. I’ve started the conversation for the first user and first session and sent 3 human_inputs one by one. Later I started the second user session(now session ID and user ID are changed). After observing that prompt template on the console, I’ve observed that langchain is not only taking chat-history of second user session, it’s taking some of the chat-history from previous user session as well, even though I’ve written the correct code to prepare chat-history for the given user session. The code to get chat-history is below:

# 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

How to teach langchain to consider only the given user session chat-history in it’s prompt. Please help

答案1

得分: 1

没有看到你构建调用openai时使用的代码的确切内容(即,你是否使用ConversationChain或LLMChain?还有最重要的是,你是否像你提供的示例中使用ConversationBufferMemory?)

我之所以问关于ConversationChain(如果你没有向它传递memory参数,它将默认为你初始化ConversationMemory)和ConversationBufferMemory,是因为听起来很像你看到的问题可能是缓冲区没有被清除... 基本上,它们共享相同的缓冲区,或者MessageHistory。

抱歉,没有看到你的每个方面的代码,我无法提供更多帮助。

英文:

Without seeing exactly what code you are using when constructing the calls to openai (i.e. are you using ConversationChain or LLMChain? and most importantly, are you using ConversationBufferMemory like in the example you linked?)

The reason I ask about ConversationChain (which will by default initialize a ConversationMemory for you if you don't pass a memory arg to it) and ConversationBufferMemory, is because it sounds a lot like what you are seeing is the buffer that isn't cleared... basically, they are sharing the same buffer, or MessageHistory.

Sorry, I can't be more helpful without seeing your eac

huangapple
  • 本文由 发表于 2023年6月5日 22:24:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定