我尝试在python上使用langchain的pandas代理进行一些开发工作,但由于它无法对一个想法采取行动,它进入了一个递归循环,这个想法是,必须运行一些pandas代码来继续对一些sales dataset(sales.csv)上的请求提示的思考过程。
下面是代码
import os
os.environ['OPENAI_API_KEY'] = 'sk-xxx'
from langchain.agents import create_pandas_dataframe_agent
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
import pandas as pd
df = pd.read_csv('sales.csv')
llm = ChatOpenAI(temperature=0.0,model_name='gpt-3.5-turbo')
pd_agent = create_pandas_dataframe_agent(llm, df, verbose=True)
pd_agent.run("what is the mean of the profit?")
字符串
它给出的答案如下(我用-)
> Entering new chain...
Thought: We need to calculate the profit first by subtracting the cogs from the total, and then find the mean of the profit.
Action: Calculate the profit and find the mean using pandas.
Action Input:
----
df['Profit'] = df['Total'] - df['cogs']
df['Profit'].mean()
----
Observation: Calculate the profit and find the mean using pandas. is not a valid tool, try another one.
Thought:I need to use python_repl_ast to execute the code.
Action: Calculate the profit and find the mean using pandas.
Action Input: `python_repl_ast`
----
df['Profit'] = df['Total'] - df['cogs']
df['Profit'].mean()
----
Observation: Calculate the profit and find the mean using pandas. is not a valid tool, try another one.
Thought:I need to use `python` instead of `python_repl_ast`.
Action: Calculate the profit and find the mean using pandas.
Action Input: `python`
----
import pandas as pd
df = pd.read_csv('filename.csv')
df['Profit'] = df['Total'] - df['cogs']
df['Profit'].mean()
----
.
.
.
.
.
.
Observation: Calculate the profit and find the mean using pandas. is not a valid tool, try another one.
Thought:
> Finished chain.
型
第一个月
现在我的问题是为什么它不使用python_repl_ast工具来计算?我甚至改变了这个代理的工具描述(python_repl_ast),
- 一个Python shell。使用它来执行python命令。输入应该是有效的python命令。使用此工具时,有时输出是缩写的-在您的答案中使用它之前,请确保它看起来不缩写。*
成
- 一个Python shell。使用它来执行python命令和利润,使用pandas进行平均计算。输入应该是有效的python命令。使用此工具时,有时输出是缩写的-在您的答案中使用它之前,请确保它看起来不缩写。*
但是这并没有帮助,我还注意到当python_repl_ast
被初始化到我的代理中时,嵌套框架被加载到它的局部变量tools = [PythonAstREPLTool(locals={"df": df})]
中,所以我猜我做错了什么。
任何帮助都将不胜感激。谢谢。
1条答案
按热度按时间knsnq2tg1#
您在Langchain agents中提供的大部分信息都应该是用于提示上下文的。
添加下面的代码为您的代理提供自定义信息。
字符串
对我很有效。