Skip to content

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:

python
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:

bash
python step1_call.py

You should see (on my local qwen3:8b, 3.7 seconds):

text
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.
  • content is empty: It did not directly answer 13300, but instead handed over the "slip."
  • tool_call: add {"a":3829,"b":9471}: It chose the add function, with the arguments being a JSON string. Note that arguments is a string, so you need to json.loads it 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:

FieldPurpose
nameThe function name. The model uses this name when calling
descriptionWhat this function does. The model relies on it to judge when to use it
parametersThe 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".

Line them upWhat happens in a single function call — click in order
  1. 1?
  2. 2?
  3. 3?
  4. 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_calls and a tool_call
  • I can explain what name, description, and parameters each do
  • I know arguments is a string and that the model does not execute functions itself

Next section: Write the Agent Loop.

gaodong.aiAboutPrivacyContact

Independent tutorials, not affiliated with any product mentioned. Product names and trademarks belong to their owners. Check official pages for prices and policies.