A Tool Is Just an Ordinary Function
Don't be scared by the word "tool." A tool you give an Agent is just a normal function you wrote, plus a line saying "what this function does," so the large model knows when to use it.
def get_weather(city: str) -> str:
"""查询某个城市今天的天气。"""
return weather_api(city)
tools = [get_weather] # 把工具交给 AgentWhat a Minimal Agent Looks Like
Write the "loop" as code and the skeleton is this small: the model decides whether to call a tool; if it does, you feed the result back; repeat until it says "I'm done answering."
while True:
step = model.think(history, tools)
if step.is_final: # 模型说:可以回答了
return step.answer
result = call_tool(step) # 做:执行工具
history.append(result) # 看:把结果喂回去,进入下一轮How Does the Model Know Which Tool to Call?
You give it a row of tools (check weather, query database, send email…), and it decides which one to use this time based on each tool's name and that one-line description—like finding a wrench in your toolbox by its label.
So the clearer the name and description, the more accurately it picks.
自测 · 学完检查一下
想真正动手做题、记进度、攒连胜?到互动课里练。
Judge: is the "tool" definition below acceptable? (Can the Agent know what it does and when to use it?)
def f(x):
return db(x)答案:Not acceptable
The name `f` and parameter `x` are unclear, and there's no description of what it does. The model decides when to call by name and description—you must give it a clear name plus a one-line description of its purpose.
What is the single most important thing a "good tool" definition must have?
答案:A clear name + a one-line doc describing what it's for
The model decides whether and when to use a tool by "name + description." Writing the description clearly matters more than anything.
In the code, what does the line `if step.is_final:` check?
答案:The model thinks it can now give the final answer
is_final = the model says "enough, I can answer," so it breaks out of the loop and returns the answer.
In the minimal Agent loop, after calling a tool and getting a result, you have to ___ the result back to the model so it can move to the next round of thinking.
答案:feed
Add the tool result back to the conversation history so the model can see the result of the "act"—that's what keeps the loop turning.
Given a row of tools, what does the Agent rely on to decide which one to call this time?
答案:Each tool's name and description
Name + description is the model's "manual." That's also why `f(x)` in question 1 was unacceptable.
Judge: this tool list has clear names and descriptions—is it acceptable?
def get_order_count(day: str) -> int:
"""查询某天的总下单量。"""
...
def send_email(to: str, body: str) -> None:
"""给指定邮箱发一封邮件。"""
...答案:Acceptable
The names are self-explanatory and each has a one-line purpose—the model can accurately judge when to use which. That's an acceptable tool definition.