网淘吧来吧,欢迎您!

xAI Grok Search技能使用说明

2026-03-29 新闻来源:网淘吧 围观:9
电脑广告
手机广告

xAI Grok 搜索

使用 xAI 的 Grok API 搜索网络和 X(Twitter),具备实时互联网访问、引用来源以及可选的图像/视频理解功能。

何时使用此技能

使用网络搜索来获取:

  • 来自网站、新闻文章、文档的当前信息
  • 实时数据(股票价格、天气、近期事件)
  • 基于最新网络来源的研究主题
  • 从特定网站/域名查找信息
  • 核实当前事实

使用 X 搜索来获取:

  • 人们在 X/Twitter 上对某个话题的讨论
  • 热门讨论和社交情绪
  • 对事件的实时反应
  • 来自特定 X 账号/用户的帖子
  • 特定日期范围内的近期社交媒体活动

请勿用于:

xAI Grok Search

  • 不会改变的历史事实
  • 已有的一般性知识
  • 数学计算
  • 代码生成
  • 创意写作

设置

所需环境变量

export XAI_API_KEY="your-xai-api-key-here"

从以下位置获取您的API密钥:https://console.x.ai/

使用

代理将根据用户查询自动选择正确的工具:

用户:"关于AI监管的最新消息是什么?" → 使用网络搜索

用户:"人们在X上对OpenAI有什么看法?" → 使用X搜索

API参考

函数:search_web

使用xAI的Grok API进行网络搜索。

参数:

  • 查询(必需):搜索查询字符串
  • 模型(可选):使用的模型(默认:"grok-4-1-fast-reasoning")
  • 允许的域名(可选):用于限制搜索范围的域名数组(最多5个)
  • excluded_domains(可选):需要排除的域名数组(最多5个)
  • enable_image_understanding(可选):启用图像分析功能(默认值:false)

返回内容:

  • content:搜索响应的文本内容
  • citations:来源信息数组,包含URL、标题和摘要片段
  • usage:令牌使用统计信息

函数:search_x

使用xAI的Grok API搜索X(原Twitter)平台

参数:

  • query(必需):搜索查询字符串
  • model(可选):使用的模型(默认:"grok-4-1-fast-reasoning")
  • allowed_x_handles(可选):要搜索的X账号数组(最多10个,不带@符号)
  • excluded_x_handles(可选): 要排除的X账号数组(最多10个,不带@)
  • from_date(可选): 起始日期,ISO8601格式 (YYYY-MM-DD)
  • to_date(可选): 结束日期,ISO8601格式 (YYYY-MM-DD)
  • enable_image_understanding(可选): 启用图片分析 (默认: false)
  • enable_video_understanding(可选): 启用视频分析 (默认: false)

返回:

  • content: 搜索响应文本
  • citations: X帖子数组,包含url、title和snippet
  • usage: 令牌使用统计

实现

此技能使用xAI Responses API (/v1/responses端点)。

网页搜索

async function search_web(options) {
  const { query, model = 'grok-4-1-fast-reasoning', 
          allowed_domains, excluded_domains, enable_image_understanding } = options;

  const tool = { type: 'web_search' };
  if (allowed_domains) tool.allowed_domains = allowed_domains;
  if (excluded_domains) tool.excluded_domains = excluded_domains;
  if (enable_image_understanding) tool.enable_image_understanding = true;

  const response = await fetch('https://api.x.ai/v1/responses', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.XAI_API_KEY}`
    },
    body: JSON.stringify({
      model,
      input: [{ role: 'user', content: query }],
      tools: [tool]
    })
  });

  const data = await response.json();
  return { 
    content: data.output[data.output.length - 1].content,
    citations: data.citations 
  };
}

X搜索

async function search_x(options) {
  const { query, model = 'grok-4-1-fast-reasoning',
          allowed_x_handles, excluded_x_handles, from_date, to_date,
          enable_image_understanding, enable_video_understanding } = options;

  const tool = { type: 'x_search' };
  if (allowed_x_handles) tool.allowed_x_handles = allowed_x_handles;
  if (excluded_x_handles) tool.excluded_x_handles = excluded_x_handles;
  if (from_date) tool.from_date = from_date;
  if (to_date) tool.to_date = to_date;
  if (enable_image_understanding) tool.enable_image_understanding = true;
  if (enable_video_understanding) tool.enable_video_understanding = true;

  const response = await fetch('https://api.x.ai/v1/responses', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${process.env.XAI_API_KEY}`
    },
    body: JSON.stringify({
      model,
      input: [{ role: 'user', content: query }],
      tools: [tool]
    })
  });

  const data = await response.json();
  return { 
    content: data.output[data.output.length - 1].content,
    citations: data.citations 
  };
}

示例

网络搜索 - 时事

const result = await search_web({ 
  query: "latest AI regulation developments" 
});

网络搜索 - 特定领域

const result = await search_web({
  query: "UN climate summit latest",
  allowed_domains: ["un.org", "gov.uk", "grokipedia.com"]
});

X 搜索 - 社交情绪

const result = await search_x({
  query: "new iPhone reactions opinions"
});

X 搜索 - 特定账号

const result = await search_x({
  query: "AI thoughts",
  allowed_x_handles: ["elonmusk", "cstanley"],
  from_date: "2025-01-01"
});

X 搜索 - 含媒体内容

const result = await search_x({
  query: "Mars landing images",
  enable_image_understanding: true,
  enable_video_understanding: true
});

最佳实践

网络搜索

  • 使用allowed_domains来限定特定域名(最多5个)
  • 使用excluded_domains来排除已知不良来源(最多5个)
  • 不能同时使用两者
  • 仅在需要时启用图像理解功能

X 搜索

  • 使用allowed_x_handles来聚焦特定账号(最多10个)
  • 使用excluded_x_handles来过滤噪音(最多10个)
  • 不能同时使用两者
  • 用户名中不要包含@符号
  • 使用ISO8601日期格式:YYYY-MM-DD
  • 媒体理解功能会增加API成本

故障排除

"未找到XAI_API_KEY"

export XAI_API_KEY="your-key-here"

速率限制

  • 实施指数退避策略
  • 缓存高频查询

结果不佳

  • 添加域名/用户名过滤器
  • 使查询更具体
  • 缩小日期范围

响应缓慢

使用推理模型(例如grok-4-1-fast-reasoning)进行搜索查询可能需要30-60秒以上才能返回结果,特别是当模型执行多次网络或X搜索时。如果搜索出现延迟,请告知用户结果仍在加载中,并请他们输入"poll"来检查已完成的响应。

API文档

免责申明
部分文章来自各大搜索引擎,如有侵权,请与我联系删除。
打赏
文章底部电脑广告
手机广告位-内容正文底部

相关文章

您是本站第326290名访客 今日有221篇新文章/评论