mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-19 08:09:21 +03:00
Add Multi-CSV/DF support in CSV and DataFrame Toolkits * CSV and DataFrame toolkits now accept list of CSVs/DFs * Add default prompts for many dataframes in `pandas_dataframe` toolkit Fixes #1958 Potentially fixes #4423 ## Testing * Add single and multi-dataframe integration tests for `pandas_dataframe` toolkit with permutations of `include_df_in_prompt` * Add single and multi-CSV integration tests for csv toolkit --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
6.7 KiB
6.7 KiB
In [1]:
from langchain.agents import create_csv_agentIn [2]:
from langchain.llms import OpenAIIn [4]:
agent = create_csv_agent(OpenAI(temperature=0), 'titanic.csv', verbose=True)In [5]:
agent.run("how many rows are there?")Out [5]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3mThought: I need to count the number of rows Action: python_repl_ast Action Input: df.shape[0][0m Observation: [36;1m[1;3m891[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: There are 891 rows.[0m [1m> Finished chain.[0m
'There are 891 rows.'
In [6]:
agent.run("how many people have more than 3 siblings")Out [6]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3mThought: I need to count the number of people with more than 3 siblings Action: python_repl_ast Action Input: df[df['SibSp'] > 3].shape[0][0m Observation: [36;1m[1;3m30[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 30 people have more than 3 siblings.[0m [1m> Finished chain.[0m
'30 people have more than 3 siblings.'
In [7]:
agent.run("whats the square root of the average age?")Out [7]:
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mThought: I need to calculate the average age first
Action: python_repl_ast
Action Input: df['Age'].mean()[0m
Observation: [36;1m[1;3m29.69911764705882[0m
Thought:[32;1m[1;3m I now need to calculate the square root of the average age
Action: python_repl_ast
Action Input: math.sqrt(df['Age'].mean())[0m
Observation: [36;1m[1;3mNameError("name 'math' is not defined")[0m
Thought:[32;1m[1;3m I need to import the math library
Action: python_repl_ast
Action Input: import math[0m
Observation: [36;1m[1;3m[0m
Thought:[32;1m[1;3m I now need to calculate the square root of the average age
Action: python_repl_ast
Action Input: math.sqrt(df['Age'].mean())[0m
Observation: [36;1m[1;3m5.449689683556195[0m
Thought:[32;1m[1;3m I now know the final answer
Final Answer: 5.449689683556195[0m
[1m> Finished chain.[0m
'5.449689683556195'
In [3]:
agent = create_csv_agent(OpenAI(temperature=0), ['titanic.csv', 'titanic_age_fillna.csv'], verbose=True)
agent.run("how many rows in the age column are different?")Out [3]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3mThought: I need to compare the age columns in both dataframes Action: python_repl_ast Action Input: len(df1[df1['Age'] != df2['Age']])[0m Observation: [36;1m[1;3m177[0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 177 rows in the age column are different.[0m [1m> Finished chain.[0m
'177 rows in the age column are different.'
In [ ]: