从写代码,到创作下一幕

探索 字节跳动 - 火山方舟 的 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

Responses API 是通过 API 与模型交互的首选方式。它支持与模型进行可选的有状态交互,其中 先前的输入提示词、推理内容和模型响应会保存在 SpaceXSI 服务器上。你可以追加新的提示词消息来继续交互,无需重新发送完整对话。此行为默认启用。如果希望在本地存储请求/响应,请参阅 禁止在服务器上存储先前请求/响应。

响应会存储 30 天,之后将被删除。这意味着发送请求后的 30 天内,你可以使用响应 ID 检索或继续对话。如果希望在 30 天后继续对话,请在本地存储响应历史记录和加密推理内容,并在新的请求体中传入它们。

对于 Python,我们还提供xAI SDK,它覆盖我们的全部功能,并使用 gRPC 实现最佳性能。两者可以混合使用。xAI SDK 允许你与 Collections、Voice API、API key 管理等所有产品交互,而 Responses API 更适合聊天机器人和 RESTful API 场景。


前置条件

在xAI Console API Keys 页面创建 API Key,并在环境中设置该 API Key:

Bash

export XAI_API_KEY="your_api_key"

创建新的模型响应

首先创建一个响应:

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

const { text, response } = await generateText({
  model: xai.responses('grok-4.7'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});

console.log(text);

// The response ID can be used to continue the conversation
console.log(response.id);

禁止在服务器上存储先前请求/响应

如果不希望在服务器上存储之前的请求/响应,可以在请求中设置 store: false。

import os
import httpx
from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_XAI_API_KEY_HERE>",
    base_url="https://api.x.ai/v1",
    timeout=httpx.Timeout(3600.0), # Override default timeout with longer timeout for reasoning models
)

response = client.responses.create(
    model="grok-4.7",
    input=[
        {"role": "system", "content": "You are Grok, an AI agent built to answer helpful questions."},
        {"role": "user", "content": "How big is the universe?"},
    ],
    store=False
)

print(response)

返回加密推理内容

如果希望返回加密推理轨迹,需要在 xAI SDK 或 gRPC 请求消息中指定 use_encrypted_content=True,或者在请求体中指定 include: ["reasoning.encrypted_content"]。

请按如下方式修改创建聊天客户端(xAI SDK)的步骤,或更改请求体:

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

// Encrypted reasoning content is included automatically by the AI SDK
// as long as `store: false` is not set. No extra configuration is needed.
const { text, reasoning } = await generateText({
  model: xai.responses('grok-4.7'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});

console.log(text);
console.log(reasoning); // Contains encrypted reasoning content

请参阅添加加密推理内容,了解在发出新请求时如何使用返回的加密推理内容。


串联对话

现在我们已经获得第一个响应的id。使用 Chat Completions API 时,我们通常会发送一个包含之前所有消息的无状态新请求。

使用 Responses API 时,可以发送之前响应的id,以及要追加到其中的新消息。

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

// First request
const result = await generateText({
  model: xai.responses('grok-4.7'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});

console.log(result.text);

// Get the response ID from the response object
const responseId = result.response.id;

// Continue the conversation using previousResponseId
const { text: secondResponse } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: "How do stars form?",
  providerOptions: {
    xai: {
      previousResponseId: responseId,
    },
  },
});

console.log(secondResponse);

添加加密推理内容

返回加密推理内容后,还可以将其添加到新响应的输入中。

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

// First request. Encrypted reasoning content is included automatically
// by the AI SDK as long as `store: false` is not set.
const result = await generateText({
  model: xai.responses('grok-4.7'),
  system: "You are Grok, an AI agent built to answer helpful questions.",
  prompt: "How big is the universe?",
});

console.log(result.text);

// Continue the conversation using previousResponseId
// The encrypted content is automatically included when using previousResponseId
const { text: secondResponse } = await generateText({
  model: xai.responses('grok-4.7'),
  prompt: "How do stars form?",
  providerOptions: {
    xai: {
      previousResponseId: result.response.id,
    },
  },
});

console.log(secondResponse);

检索之前的模型响应

如果拥有之前响应的 ID,就可以检索该响应的内容。

// Note: The Vercel AI SDK does not provide a method to retrieve previous responses.
// Use the OpenAI SDK as shown above for this functionality.

import OpenAI from "openai";

const client = new OpenAI({
    apiKey: "<api key>",
    baseURL: "https://api.x.ai/v1",
    timeout: 360000,
});

const response = await client.responses.retrieve("<The previous response's id>");

console.log(response);

删除模型响应

如果不再希望存储之前的模型响应,可以将其删除。

// Note: The Vercel AI SDK does not provide a method to delete previous responses.
// Use the OpenAI SDK as shown above for this functionality.

import OpenAI from "openai";

const client = new OpenAI({
    apiKey: "<api key>",
    baseURL: "https://api.x.ai/v1",
    timeout: 360000,
});

const response = await client.responses.delete("<The previous response's id>");

console.log(response);

最后更新:2026 年 10 月 10 日