LangChain CSV 代理/Pandas Dataframe 代理将 json 函数调用返回给用户而不是执行它

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

背景

我正在尝试编写一个简单的脚本来为用户提供 CSV 数据分析。我使用的是 CSV 代理,它本质上是 Pandas Dataframe 代理的包装器,两者都包含在

langchain-experimental
中。初始化 LLM 和代理(csv 代理使用包含来自在线零售商的数据的 csv 文件进行初始化)后,我使用
agent.run(user_message)
运行代理。

期待

Agent应该使用openai函数模板提示LLM,LLM将返回一个json结果,其中指定了python repl工具,以及要执行的python代码。然后,该代码应由 python repl 执行,将结果传回 LLM,LLM 将用描述结果的自然语言答案进行响应。

例如,来自文档:

agent.run("how many rows are there?")
> Entering new  chain...

Invoking: `python_repl_ast` with `df.shape[0]`


891There are 891 rows in the dataframe.

> Finished chain.

问题陈述

代理开始执行并获取描述要使用的工具和要传递给该工具的查询的 json 结果,但不调用该工具。

问题示例:

agent.run("How many rows are there?")

产生结果:

> Entering new AgentExecutor chain...
{
  "function": "python_repl_ast",
  "parameters": {
    "query": "len(df)"
  }
}

> Finished chain.

完整代码

import os
from langchain.chat_models import ChatOpenAI
from langchain.agents.agent_types import AgentType 
from langchain_experimental.agents.agent_toolkits import create_csv_agent

api_key = os.environ.get("OPENROUTER_API_KEY")
api_base = "https://openrouter.ai/api/v1"
model = "mistralai/mixtral-8x7b-instruct"
chat_model = ChatOpenAI(
    api_key=api_key,
    base_url=api_base,
    model=model,
    temperature=0.0,
)

def main():
    filepath = input("Enter the path to the CSV file: ")
    agent = create_csv_agent(
        chat_model,
        filepath,
        verbose=True,
        openai_model=chat_model,
        agent_type=AgentType.OPENAI_FUNCTIONS,
    )
    while True:
        user_message = input("You: ")
        lower_message = user_message.lower()
        if lower_message == "goodbye" or lower_message == "goodbye!":
            break
        response = agent.run(user_message)


if __name__ == "__main__":
    main()

来源

python pandas agent langchain large-language-model
1个回答
0
投票

我已经回答了我自己的问题:问题似乎是 Mistralai 模型没有返回格式正确的结果。当我将模型更改为使用

openai/gpt-4
而不是
mistralai/mixtral-8x7b-instruct
时,代理成功完成了任务。

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