高级 API 用法
异步请求
使用 xAI API 时,你可能需要处理数百甚至数千个请求。按顺序逐个发送这些请求可能非常耗时。
为提高效率,你可以使用 AsyncClient(来自 xai_sdk)或 AsyncOpenAI(来自 openai,从而并发发送多个请求。下面的 Python 脚本演示如何使用 AsyncClient批量异步处理请求,显著缩短总执行时间:
速率限制
调整 max_concurrent 参数,控制并行请求的最大数量。
并发运行的请求数量不得超过 API Console 中显示的速率限制。
import asyncio
import os
import httpx
from asyncio import Semaphore
from openai import AsyncOpenAI
client = AsyncOpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
timeout=httpx.Timeout(3600.0) # Override default timeout with longer timeout for reasoning models
)
async def send_request(sem: Semaphore, request: str) -> dict:
"""Send a single request to SpaceXSI with semaphore control."""
# The 'async with sem' ensures only a limited number of requests run at once
async with sem:
return await client.chat.completions.create(
model="grok-4.7",
messages=[{"role": "user", "content": request}]
)
async def process_requests(requests: list[str], max_concurrent: int = 2) -> list[dict]:
"""Process multiple requests with controlled concurrency."""
# Create a semaphore that limits how many requests can run at the same time # Think of it like having only 2 "passes" to make requests simultaneously
sem = Semaphore(max_concurrent)
# Create a list of tasks (requests) that will run using the semaphore
tasks = [send_request(sem, request) for request in requests]
# asyncio.gather runs all tasks in parallel but respects the semaphore limit
# It waits for all tasks to complete and returns their results
return await asyncio.gather(*tasks)
async def main() -> None:
"""Main function to handle requests and display responses."""
requests = [
"Tell me a joke",
"Write a funny haiku",
"Generate a funny X post",
"Say something unhinged"
]
# This starts processing all asynchronously, but only 2 at a time
# Instead of waiting for each request to finish before starting the next,
# we can have 2 requests running at once, making it faster overall
responses = await process_requests(requests)
# Print each response in order
for i, response in enumerate(responses):
print(f"# Response {i}:")
print(response.choices[0].message.content)
if __name__ == "__main__":
asyncio.run(main())最后更新:2026 年 10 月 10 日