Agents and Tools
Giving the LLM the ability to think, decide, and execute external functions.
🧑🏫 Sabse pehle — simple mein samjho#
Ab tak LLM sirf text generate kar raha tha based on us data ke jo tumne prompt mein daala (RAG). Par kya ho agar user puche "Aaj Delhi ka weather kya hai?". LLM ka training data purana hota hai, usko aaj ka weather nahi pata. Yahan par Agents aate hain. Tum LLM ko ek "Tool" (jaise Weather API call karne ka function) dete ho. Jab LLM dekhta hai ki use current weather chahiye, toh wo tumhare function ko call karne ka decision leta hai, data lata hai, aur phir final answer deta hai. Agent matlab LLM ko internet aur APIs ki taqat dena!
What is an Agent?#
In an LCEL Chain, the sequence of events is hardcoded (e.g., always embed the query, always search Chroma, always generate an answer).
An Agent uses an LLM as a reasoning engine to determine which actions to take and in what order. If you give an Agent three tools (a Web Search tool, a Calculator tool, and a SQL Database tool), the LLM reads the user's prompt and decides which tool to use. If it needs to use the calculator to sum up the web search results, it will do that automatically!
1. Creating Tools#
A tool is simply a Python function wrapped in a decorator @tool. The docstring of the function is critically important—the LLM reads the docstring to understand what the tool does and when to use it!
from langchain_core.tools import tool
# The docstring here is mandatory. The LLM reads it!
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two integers together."""
return a * b
@tool
def get_current_weather(location: str) -> str:
"""Get the current weather in a given location."""
# Imagine this makes an actual API call to OpenWeatherMap
return f"The weather in {location} is 72 degrees and sunny."
# Create a list of tools we want to give to the agent
tools = [multiply, get_current_weather]
2. Binding Tools to the LLM#
We must use an LLM that supports "Function Calling" (like gpt-4o or gpt-3.5-turbo). We bind our Python tools to the model so the model knows they exist.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
# Bind the tools to the model
llm_with_tools = llm.bind_tools(tools)
3. Creating and Running the Agent#
To run an agent, we typically use the create_tool_calling_agent helper and wrap it in an AgentExecutor. The Executor is the loop that actually calls the Python functions and passes the results back to the LLM.
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
# Pull a standard agent prompt from the LangChain Hub
prompt = hub.pull("hwchase17/openai-functions-agent")
# 1. Create the agent logic
agent = create_tool_calling_agent(llm, tools, prompt)
# 2. Create the executor loop
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# 3. Ask a question!
response = agent_executor.invoke({
"input": "What is the weather in New York? And what is 15 multiplied by 4?"
})
print(response["output"])
What happens behind the scenes (The ReAct Loop):#
- Thought: The LLM reads the input. It decides it needs the weather first.
- Action: The LLM stops generating text and outputs a JSON command to execute
get_current_weather("New York"). - Observation: The AgentExecutor runs the Python function and feeds the string "72 degrees and sunny" back to the LLM.
- Thought: The LLM reads the observation. Now it needs to do math.
- Action: It commands the executor to run
multiply(15, 4). - Observation: The executor runs the math and feeds
60back. - Final Answer: The LLM combines all observations and outputs: "The weather in New York is 72 degrees and sunny. Also, 15 multiplied by 4 is 60."