Have the Model Call a Function
This section does just one thing: give the model a description of an "addition" function, ask it an addition problem, and see how it responds. Once you finish, you will understand the most fundamental building block of an Agent.
First, Think of an Analogy
You are the boss, and you have an assistant who is great at talking but has no calculator in hand. You hand him a note: "When you need to do addition, write me a slip that says 'Please calculate a plus b,' and I'll press the calculator for you."
So instead of guessing the answer himself, he hands you a slip: "Please calculate 3829 plus 9471."
The model is this assistant, and a function call is that slip. The model does not execute any function itself; it only states, in the format you provide, "which function I want to call and what the parameters are." What actually executes is the program you wrote.
Code
Download step1_call.py, or type it yourself:
import json, os
from openai import OpenAI
client = OpenAI(base_url=os.environ.get("AGENT_BASE_URL", "http://localhost:11434/v1"),
api_key=os.environ.get("AGENT_API_KEY", "ollama"))
MODEL = os.environ.get("AGENT_MODEL", "qwen3:8b")
tools = [{
"type": "function",
"function": {
"name": "add",
"description": "计算两个数的和",
"parameters": {
"type": "object",
"properties": {"a": {"type": "number"}, "b": {"type": "number"}},
"required": ["a", "b"],
},
},
}]
resp = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": "3829 加 9471 等于多少?"}],
tools=tools,
)
msg = resp.choices[0].message
print("finish_reason:", resp.choices[0].finish_reason)
print("content:", msg.content)
for call in msg.tool_calls or []:
print("tool_call:", call.function.name, call.function.arguments)Run it:
python step1_call.pyYou should see (on my local qwen3:8b, 3.7 seconds):
finish_reason: tool_calls
content:
tool_call: add {"a":3829,"b":9471}Understand These Three Lines
finish_reason: tool_calls: The model stopped at this turn because it wants to call a tool, not because it finished speaking.contentis empty: It did not directly answer 13300, but instead handed over the "slip."tool_call: add {"a":3829,"b":9471}: It chose theaddfunction, with the arguments being a JSON string. Note thatargumentsis a string, so you need tojson.loadsit before use.
We ran the same code with the cloud-based GLM model, and the output was the same, except the JSON had extra spaces: add {"a": 3829, "b": 9471}.
How to Write the Function Description
Each item in tools tells the model about one function:
| Field | Purpose |
|---|---|
name | The function name. The model uses this name when calling |
description | What this function does. The model relies on it to judge when to use it |
parameters | The JSON Schema of the parameters: which parameters exist, their types, and which are required |
How well description is written directly determines whether the model will use it at the right time. Writing "calculates the sum of two numbers" is far better than writing "tool 1".
- 1?
- 2?
- 3?
- 4?
Click them one by one, in order.
Quiz timeWhen the model returns tool_calls, has the function been executed?
Check Yourself
- I got
finish_reason: tool_callsand atool_call - I can explain what
name,description, andparameterseach do - I know
argumentsis a string and that the model does not execute functions itself
Next section: Write the Agent Loop.