ウェブ検索 (Tools)
ウェブ検索ツール。リアルタイムのウェブ情報取得をサポート。
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"
}
]
}コード例
Python
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)JavaScript
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://your-proxy-domain.com/v1'
});
async function searchWeb() {
const response = await client.chat.completions.create({
model: 'GLM-4-Air',
messages: [
{ role: 'user', content: '请搜索一下今天的新闻' }
],
tools: [
{
type: 'web_search',
web_search: {
enable: true,
search_query: '今天的新闻'
}
}
]
});
console.log(response.choices[0].message.content);
}
searchWeb();cURL
curl https://your-proxy-domain.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "GLM-4-Air",
"messages": [
{"role": "user", "content": "请搜索一下今天的新闻"}
],
"tools": [
{
"type": "web_search",
"web_search": {
"enable": true,
"search_query": "今天的新闻"
}
}
]
}'