Complete the code to create a basic LangChain agent using an LLM.
from langchain.agents import initialize_agent from langchain.llms import OpenAI llm = OpenAI(temperature=0) agent = initialize_agent(llm, tools=[], agent= [1] )
The initialize_agent function requires the agent type as a string. "zero-shot-react-description" is a common agent type for LangChain agents that react to inputs and decide actions.
Complete the code to add a tool to the LangChain agent.
from langchain.agents import Tool tool = Tool(name="Search", func=lambda x: "Searching..." + x, description="Search tool") agent = initialize_agent(llm, tools=[[1]], agent="zero-shot-react-description")
The tools parameter expects a list of Tool objects. Here, the variable tool is the correct object to pass.
Fix the error in the code to run the LangChain agent on an input.
response = agent.run([1]) print(response)
The run method expects a string input. The input must be a string literal or variable containing a string. Passing a raw string without quotes causes a syntax error.
Fill both blanks to create a custom tool and add it to the agent.
def custom_func(input_text): return f"Processed: {input_text}" custom_tool = Tool(name=[1], func=[2], description="Custom processing tool") agent = initialize_agent(llm, tools=[custom_tool], agent="zero-shot-react-description")
The Tool constructor requires the name as a string and the function as a callable. So the name is a string like "CustomTool" and the function is the function object custom_func.
Fill all three blanks to create an agent that uses multiple tools and runs a query.
tool1 = Tool(name=[1], func=lambda x: f"Tool1: {x}", description="First tool") tool2 = Tool(name=[2], func=lambda x: f"Tool2: {x}", description="Second tool") agent = initialize_agent(llm, tools=[tool1, tool2], agent="zero-shot-react-description") response = agent.run([3]) print(response)
The first tool is named "Tool1" (matching the variable), the second is "ToolTwo" as a distinct name, and the query passed to run must be a string question.