如何向 pandas 工具包代理添加对话记忆?

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

How to add conversational memory to pandas toolkit agent?

问题

我想将ConversationBufferMemory添加到pandas_dataframe_agent,但迄今为止我没有成功。

  • 我尝试通过构造函数添加内存:create_pandas_dataframe_agent(llm, df, verbose=True, memory=memory),这不会破坏代码,但也不会使代理记住我的先前问题。
  • 我还尝试通过以下代码将内存添加到代理中:pd_agent.agent.llm_chain.memory = memory。结果是ValueError: One input key expected got ['input', 'agent_scratchpad']

到目前为止,这是我的代码(不起作用):

llm = ChatOpenAI(temperature=0, model_name="gpt-4-0613")

memory = ConversationBufferMemory()

pd_agent = create_pandas_dataframe_agent(llm, df, verbose=True, memory=memory)
# pd_agent.agent.llm_chain.memory = memory #或者如果我使用这种方法,当调用.run()方法时,代码会中断

pd_agent.run("在第12步中查看数据。是否有任何奇怪的模式?我们可以对数据集的这一部分说些什么。")
pd_agent.run("我的上一个问题是什么?") #代理不记得
英文:

I want to add a ConversationBufferMemory to pandas_dataframe_agent but so far I was unsuccessful.

  • I have tried adding the memory via construcor: create_pandas_dataframe_agent(llm, df, verbose=True, memory=memory) which didn't break the code but didn't resulted in the agent to remember my previous questions.
  • Also I have tried to add memory into the agent via this pieace of code: pd_agent.agent.llm_chain.memory = memory. Which resulted in ValueError: One input key expected got ['input', 'agent_scratchpad']

This is my code so far (which doesn't work):

llm = ChatOpenAI(temperature=0, model_name="gpt-4-0613")

memory = ConversationBufferMemory()

pd_agent = create_pandas_dataframe_agent(llm, df, verbose=True, memory=memory)
#pd_agent.agent.llm_chain.memory = memory #Or if I use this approach the code breaks when calling the .run() methods

pd_agent.run("Look into the data in step 12. Are there any weird patterns? What can we say about this part of the dataset.")
pd_agent.run("What was my previouse question?") #Agent doesn't rember

答案1

得分: 1

在版本 0.0.202 中,我找到的唯一添加内存到 pandas_agent 的方法如下(您还需要更改 prompt.py 文件 - 下面的代码中有说明):

# 我们想要创建两个不同的模型 - 一个用于生成代码,第二个用于上下文
llm_code = ChatOpenAI(temperature=0, model_name="gpt-4-0613")
llm_context = ChatOpenAI(temperature=0.5, model_name="gpt-4")

chat_history_buffer = ConversationBufferWindowMemory(
    k=5,
    memory_key="chat_history_buffer",
    input_key="input"
)

chat_history_summary = ConversationSummaryMemory(
    llm=llm_context, 
    memory_key="chat_history_summary",
    input_key="input"
)

chat_history_KG = ConversationKGMemory(
    llm=llm_context, 
    memory_key="chat_history_KG",
    input_key="input",
)

memory = CombinedMemory(memories=[chat_history_buffer, chat_history_summary, chat_history_KG])

pd_agent = create_pandas_dataframe_agent(
    llm_code, 
    df, 
    verbose=True, 
    agent_executor_kwargs={"memory": memory},
    input_variables=['df_head', 'input', 'agent_scratchpad', 'chat_history_buffer', 'chat_history_summary', 'chat_history_KG']
)

首先,您需要为要使用的每种内存类型指定一个 memory_key。然后,需要将内存对象传递给 pandas_agent,如下所示:

agent_executor_kwargs={"memory": memory}

非常重要!!!

需要更改位于 ../langchain/agents/agent_toolkits/pandas/prompt.py 中的 prompt.py 文件,以考虑您添加的新内存。

您唯一需要更改的是 PREFIX。这是对我有效的更改:

PREFIX = """
您正在使用 Python 中的 pandas 数据框。数据框的名称是 `df`。
您应该使用以下工具来回答您提出的问题:

整个对话的摘要:
{chat_history_summary}

您与用户之间的最后几条消息:
{chat_history_buffer}

对话涉及的实体:
{chat_history_KG}
"""

希望这些信息对您有所帮助。

英文:

In the version 0.0.202 the only way I found out to add memory into pandas_agent is like this (you also need to change the prompt.py file - how-to is written below the code):

We want to create two diffrent models - one for generating code and the second one for the context
llm_code = ChatOpenAI(temperature=0, model_name="gpt-4-0613") #gpt-3.5-turbo-16k-0613
llm_context = ChatOpenAI(temperature=0.5, model_name="gpt-4") #gpt-3.5-turbo

chat_history_buffer = ConversationBufferWindowMemory(
    k=5,
    memory_key="chat_history_buffer",
    input_key="input"
    )

chat_history_summary = ConversationSummaryMemory(
    llm=llm_context, 
    memory_key="chat_history_summary",
    input_key="input"
    )

chat_history_KG = ConversationKGMemory(
    llm=llm_context, 
    memory_key="chat_history_KG",
    input_key="input",
    )

memory = CombinedMemory(memories=[chat_history_buffer, chat_history_summary, chat_history_KG])

pd_agent = create_pandas_dataframe_agent(
    llm_code, 
    df, 
    verbose=True, 
    agent_executor_kwargs={"memory": memory},
    input_variables=['df_head', 'input', 'agent_scratchpad', 'chat_history_buffer', 'chat_history_summary', 'chat_history_KG']
    )

First you specify for each memory type you want to use a memory_key. This memory_key needs to be passed into input_variables.

You also need to pass the memory object into the pandas_agent like this:

agent_executor_kwargs={"memory": memory}

VERY IMPORTANT!!!

You need to change the prompt.py file located in ../langchain/agents/agent_toolkits/pandas/prompt.py to take into account the new memory you added.

The only thing you need to change is PREFIX. This is the change that worked for me:

PREFIX = """
You are working with a pandas dataframe in Python. The name of the dataframe is `df`.
You should use the tools below to answer the question posed of you:

Summary of the whole conversation:
{chat_history_summary}

Last few messages between you and user:
{chat_history_buffer}

Entities that the conversation is about:
{chat_history_KG}
"""

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

发表评论

匿名网友

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

确定