from langchain import LLMChain from langchain.agents import (AgentExecutor, Tool, ZeroShotAgent) from langchain_experimental.tools import PythonREPLTool # Define a description to suggest how to determine the choice of tool description = ( "Useful when you require to answer analytical questions about customers. " "Use this more than the Python REPL tool if the question is about customer analytics," "like 'How many customers are there?' or 'count the number of transactions by age group'. " "Try not to use clause in the SQL." ) # Create a Tool object for customer data with the previously defined agent executor 'create_sql_agent' and description customer_data_tool = Tool( name="Customer", func=agent_executor.run, description=description, ) # Create the whole list of tools tools = [PythonREPLTool()] tools.append(customer_data_tool) # Define the prefix and suffix for the prompt prefix = "Below are tools that you can access:" suffix = ( "Pass the relevant part of the request directly to the Customer tool.nn" "Request: {input}n" "{agent_scratchpad}" ) # Create the prompt using ZeroShotAgent # Use agent_scratchpad to store the actions previously used, guiding the subsequent responses. agent_prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "agent_scratchpad"] ) # Create an instance of ZeroShotAgent with the LLMChain and the allowed tool names zero_shot_agent = ZeroShotAgent( llm_chain=LLMChain(llm=llm, prompt=agent_prompt), allowed_tools=[tool.name for tool in tools] ) # Create an AgentExecutor which enables verbose mode and handling parsing errors agent_executor = AgentExecutor.from_agent_and_tools( agent=zero_shot_agent, tools=tools, verbose=True, handle_parsing_errors=True ) # Define user input user_inquiry = "Use a grouped bar graph to visualize the result of the following inquiry: " "What are the relationships between product category, average total amount, and gender?" # Run the agent to generate a response agent_executor.run(user_inquiry)