从写代码,到创作下一幕

探索 字节跳动 - 火山方舟 的 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 秒视频、多模态参考与局部编辑,把脑海中的画面变成下一支作品。

探索视频创作

工具

流式传输和同步请求

查看 Markdown

智能体请求可以使用流式或同步模式执行。本页介绍这两种方式及其有效用法。


使用智能体工具调用时,强烈建议使用流式模式。它提供:

  • 实时可观察性,可在工具调用发生时立即查看

  • 即时反馈,适用于可能耗时较长的请求

  • 推理 token 计数,并随模型思考过程更新

流式示例

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

const { fullStream } = streamText({
  model: xai.responses('grok-4.7'),
  prompt: 'What are the latest updates from SpaceXSI?',
  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 === 'tool-call') {
    console.log(\`Calling tool: \${part.toolName}\`);
  } else if (part.type === 'text-delta') {
    process.stdout.write(part.text);
  } else if (part.type === 'source' && part.sourceType === 'url') {
    console.log(\`Citation: \${part.url}\`);
  }
}

同步模式

对于较简单的使用场景,或者希望等完整的智能体工作流结束后再处理响应,可以使用同步请求:

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

// Synchronous request - waits for complete response
const { text, sources } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: 'What is the latest update from SpaceXSI?',
  tools: {
    web_search: xai.tools.webSearch(),
    x_search: xai.tools.xSearch(),
    code_execution: xai.tools.codeExecution(),
  },
});

console.log('Final Response:');
console.log(text);

console.log('\\nCitations:');
console.log(sources);

同步请求会等待整个智能体流程完成后再返回。它更适合基础场景,但对中间步骤的可见性较低。


在 Responses API 中使用工具

Responses API 同时支持流式和非流式模式:

import os
from openai import OpenAI

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

response = client.responses.create(
    model="grok-4.7",
    input=[
        {
            "role": "user",
            "content": "what is the latest update from SpaceXSI?",
        },
    ],
    tools=[
        {
            "type": "web_search",
        },
        {
            "type": "x_search",
        },
    ],
)

print(response)

访问工具输出

服务端工具调用的输出可能很大,因此默认不返回。不过,可以选择接收这些输出:

xAI SDK

工具用于 include 字段的值
"web_search""web_search_call_output"
"x_search""x_search_call_output"
"code_execution""code_execution_call_output"
"collections_search""collections_search_call_output"
"attachment_search""attachment_search_call_output"
"mcp""mcp_call_output"

Python

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import code_execution

client = Client(api_key=os.getenv("XAI_API_KEY"))
chat = client.chat.create(
    model="grok-4.7",
    tools=[
        code_execution(),
    ],
    include=["code_execution_call_output"],
)
chat.append(user("What is the 100th Fibonacci number?"))

# stream or sample the response...

Responses API

工具Responses API 工具名称用于 include 字段的值
"web_search""web_search""web_search_call.action.sources"
"code_execution""code_interpreter""code_interpreter_call.outputs"
"collections_search""file_search""file_search_call.results"
"mcp""mcp"在 Responses API 中始终返回

最后更新:2026 年 10 月 10 日