模型能力
文本生成
Responses API 是通过 API 与模型交互的首选方式。它支持与模型进行可选的有状态交互,在这种交互中,之前的输入提示词、推理内容和模型响应会保存并存储在 xAI 的服务器上。你可以追加新的 prompt 消息来继续交互,无需重新发送完整对话。此行为默认启用。如果希望在本地存储 request/response,请参阅禁止在服务器上存储之前的 request/response。
响应会存储 30 天,之后将被删除。这意味着发送请求后的 30 天内,你可以使用 response ID 检索或继续对话。如果希望在 30 天后继续对话,请在本地存储响应历史记录和加密思考内容,并在新的 request body 中传入它们。
对于 Python,我们还提供xAI SDK,它覆盖我们的全部功能,并使用 gRPC 实现最佳性能。两者可以混合使用。xAI SDK 允许你与 Collections、Voice API、API Key 管理等所有产品交互,而 Responses API 更适合 chatbot 和 RESTful API 场景。
前置条件
在xAI Console API Keys 页面创建 API Key,并在环境中设置该 API Key:
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.5")
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)禁止在服务器上存储之前的 request/response
如果不希望在服务器上存储之前的 request/response,可以在请求中设置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.5", 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 request message 中指定use_encrypted_content=True,或者在 request body 中指定include: ["reasoning.encrypted_content"]。
按如下方式修改创建 chat client(xAI SDK)的步骤或更改 request body:
chat = client.chat.create(model="grok-4.5",
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.5", 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.5",
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)添加加密思考内容
返回加密思考内容后,还可以将其添加到新响应的 input 中。
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.5", 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)