GLM 的联网搜索通过 tools.type=web_search 触发,但它不是只写一个 type 就能工作的最小 OpenAI function tool。
正确请求形状
推荐在强时效任务中显式传入 search_query:
curl -X POST "https://models.kapon.cloud/v1/chat/completions" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-4.7",
"messages": [
{
"role": "user",
"content": "请只根据联网搜索结果回答当前北京时间日期,格式 YYYY-MM-DD,并列出搜索结果来源。"
}
],
"tools": [
{
"type": "web_search",
"web_search": {
"enable": true,
"search_engine": "search_pro_jina",
"search_result": true,
"search_query": "北京时间 今天 日期 timeanddate Beijing current date",
"count": 3
}
}
],
"max_tokens": 900
}'
常用字段
| 字段 | 含义 | 建议 |
|---|
enable | 是否启用联网搜索 | 固定传 true |
search_engine | 搜索引擎标识 | 推荐 search_pro_jina |
search_result | 是否返回搜索证据 | 建议传 true |
search_query | 显式搜索词 | 对日期、天气、新闻等强时效问题建议显式传入 |
count | 搜索结果条数 | 常用 3 到 5 |
响应字段
当上游返回搜索证据时,除了 choices[0].message.content,响应顶层还会出现 web_search:
{
"choices": [
{
"message": {
"content": "2026-06-03\n来源:[1], [2], [3]"
}
}
],
"web_search": [
{
"title": "Current Local Time in Beijing, Beijing Municipality, China",
"link": "https://www.timeanddate.com/worldclock/china/beijing",
"refer": "ref_1"
}
]
}
不合法请求
下面这种最小写法是不合法的:
{
"tools": [
{ "type": "web_search" }
]
}
会返回类似错误:
{
"error": {
"code": "invalid_tool",
"message": "web_search tool requires web_search object"
}
}
实战建议
普通问答
如果只是泛化问题,例如“总结一下最近的 AI 行业趋势”,可以只传 enable: true,由模型自主决定搜索。
强时效问题
如果问题依赖今天日期、天气、最近新闻、近期公告等信息,建议:
- 传
search_result: true
- 显式传
search_query
- 读取顶层
web_search,而不只看模型自然语言答案
如果你只让模型“自主搜索”,而不传显式 search_query,在强时效场景下结果可能不够稳定。