工具
X Search
X Search tool 使 Grok 能够在 X(原 Twitter)上执行 keyword search、semantic search、user search 和 thread fetch。该 tool 允许 model 访问实时社交媒体内容、分析帖子并从 X 的海量数据中获取洞察。
SDK 支持
| SDK/API | Tool 名称 |
|---|---|
| xAI SDK | x_search |
| OpenAI Responses API | x_search |
| Vercel AI SDK | xai.tools.xSearch() |
所有与 Responses API 兼容的 SDK 也支持该 tool。
基础用法
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import x_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.5", # reasoning model
tools=[x_search()],
include=["verbose_streaming"],
)
chat.append(user("What are people saying about xAI on X?"))
is_thinking = True
for response, chunk in chat.stream():
for tool_call in chunk.tool_calls:
print(f"\\nCalling tool: {tool_call.function.name} with arguments: {tool_call.function.arguments}")
if response.usage.reasoning_tokens and is_thinking:
print(f"\\rThinking... ({response.usage.reasoning_tokens} tokens)", end="", flush=True)
if chunk.content and is_thinking:
print("\\n\\nFinal Response:")
is_thinking = False
if chunk.content and not is_thinking:
print(chunk.content, end="", flush=True)
print("\\n\\nCitations:")
print(response.citations)X Search 参数
| 参数 | 说明 |
|---|---|
allowed_x_handles | 仅考虑来自指定 X handle 的帖子(最多 20 个) |
excluded_x_handles | 排除来自指定 X handle 的帖子(最多 20 个) |
from_date | 搜索范围开始日期(ISO8601 格式) |
to_date | 搜索范围结束日期(ISO8601 格式) |
enable_image_understanding | 启用对帖子中图像的分析 |
enable_video_understanding | 启用对帖子中视频的分析 |
仅考虑指定 Handle 的帖子
使用 allowed_x_handles,仅考虑来自给定 X handle 列表的帖子。最多可包含 20 个 handle。
import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import x_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.5",
tools=[
x_search(allowed_x_handles=["elonmusk"]),
],
)
chat.append(user("What is the current status of xAI?"))
# stream or sample the response...排除指定 Handle 的帖子
使用 excluded_x_handles,防止 model 在任何 X search tool invocation 中包含来自指定 handle 的帖子。最多可排除 20 个 handle。
chat = client.chat.create(
model="grok-4.5",
tools=[
x_search(excluded_x_handles=["elonmusk"]),
],
)日期范围
可以通过指定 from_date 和 to_date 限制搜索数据的日期范围。数据会被限制在从 from_date 到 to_date 的时间段内,包含两个日期。
两个字段都必须使用 ISO8601 格式,例如 "YYYY-MM-DD"。如果使用 xAI Python SDK,from_date 和 to_date 字段可以通过 datetime.datetime object 传入。
import os
from datetime import datetime
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import x_search
client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
model="grok-4.5",
tools=[
x_search(
from_date=datetime(2025, 10, 1),
to_date=datetime(2025, 10, 10),
),
],
)
chat.append(user("What is the current status of xAI?"))
# stream or sample the response...启用 Image Understanding
将 enable_image_understanding 设置为 true,允许 Agent 分析搜索过程中遇到的 X 帖子中的图像。
chat = client.chat.create(
model="grok-4.5",
tools=[
x_search(enable_image_understanding=True),
],
)启用 Video Understanding
将 enable_video_understanding 设置为 true,允许 Agent 分析 X 帖子中的视频。该功能仅适用于 X Search,不适用于 Web Search。
chat = client.chat.create(
model="grok-4.5",
tools=[
x_search(enable_video_understanding=True),
],
)Citation
有关如何获取和使用搜索结果 citation 的详情,请参阅 Citation 页面。