跳转到内容

工具

概述

xAI API 支持工具调用,使 Grok 能够执行超出文本生成的操作——例如搜索网络、执行代码、查询您的数据或调用您自己的自定义函数。工具扩展了 API 的可能性,让您能够构建强大、交互式的应用程序。

工具类型

xAI API 提供两种类别的工具:

类型描述示例
内置工具由 xAI 管理的服务器端工具,自动执行网络搜索、X 搜索、代码解释器、集合搜索
函数调用您定义的自定义函数,模型可以调用数据库查询、API 调用、自定义业务逻辑

内置工具在 xAI 的服务器上运行——您提供工具配置,API 处理执行并返回结果。函数调用让您定义自己的工具,模型可以请求这些工具,让您完全控制调用时发生的事情。

定价

工具请求根据两个组件定价:令牌使用工具调用。由于模型可能调用多个工具来回答查询,成本会随复杂度增加。

有关工具定价的更多详细信息,请查看定价页面

工作原理

当您向请求提供工具时,xAI API 可以使用它们来收集信息或执行操作:

  1. 分析查询并确定需要什么信息或操作
  2. 决定下一步做什么:进行工具调用,或提供最终答案
  3. 执行工具(对于内置工具)或返回工具调用请求(对于函数调用)
  4. 处理结果并继续,直到收集到足够的信息
  5. 返回最终响应,并在适用时添加引用

快速入门

bash
curl https://api.x.ai/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-4.5",
  "stream": true,
  "input": [
    {
      "role": "user",
      "content": "What are the latest updates from xAI?"
    }
  ],
  "tools": [
    { "type": "web_search" },
    { "type": "x_search" },
    { "type": "code_interpreter" }
  ]
}'
python
import os

from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import web_search, x_search, code_execution

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.5",
    tools=[
        web_search(),
        x_search(),
        code_execution(),
    ],
)

chat.append(user("What are the latest updates from xAI?"))

for response, chunk in chat.stream():
    if chunk.content:
        print(chunk.content, end="", flush=True)

print("\nCitations:", response.citations)
python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
)

response = client.responses.create(
    model="grok-4.5",
    input=[
        {"role": "user", "content": "What are the latest updates from xAI?"}
    ],
    tools=[
        {"type": "web_search"},
        {"type": "x_search"},
        {"type": "code_interpreter"},
    ],
    stream=True,
)

for event in response:
    if event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)
javascript
import { xai } from '@ai-sdk/xai';
import { streamText } from 'ai';

const { fullStream } = streamText({
  model: xai.responses('grok-4.5'),
  prompt: 'What are the latest updates from xAI?',
  tools: {
    web_search: xai.tools.webSearch(),
    x_search: xai.tools.xSearch(),
    code_execution: xai.tools.codeExecution(),
  },
});

for await (const part of fullStream) {
  if (part.type === 'text-delta') {
    process.stdout.write(part.text);
  } else if (part.type === 'source' && part.sourceType === 'url') {
    console.log(`Citation: ${part.url}`);
  }
}
javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://api.x.ai/v1",
});

const stream = await client.responses.create({
  model: "grok-4.5",
  input: [
    { role: "user", content: "What are the latest updates from xAI?" }
  ],
  tools: [
    { type: "web_search" },
    { type: "x_search" },
    { type: "code_interpreter" },
  ],
  stream: true,
});

for await (const event of stream) {
  if (event.type === "response.output_text.delta") {
    process.stdout.write(event.delta);
  }
}

引用

API 自动返回通过工具收集的信息的源 URL。有关访问和使用引用数据的详细信息,请参阅引用

下一步

本文档为 docs.x.ai 全站中文翻译,由 AI 自动翻译生成。代码示例请以原文为准。