Websuche (Tools)
Websuche-Tool mit Unterstützung für Echtzeit-Webinformationsabruf.
API-Endpunkte
POST
/chat/completionsDialog mit Tools
Anfrageparameter
| Parameter | Typ | Erforderlich | Beschreibung |
|---|---|---|---|
model | string | Erforderlich | Modell mit Tool-Calling-Unterstützung |
messages | array | Erforderlich | Dialognachrichten |
tools | array | Erforderlich | Tool-Definitionsliste, einschließlich web_search |
Anfrage-Beispiel
Anfrage-Beispiel
{
"model": "GLM-4-Air",
"messages": [
{
"role": "user",
"content": "请搜索一下今天的新闻"
}
],
"tools": [
{
"type": "web_search",
"web_search": {
"enable": true,
"search_query": "今天的新闻"
}
}
]
}Antwort-Beispiel
Antwort-Beispiel
{
"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"
}
]
}Code-Beispiele
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": "今天的新闻"
}
}
]
}'