Python SDK
API Router 控制台 API 的官方 Python 客户端库。
为什么使用 SDK
- 类型化模型:每个响应都是 pydantic v2 模型,IDE 可自动补全字段,解析器会拒绝格式错误的数据。
- 透明认证:SDK 在创建时将 API 密钥交换为 Bearer 令牌,并在过期时自动刷新,无需手动调用
loginByApiKey。 - 同步与异步:
WltClient(同步)和AsyncWltClient(异步)提供相同的方法接口。 - 内置重试:网络调用失败或响应提示令牌过期时,自动重试。
安装
pip install wlt-platform
快速开始
from wlt import WltClient
client = WltClient(
api_key="your-api-key",
base_url="https://your-console-host",
)
# List your API keys (Secrets)
result = client.secrets.list(page_num=1, page_size=20)
print(result.page_info.list)
# Stream a chat completion
for chunk in client.model_lab.stream(
provider="dashscope",
model_name="Qwen2.5-72B-Instruct",
user_prompt="Hello!",
):
print(chunk, end="", flush=True)
client.close()
异步场景使用
AsyncWltClient,并在调用每个方法时使用await。
配置
将 base_url 设置为部署的 WLT 管理端绝对源地址,例如 https://your-console-host,该地址必须提供 /xt-console/api/...。不要包含 /msp-console 等控制台页面路径、/xt-console/api 路径或推理 /v1 前缀;SDK 会自行附加管理路径。
client = WltClient(
api_key="your-api-key", # Required. Auto-exchanged for Bearer token
base_url="https://your-console-host",
timeout=30.0, # Request timeout in seconds (default: 30)
max_retries=2, # Max retry attempts (default: 2)
)
基础 URL 解析
优先级从高到低:
| 优先级 | 来源 |
|---|---|
| 1 | 构造函数 base_url 参数,或 CLI 的 --base-url |
| 2 | WLT_BASE_URL 环境变量 |
| 3 | ~/.wlt/config.json 中的 base_url 字段,仅适用于 CLI 用户 |
| 4 | 内置默认值 |
末尾斜杠会自动移除;https://host/ 与 https://host 等效。
模块参考
| 模块 | 访问方式 | 方法数 |
|---|---|---|
| 用户 | client.user | 1 |
| 密钥 | client.secrets | 8 |
| API 密钥 | client.api_keys | 7 |
| 用量 | client.usage | 4 |
| 账单 | client.billing | 4 |
| 支付 | client.payment | 5 |
| 模型广场 | client.model_gallery | 3 |
| 模型实验室 | client.model_lab | 3 |
管理员范围
独立的 AdminClient 处理管理员范围操作,包括账单报告、终端客户增删改查、模型定价、额度、速率限制和上下文记录。它通过管理员用户名和密码认证,无需验证码或 API 密钥,并签发仅适用于管理员端点的会话令牌。调用方需要以管理员身份操作时,可与 WltClient 一起创建此客户端。
from wlt import AdminClient
admin = AdminClient(
username="admin",
password="your-admin-password",
base_url="https://your-console-host",
)
info = admin.auth.info() # admin identity
totals = admin.billing.customer_total( # billing rollup
start_time="2026-06-01 00:00:00",
end_time="2026-06-30 23:59:59",
)
customers = admin.customer.list(page_num=1, page_size=20) # customer CRUD
| 模块 | 访问方式 | 方法数 |
|---|---|---|
| 认证 | admin.auth | 1 |
| 账单 | admin.billing | 6 |
| 客户 | admin.customer | 3 |
| 定价 | admin.pricing | 9 |
| 额度 | admin.credits | 2 |
| 速率限制 | admin.rate_limit | 2 |
| 上下文 | admin.context | 3 |