LangChain agents help connect language models with tools to perform tasks. What is their main role?
Think about how agents extend the capabilities of language models beyond just text generation.
LangChain agents act as intermediaries that allow language models to use external tools and APIs, enabling dynamic task execution.
You want an agent that can choose between several tools based on the input question. Which agent type fits best?
Look for the agent type that uses reasoning and action steps to decide which tool to use.
ReAct agents combine reasoning and acting steps to decide dynamically which tool to use among many.
Given the following Python code using LangChain, what will be printed?
from langchain.agents import initialize_agent, Tool from langchain.llms import OpenAI tools = [Tool(name="Calculator", func=lambda x: str(eval(x)), description="Performs math calculations")] llm = OpenAI(temperature=0) agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False) result = agent.run("What is 5 plus 7?") print(result)
The Calculator tool evaluates math expressions passed as strings.
The agent uses the Calculator tool to evaluate '5 plus 7' as '5+7' which equals 12, returned as string "12".
When configuring the language model inside a LangChain agent, which hyperparameter controls how creative or random the agent's responses are?
This parameter ranges from 0 to 1 and influences randomness in output.
The temperature hyperparameter controls randomness; higher values produce more creative outputs.
Consider this code snippet:
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
tools = []
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=False)
result = agent.run("Calculate 2 + 2")
print(result)Why does it raise a ValueError?
Think about what happens if the agent has no tools to use.
The agent requires at least one tool to perform actions; an empty tools list causes a ValueError.