gRPC API
批处理管理
xai_api.BatchMgmt
用于异步处理批量请求的 API 服务,其优先级低于 Chat 服务。
方法:xai_api.BatchMgmt
CreateBatchunary创建新批次。
GetBatchunary获取单个批次。
ListBatchesunary获取团队拥有的所有批次列表。
CancelBatchunary停止处理批次中的所有未完成请求。
AddBatchRequestsunary向批次添加请求。
ListBatchRequestMetadataunary列出批次中各请求的元数据。
ListBatchResultsunary列出批次的处理结果。
GetBatchRequestResultunary获取批次中的单个请求。
创建批次并添加请求
import os
import xai_sdk
from xai_sdk.chat import user
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
# Create a batch
batch = client.batch.create("my_batch")
print(f"Batch ID: {batch.batch_id}")
# Add chat requests to the batch
chats = []
for country in ["UK", "USA", "Egypt"]:
chat = client.chat.create(
model="grok-4.7",
batch_request_id=f"capital_{country.lower()}",
)
chat.append(user(f"What is the capital of {country}?"))
chats.append(chat)
client.batch.add(batch.batch_id, chats)
列出批次并获取结果
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
# List all batches
batches = client.batch.list(limit=10)
for b in batches.batches:
print(f"{b.name}: {b.batch_id}")
# Get results for a specific batch
results = client.batch.list_batch_results("BATCH_ID")
for r in results.results:
print(f"{r.batch_request_id}: {r.response}")
最后更新:2026 年 9 月 2 日