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)