工具调用 (Web Search)
联网搜索工具,支持实时网络信息检索。
API 端点
POST
/chat/completions使用工具的对话
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 必填 | 支持工具调用的模型 |
messages | array | 必填 | 对话消息 |
tools | array | 必填 | 工具定义列表,包含 web_search |
请求示例
请求示例
{
"model": "GLM-4-Air",
"messages": [
{
"role": "user",
"content": "请搜索一下今天的新闻"
}
],
"tools": [
{
"type": "web_search",
"web_search": {
"enable": true,
"search_query": "今天的新闻"
}
}
]
}响应示例
响应示例
{
"id": "chatcmpl-789",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "根据搜索结果,今天的主要新闻包括...",
"tool_calls": [
{
"id": "call_123",
"type": "web_search",
"web_search": {
"search_results": [...]
}
}
]
},
"finish_reason": "stop"
}
]
}代码示例
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://your-proxy-domain.com/v1"
)
response = client.chat.completions.create(
model="GLM-4-Air",
messages=[
{"role": "user", "content": "请搜索一下今天的新闻"}
],
tools=[
{
"type": "web_search",
"web_search": {
"enable": True,
"search_query": "今天的新闻"
}
}
]
)
print(response.choices[0].message.content)