Reputation: 11
So I was trying to write a code using Langchain to query my Postgres database and it worked perfectly then I tried to visualize the data if the user prompts like "Plot bar chart" now for that I found using python-repl tool could be an option but I am unable to use multiple tools (other toold is SQLDatabaseToolkit.
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
agent_executor = create_sql_agent(
llm=llm,
toolkit=toolkit,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
input_variables = ["question", "agent_scratchpad"],
suffix = suffix, # must have history as variable,
verbose = True,
handle_parsing_errors=True
)
This is my code that can query things from my database, so how can I plot from this queries?
Upvotes: 1
Views: 6717
Reputation: 195
You can create two or more agents and use them as tools with initialize_agent().
Example:- Consider CSV file having data:-
question, answer
2+3,
22/7,
9+2,
I want to fetch data from CSV file (database in your case), then use PythonREPLTool to process data and save it back to the CSV file. For this, I can create two agents as follows:-
Step 0:- Import dependencies
from langchain.llms import OpenAI
from langchain.agents.agent_toolkits import create_python_agent
from langchain.agents.agent_toolkits import create_csv_agent
from langchain.agents import initialize_agent
from langchain.tools.python.tool import PythonREPLTool
Step 1:- Create Python agent
llm = OpenAI(model_name="text-davinci-003", temperature=0)
python_agent = create_python_agent(llm, tool=PythonREPLTool(), verbose=True)
Step 2:- Create CSV agent
current_dir = os.path.dirname(__file__)
data_file_path = os.path.join(current_dir, "data.csv")
csv_agent = create_csv_agent(llm, path=data_file_path, verbose=True)
Step 3:- Create a new agent to use Python and CSV agents as tool
agent = initialize_agent(
tools=[
Tool(
name="PythonAgent",
func=python_agent.run,
description="""Useful to run python commands""",
),
Tool(
name="CSVAgent",
func=csv_agent.run,
description="""Useful to manipulate csv files""",
),
],
llm=llm,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)
output = agent.run(
"In data.csv file, evaluate expression in first column and save it to second cloumn. Do this for all rows and save new file as solved.csv"
)
Output:-
,question,answer
0,2+3,5.0
1,22/7,3.142857142857143
2,9+2,11.0
Upvotes: 1