Complete the code to create an agent that uses a language model and tools.
from langchain.agents import initialize_agent from langchain.llms import OpenAI from langchain.tools import Tool llm = OpenAI(temperature=0) tools = [Tool(name="Search", func=search_function)] agent = initialize_agent(tools, llm, agent_type=[1])
The initialize_agent function requires an agent_type string. "zero-shot-react-description" is a common agent type that adds autonomy by letting the agent decide which tools to use.
Complete the code to add a tool that the agent can use to fetch current weather.
def get_weather(location: str) -> str: # Imagine this calls a weather API return f"Weather in {location} is sunny" weather_tool = Tool(name="Weather", func=[1], description="Get current weather")
The tool's function must be the actual function defined to get weather, which is get_weather.
Fix the error in the agent call to run a query with tool usage.
response = agent.run(input=[1])
The input to agent.run must be a string, so it needs quotes around the query text.
Fill both blanks to create a tool that calls a calculator function and add it to the tools list.
def calculator(query: str) -> str: # Simple calculator logic return str(eval(query)) calc_tool = Tool(name=[1], func=[2], description="Perform calculations") tools = [calc_tool]
The tool name should be a string like "Calculator" and the function should be the actual calculator function.
Fill all three blanks to create an agent that uses both weather and calculator tools with zero-shot reasoning.
weather_tool = Tool(name=[1], func=get_weather, description="Get current weather") calc_tool = Tool(name=[2], func=calculator, description="Perform calculations") tools = [weather_tool, calc_tool] agent = initialize_agent(tools, llm, agent_type=[3])
The tools must be named "Weather" and "Calculator" matching their functions, and the agent type for autonomy is "zero-shot-react-description".