模型能力

文本生成

查看 Markdown

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

响应会存储 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 os
from xai_sdk import Client
from xai_sdk.chat import user, system

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

chat = client.chat.create(model="grok-4.7")
chat.append(system("You are Grok, an AI agent built to answer helpful questions."))
chat.append(user("How big is the universe?"))
response = chat.sample()

print(response)

# The response ID that can be used to continue the conversation later

print(response.id)

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

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

import os
from xai_sdk import Client
from xai_sdk.chat import user, system

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

chat = client.chat.create(model="grok-4.7", store_messages=False)
chat.append(system("You are Grok, an AI agent built to answer helpful questions."))
chat.append(user("How big is the universe?"))
response = chat.sample()

print(response)

返回加密推理内容

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

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

chat = client.chat.create(model="grok-4.7",
        use_encrypted_content=True)

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


串联对话

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

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

import os
from xai_sdk import Client
from xai_sdk.chat import user, system

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

chat = client.chat.create(model="grok-4.7", store_messages=True)
chat.append(system("You are Grok, an AI agent built to answer helpful questions."))
chat.append(user("How big is the universe?"))
response = chat.sample()

print(response)

# The response ID that can be used to continue the conversation later

print(response.id)

# New steps

chat = client.chat.create(
    model="grok-4.7",
    previous_response_id=response.id,
    store_messages=True,
)
chat.append(user("How do stars form?"))
second_response = chat.sample()

print(second_response)

# The response ID that can be used to continue the conversation later

print(second_response.id)

添加加密推理内容

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

import os
from xai_sdk import Client
from xai_sdk.chat import user, system

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

chat = client.chat.create(model="grok-4.7", store_messages=True, use_encrypted_content=True)
chat.append(system("You are Grok, an AI agent built to answer helpful questions."))
chat.append(user("How big is the universe?"))
response = chat.sample()

print(response)

# The response ID that can be used to continue the conversation later

print(response.id)

# New steps

chat.append(response)  ## Append the response and the SDK will automatically add the outputs from response to message history

chat.append(user("How do stars form?"))
second_response = chat.sample()

print(second_response)

# The response ID that can be used to continue the conversation later

print(second_response.id)

检索之前的模型响应

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

import os
from xai_sdk import Client
from xai_sdk.chat import user, system

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

response = client.chat.get_stored_completion("<The previous response's id>")

print(response)

删除模型响应

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

import os
from xai_sdk import Client
from xai_sdk.chat import user, system

client = Client(
    api_key=os.getenv("XAI_API_KEY"),
    management_api_key=os.getenv("XAI_MANAGEMENT_API_KEY"),
    timeout=3600,
)

response = client.chat.delete_stored_completion("<The previous response's id>")
print(response)

最后更新:2026 年 9 月 21 日