Jak*_*aur 6 python openai-api langchain
我想添加一个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 #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
Run Code Online (Sandbox Code Playgroud)
在该版本中0.0.202,我发现将内存添加到 pandas_agent 的唯一方法是这样的(您还需要更改prompt.py文件 - 操作方法写在代码下面):
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']
)
Run Code Online (Sandbox Code Playgroud)
首先,为要使用的每种内存类型指定memory_key. 这memory_key需要传递到input_variables.
您还需要将内存对象传递到 pandas_agent 中,如下所示:
agent_executor_kwargs={"memory": memory}
Run Code Online (Sandbox Code Playgroud)
您需要更改prompt.py位于的文件../langchain/agents/agent_toolkits/pandas/prompt.py以考虑您添加的新内存。
您唯一需要改变的是PREFIX。这是对我有用的改变:
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}
"""
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3070 次 |
| 最近记录: |