从写代码,到创作下一幕

探索 字节跳动 - 火山方舟 的 AI 编程与视频创作活动。

火山方舟 Agent Plan 与 Coding Plan 活动,价格 ¥9.9 起

Agent Plan & Coding Plan

一站体验多款热门模型,为 AI 编程与智能体开发提供更多选择。新用户可联系(微信: goo_lvyouyou)免费体验 9.9 agent plan。

查看套餐优惠
Seedance 2.5 视频创作活动:30 秒视频、多模态参考、局部编辑

Seedance 2.5

让创意,跃然成片。探索 30 秒视频、多模态参考与局部编辑,把脑海中的画面变成下一支作品。

探索视频创作

工具

Web Search

查看 Markdown

Web Search 工具让 Grok 能够实时搜索网络并浏览网页以查找信息。该工具允许模型搜索互联网、访问网页并提取相关信息,以最新内容回答查询。


SDK 支持

SDK/API工具名称
xAI SDKweb_search
OpenAI Responses APIweb_search
Vercel AI SDKxai.tools.webSearch()

所有与 Responses API 兼容的 SDK 也支持此工具。


基础用法

import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

const { text, sources } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: 'What is SpaceXSI?',
  tools: {
    web_search: xai.tools.webSearch(),
  },
});

console.log(text);
console.log('Citations:', sources);

Web Search 参数

参数说明
allowed_domains仅在指定域名内搜索(最多 5 个)
excluded_domains从搜索中排除指定域名(最多 5 个)
enable_image_understanding启用对浏览过程中发现图像的分析
enable_image_search启用可嵌入响应的图像搜索结果

仅在指定域名中搜索

使用 allowed_domains,使 web search 仅在指定域名范围内的网页上执行搜索和浏览。

const { text } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: 'What is SpaceXSI?',
  tools: {
    web_search: xai.tools.webSearch({
      allowedDomains: ['grokipedia.com'],
    }),
  },
});

排除指定域名

使用 excluded_domains,防止模型在任何 web search tool invocation 中包含指定域名。

response = client.responses.create(
    model="grok-4.7",
    input=[{"role": "user", "content": "What is SpaceXSI?"}],
    tools=[
        {
            "type": "web_search",
            "filters": {"excluded_domains": ["grokipedia.com"]},
        },
    ],
)

启用图像理解

将 enable_image_understanding 设置为 true,会让 Agent 能够访问 view_image tool,从而分析搜索过程中遇到的图像。

启用后,会在 SERVER_SIDE_TOOL_VIEW_IMAGE 中看到 response.server_side_tool_usage,以及其调用次数。

const { text } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: "What is included in the image in SpaceXSI's official website?",
  tools: {
    web_search: xai.tools.webSearch({
      enableImageUnderstanding: true,
    }),
  },
});

将 enable_image_search 设置为 true,可让 Grok 搜索相关图像,并以 Markdown image embed 的形式将其包含在 response 中,例如 ![alt](url)。

Vercel AI SDK 尚未公开 enableImageSearch;以下示例使用 Responses API 和 xAI Python SDK。

response = client.responses.create(
    model="grok-4.7",
    input=[
        {
            "role": "user",
            "content": "Show me images of Starship on the launch pad.",
        },
    ],
    tools=[
        {
            "type": "web_search",
            "enable_image_search": True,
        },
    ],
)

print(response)

Response 可以直接在输出文本中包含 Markdown image embed:

Output

![Why the SpaceX Starship launch pad matters](https://www.astronomy.com/wp-content/uploads/2024/09/starship-test-flight-mission-scaled.jpg)

Here are several high-quality images of SpaceX's Starship on the launch pad at Starbase in Boca Chica, Texas.

在 xAI SDK 中,成功执行的 image search 会以 response.server_side_tool_usage 的形式出现在 SERVER_SIDE_TOOL_IMAGE_SEARCH。


引用

有关如何获取和使用搜索结果引用的详情,请参阅 引用 页面。


最后更新:2026 年 10 月 10 日