介绍
# xAI Search (Grok API)
使用 xAI 的智能体搜索功能实时查询 X/Twitter 和网络。这利用了 Grok 的 `web_search` 和 `x_search` 工具。
**文档:** https://docs.x.ai/docs/
## 要求
- `XAI_API_KEY` 环境变量 - Python 3 + xai-sdk:`pip install xai-sdk`
## 快速使用 (curl)
### 网络搜索 ```bash curl -s https://api.x.ai/v1/chat/completions \ -H "Authorization: Bearer $XAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "grok-3-fast", "messages": [{"role": "user", "content": "YOUR QUERY HERE"}], "tools": [{"type": "function", "function": {"name": "web_search"}}] }' | jq -r '.choices[0].message.content' ```
### X/Twitter 搜索 ```bash curl -s https://api.x.ai/v1/chat/completions \ -H "Authorization: Bearer $XAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "grok-3-fast", "messages": [{"role": "user", "content": "YOUR QUERY HERE"}], "tools": [{"type": "function", "function": {"name": "x_search"}}] }' | jq -r '.choices[0].message.content' ```
### 组合搜索 (网络 + X) ```bash curl -s https://api.x.ai/v1/chat/completions \ -H "Authorization: Bearer $XAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "grok-3-fast", "messages": [{"role": "user", "content": "YOUR QUERY HERE"}], "tools": [ {"type": "function", "function": {"name": "web_search"}}, {"type": "function", "function": {"name": "x_search"}} ] }' | jq -r '.choices[0].message.content' ```
## 辅助脚本
为方便起见,请使用 `scripts/` 文件夹中的 `xai-search.py` 脚本:
```bash # Web search (adjust path to your skill location) python ~/.clawdbot/skills/xai-search/scripts/xai-search.py web "latest news about AI"
# X/Twitter search python ~/.clawdbot/skills/xai-search/scripts/xai-search.py x "what are people saying about Clawdbot"
# Both python ~/.clawdbot/skills/xai-search/scripts/xai-search.py both "current events today" ```
## 模型
- `grok-3-fast` — 快速,适合快速搜索 - `grok-4-1-fast` — 推理模型,更适合复杂查询
## X 搜索过滤器
您可以通过以下方式过滤 X 搜索: - `allowed_x_handles` / `excluded_x_handles` — 限制特定账号 - `from_date` / `to_date` — 日期范围 (ISO8601 格式) - `enable_image_understanding` — 分析帖子中的图片 - `enable_video_understanding` — 分析帖子中的视频
## 网络搜索过滤器
- `allowed_domains` / `excluded_domains` — 限制特定网站 - `enable_image_understanding` — 分析页面上的图片
## 提示
- 对于突发新闻:使用 X 搜索 - 对于事实/研究查询:使用网络搜索或两者结合 - 对于情绪/观点:使用 X 搜索 - 如果需要,模型将进行多次搜索调用(智能体行为)