联网搜索接口
通过 GPT Responses 与 Gemini Chat Completions 调用联网搜索能力
联网搜索接口用于处理需要实时信息的问题,例如天气、新闻、价格、政策、赛事结果等。调用时在请求中声明搜索工具,模型会根据问题自动发起搜索并基于搜索结果生成回答。
将示例中的 YOUR_API_KEY 替换为您自己的 API Key。联网搜索适合实时问题;如果只是普通对话,可以不传 tools 参数。
GPT Responses 联网搜索
GPT 模型推荐使用 Responses 接口,并在 tools 中传入 web_search_preview。
URL
https://api.xty.app/v1/responses
方法
POST
模型示例
gpt-5.4
请求示例
curl https://api.xty.app/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-5.4",
"input": "查询一下今天北京的天气,输出日期",
"tools": [
{
"type": "web_search_preview"
}
]
}'请求体
{
"model": "gpt-5.4",
"input": "查询一下今天北京的天气,输出日期",
"tools": [
{
"type": "web_search_preview"
}
]
}响应示例
{
"id": "resp_0f1ef653f3f89cfb0169e0733f2cc881909ac2ea9d0dd9783a",
"object": "response",
"created_at": 1776317247,
"status": "completed",
"model": "gpt-5.4",
"output": [
{
"id": "ws_0f1ef653f3f89cfb0169e0733fd3a0819082a08a3ab643d1a6",
"type": "web_search_call",
"status": "completed",
"action": {
"type": "search",
"queries": [
"weather: China, Beijing, Beijing"
],
"query": "weather: China, Beijing, Beijing"
}
},
{
"id": "msg_0f1ef653f3f89cfb0169e07340cb4881909c74f11676aebc5c",
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"annotations": [],
"logprobs": [],
"text": "今天北京天气:小雨,22°C。\n日期:4月16日,星期四。"
}
],
"phase": "final_answer",
"role": "assistant"
}
],
"tool_choice": "auto",
"tools": [
{
"type": "web_search",
"search_context_size": "medium",
"user_location": {
"type": "approximate",
"country": "US"
}
}
],
"usage": {
"input_tokens": 3394,
"output_tokens": 52,
"total_tokens": 3446
}
}读取 GPT 输出
Responses 接口的最终文本通常位于:
output[].content[].text其中 output 中可能同时包含 web_search_call 和 message。实际业务中可以筛选 type 为 message 的对象,再读取其 content 中 type 为 output_text 的文本。
Gemini Chat Completions 联网搜索
Gemini 模型可通过 Chat Completions 接口调用联网搜索能力。调用时在 tools 中声明 googleSearch 函数。
URL
https://api.xty.app/v1/chat/completions
方法
POST
模型示例
gemini-3-flash-preview
请求示例
curl https://api.xty.app/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gemini-3-flash-preview",
"messages": [
{
"role": "user",
"content": "查询一下今天北京的天气,输出日期"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "googleSearch",
"description": "Use Google Search to ground the answer with current information.",
"parameters": {
"type": "object",
"properties": {}
}
}
}
]
}'请求体
{
"model": "gemini-3-flash-preview",
"messages": [
{
"role": "user",
"content": "查询一下今天北京的天气,输出日期"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "googleSearch",
"description": "Use Google Search to ground the answer with current information.",
"parameters": {
"type": "object",
"properties": {}
}
}
}
]
}响应示例
{
"id": "chatcmpl-202604160524033486934008268d9d6Njum3JXv",
"model": "gemini-3-flash-preview",
"object": "chat.completion",
"created": 1776317050,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "今天是 **2026年4月16日**,星期四。\n\n北京今天的天气情况如下:\n\n* **天气状况**:今天北京有一次明显的降雨降温过程。白天阴有小雨,夜间阴,大部分地区有阵雨转多云。\n* **气温**:最高气温约 **18℃ ~ 20℃**,最低气温约 **12℃**。\n* **风力**:白天北转南风2、3级,夜间南转北风1、2级。"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 60,
"completion_tokens": 562,
"total_tokens": 622
}
}读取 Gemini 输出
Chat Completions 接口的最终文本位于:
choices[0].message.content注意事项
- GPT Responses 示例使用
https://api.xty.app/v1/responses。 - Gemini Chat Completions 示例使用
https://api.xty.app/v1/chat/completions。 - 联网搜索结果会随时间变化,同一个问题在不同日期可能返回不同内容。
- 如果接口返回中包含工具调用记录,最终回答仍以
message文本为准。