Reputation: 23
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 !
Upvotes: 2
Views: 2478
Reputation: 995
For those facing difficulties in integrating tools with the pandas agent, I've successfully integrated additional functionalities into the create_pandas_dataframe_agent
.
Please note that my experience is primarily with the pandas agent. However, upon reviewing the source code, I believe this could also be applied to the CSV agent.
import openai
import pandas as pd
from dotenv import load_dotenv
from langchain.chat_models import AzureChatOpenAI
from langchain.agents import Tool
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent
from langchain.agents import AgentType
from langchain_community.tools.pubmed.tool import PubmedQueryRun
from langchain.tools import YouTubeSearchTool
pubmed_ = PubmedQueryRun()
youtube_ = YouTubeSearchTool()
##read env variables and create your llm (i will skip this part for simplicity)
tools = [
Tool(
name="Pubmed",
func=pubmed_.run,
description="useful for when you need to answer questions about medical research"
),
Tool(
name = "Youtube",
func = youtube_.run,
description = "useful for when you need to answer questions about videos"
),
]
PREFIX = " if question is not related with pandas, you can use extra tools. the extra tools are: 1. Pubmed 2. Youtube"
df = pd.read_csv("foo.csv")
result = create_pandas_dataframe_agent(llm, df, extra_tools=tools,verbose=True, handle_parsing_errors=True, prefix=PREFIX,agent_type = AgentType.ZERO_SHOT_REACT_DESCRIPTION)
print(result.invoke("send me 2 lex friedman videos on youtube"))
Packages:
Notes: Please ensure to utilize the langchain-experimental and langchain packages as I have specified.
Upvotes: 1