gRPC API

聊天

查看 Markdown

xai_api.Chat

通过 Chat 接口提供语言模型能力的 API 服务。

方法:xai_api.Chat

GetCompletionunary

从模型采样响应,并阻塞等待响应完全生成。

GetCompletionChunkserver streaming

从模型采样响应,并在模型生成 token 时持续流式输出。

StartDeferredCompletionunary

开始模型采样并立即返回包含 request ID 的响应。可以使用该 request ID 轮询 `GetDeferredCompletion` RPC。

GetDeferredCompletionunary

获取通过 `StartDeferredCompletion` 启动的延迟补全结果。

GetStoredCompletionunary

使用响应 ID 检索已存储的响应。

DeleteStoredCompletionunary

使用响应 ID 删除已存储的响应。

发送聊天补全

import os
import xai_sdk
from xai_sdk.chat import user

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(model="grok-4.7")
chat.append(user("What is the meaning of life?"))

response = chat.sample()
print(response.content)

流式聊天

import os
import xai_sdk
from xai_sdk.chat import user

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

chat = client.chat.create(model="grok-4.7")
chat.append(user("Tell me a short joke"))

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

压缩长对话

将较长对话压缩为一个加密的 compaction blob,可追加到后续聊天中。完整指南请参阅 上下文压缩

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

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

# Option A: compact a Chat in place. Prior messages are replaced with the
# encrypted compaction blob; chat.sample() continues to work transparently.
chat = client.chat.create(model="grok-4.7", use_encrypted_content=True)
chat.append(system("You are a concise and knowledgeable science tutor."))
chat.append(user("What is the Higgs boson and why is it important?"))
chat.append(chat.sample())
# ... many more turns ...

compact = chat.compact()
print(f"Dropped {compact.dropped_message_count} messages, "
      f"summary used {compact.usage.total_tokens} tokens")

# Option B: compact a standalone message list with client.chat.compact_context().
messages = [
    system("You are a concise and knowledgeable science tutor."),
    user("What is the Higgs boson and why is it important?"),
    assistant("The Higgs boson is an elementary particle..."),
]
compact = client.chat.compact_context(model="grok-4.7", messages=messages)

# Hand the compaction to a fresh chat; appending replaces existing messages
# with the encrypted blob.
new_chat = client.chat.create(model="grok-4.7", use_encrypted_content=True)
new_chat.append(compact)
new_chat.append(user("What gives particles their mass?"))
print(new_chat.sample().content)

最后更新:2026 年 9 月 2 日