0

I have a very simple code using the langchain library to query a pandas dataframe.

model = AzureOpenAI(
            deployment_name="text-davinci-003",
            openai_api_version="2023-05-15",
            openai_api_key=MY_KEY,
            openai_api_base=MY_LINK,
            openai_api_type="azure",
            temperature=0,
        )

data = get_data() # this is a pandas dataframe

agent = create_pandas_dataframe_agent(
            model,
            data,
            verbose=True,
        )

response = agent.run(MY_QUESTION)

I would like my agent to be able to also use the GoogleSearch Tool from the same library when the pandas dataframe does not give the answer.

Right now, when I ask for a cheesecake recipe (for example), which can't be found in the dataframe, the agent repeatedly says in the logs :

Thought: i need to find a recipe online
Action: google
Action Input: cheesecake recipe
Observation: google is not a valid tool, try one of [python_repl_ast].

Eventually, I get this error :

Agent stopped due to iteration limit or time limit.

I know custom agents must be the solution, however I am very confused as to how to implement it. Indeed, in the source code of create_pandas_dataframe_agent, it seems that the agent that is returned can't be modified, or that its tools can't be modified. I am not an expert obviously. I wish there was a way to do this thing :

from langchain.tools import Tool
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.agents import create_pandas_dataframe_agent

search = GoogleSearchAPIWrapper()

tools = [
    Tool(
        name="PandasDataFrameReader",
        func=create_pandas_dataframe_agent,
        description="useful for when you need to answer questions about the event",
    ),
    Tool(
        name="Google Search",
        description="Search Google for recent results.",
        func=search.run,
    )
]

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

But I know that this is not possible since create_pandas_dataframe_agent is not really a Tool (I just gave this example to hopefully make my question clearer).

I think that the person here was trying to achieve the same thing : Are Langchain toolkits able to be modified? Can we add tools to a pandas_dataframe_agent toolkit? But I was not able to understand the answer that was given since I did not understand how to join the AgentExecutor with the Google Searhc tool.

Any help or hint would be greatly appreciated !

perly
  • 3
  • 3

0 Answers0