2026-11-19 · Prompt Tutorials
Tool calling (Function Calling) is the key step that turns GLM from a "chat box" into an assistant that gets things done: you describe a tool's name, purpose, and parameter format, the model decides when to call it and outputs the arguments in the right shape, you execute the call, feed the result back, and the model composes the final answer. The GLM model family handles this well, digesting task descriptions in both Chinese and English with stable parameter formatting. This guide walks you from Playground to API in about 30 minutes.
Head over to the playground linked from glmmodel.net (Zhipu's official experience space) — no code needed to get a feel for tool calling. Describe a tool in the system prompt, ask a question that requires it, and watch whether the model emits structured JSON.
Paste this straight into the system prompt box:
You are an assistant with access to the following tool:
Tool name: get_weather
Purpose: Look up current weather for a given city
Parameters:
- city (string, required): city name, Chinese or English supported
- unit (string, optional): temperature unit, celsius or fahrenheit, default celsius
Rules:
1. Only call the tool when the user asks about weather; skip it for small talk.
2. When calling, output JSON: {"tool": "get_weather", "args": {"city": "...", "unit": "..."}}
3. If the user doesn't mention a city, ask a follow-up question instead of guessing.
Then ask "What's the temperature in Beijing today?". If the model outputs JSON with a city argument instead of making up weather, it has understood "this is a tool moment." The value of this step: you can verify your task description is clear enough before writing any code.
The playground only covers half the flow — the real loop is: model returns a call request → your code runs the tool → you pass the result back → the model produces the answer. After creating an API key on the BigModel platform, define tools via the SDK's tools parameter (see official docs for exact fields). The core loop looks like this:
import json
from zhipuai import ZhipuAiClient # use the actual class name from the official SDK
client = ZhipuAiClient(api_key="YOUR_KEY")
def get_weather(city, unit="celsius"):
# swap in your real data source; this example returns a fixed value
return {"city": city, "temp": 22, "condition": "cloudy", "unit": unit}
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Look up current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
}]
messages = [{"role": "user", "content": "Is today good for a run in Shanghai?"}]
resp = client.chat.completions.create(model="glm-5", messages=messages, tools=tools)
# when the model requests a tool call, execute it and pass the result back
tool_call = resp.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
result = get_weather(**args)
messages.append(resp.choices[0].message)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result, ensure_ascii=False)
})
final = client.chat.completions.create(model="glm-5", messages=messages, tools=tools)
print(final.choices[0].message.content)
Run it and you'll see the full loop: the model first reasons that "a run depends on the weather," issues a call request, then after receiving 22°C and cloudy skies, delivers a verdict like "great for a morning run, watch the sun."
Tool calling is 70% about the description. Three practical habits:
1. Write "when to use it," not just "what it is." "Call this when the user asks about future dates or needs date differences" beats a bare "date tool" by a wide margin. 2. Annotate parameters with constraints. Spell out type, meaning, and defaults for each parameter; use enums wherever possible and the model will guess far less. 3. Give the model an escape hatch. State explicitly in the system prompt: "If the tool returns nothing, say so honestly" — this prevents fabricated results.
A bonus for multilingual use cases: GLM is strong in both Chinese and English, so a user asking in English while the tool returns Chinese data flows naturally — no translation layer needed.
From here, the natural next step is agents: hook up multiple tools, add a "plan first, then execute" system prompt, and you have a minimal working agent. Coding Plan subscribers building coding agents will find that tool calling plus GLM's code capabilities work especially well together. Once this one example runs, wiring up databases, calendars, or search is just copy-pasting the same pattern.
本文为使用方法整理,内容选自网上公开资料,供参考;本站为非官方资讯站,产品能力以官方页面为准。