Are Langchain toolkits able to be modified? Can we add tools to a pandas_dataframe_agent toolkit?
Asked Answered
O

1

6

I'm new to langchain, so I'm guessing this is possible but demonstrates my lack of a full understanding of the components in langchain. I have successfully created and used the Pandas Dataframe Agent toolkit to analyze a small dataframe. I have also successfully used the SERPAPI tool to utilize the Google Search API and retrieve answers from the internet. My goal is to combine the two things into one agent...an agent that can query data for answers when they exist in the data, and default to the internet for when they don't. I feel like there is likely a way to add a tool to an existing agent tookit. The alternative seems to be creating a new agent from scratch that replicates what the pandas dataframe agent does and adds additional tools...but that's beyond my current skillset at this point.

Given a dataframe sales_df:

from langchain.agents import create_pandas_dataframe_agent
from langchain.llms import OpenAI

question = 'Which itemnumber has the most sales?'
agent = create_pandas_dataframe_agent(OpenAI(temperature=0), 
                                      sales_df,
                                      verbose=True,
                                     )
agent.run(question)

This code successfully returns the correct answer to my pandas dataframe question.

Given the itemnumber from above (xxx'd out for privacy):

from langchain.agents import initialize_agent, load_tools, AgentType
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
tools = load_tools(['serpapi'], serpapi_api_key=SERPAPI_API_KEY)

agent = initialize_agent(
    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)

agent.run("What is the description of the product which has an itemnumber of XXXXXXXX?")

This code successfully returns a description of the product.

I'm looking for a way to combine these two things - such that the agent can both analyze a dataframe and search for answers on the web.

I've tried this....it's doesn't throw an error but the agent does not seem to have any idea that it should have a search tool available to use, so it continues to rely upon the dataframe to try to find answers (and comes back with wrong ones, since the answer cannot be found there)

from langchain.agents import create_pandas_dataframe_agent
from langchain.agents import load_tools
from langchain.llms import OpenAI
from langchain import SerpAPIWrapper
from langchain.tools import Tool


question = 'Which itemnumber has the most sales and what is the product description of the itemnumber?'
search = SerpAPIWrapper(serpapi_api_key=SERPAPI_API_KEY)
my_tools = [
    Tool.from_function(
        func=search.run,
        name="Search",
        description="useful for when you need to lookup answers outside of the dataset"
    ),
]

agent = create_pandas_dataframe_agent(OpenAI(temperature=0), 
                                      sales_df,
                                      verbose=True,
                                      tools = tools + my_tools,
                                     )
agent.run(question)
Ophir answered 29/6, 2023 at 16:59 Comment(2)
Hi Mark, did you find a way? I'm looking for the same stuffGylys
https://mcmap.net/q/1918667/-how-can-i-add-an-extra-tool-to-csvagent-or-pandasdataframeagent-from-langchain-libraryKierkegaardian
M
0

I ran into the same thought and problem when exploring. Please reference this documentation:

https://api.python.langchain.com/en/latest/agents/langchain.agents.agent.AgentExecutor.html#langchain.agents.agent.AgentExecutor

You can pass a pandas DataFrame to the AgentExecutor class as long as the DataFrame is compatible with the requirements of the AgentExecutor. The AgentExecutor class has several parameters that can accept different types of input, including a pandas DataFrame.

It looks like you can pass the DataFrame as part of the metadata parameter, which is an optional dictionary that allows you to provide additional data to the agent execution.

Martynne answered 1/8, 2023 at 21:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.