模型实验室 — Python SDK
交互式模型体验区:查询模型、管理会话并进行流式推理。
概览
Model Lab 资源为控制台体验区提供能力:可筛选模型列表、多轮会话创建,以及输出 OpenAI 兼容数据块的流式(SSE)推理。
通过 client.model_lab 访问。
可用操作
| 方法 | 说明 |
|---|---|
list() | 获取模型实验室的模型列表 |
createSession() | 为多轮对话创建聊天会话 |
stream() | 调用模型流式推理(SSE) |
list
获取模型实验室的模型列表。
page_num 和 page_size 在类型中可选,但后端要求提供,缺少时返回 code=1000。建议默认值为 page_num=1、page_size=20。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
# List inference models
models = client.model_lab.list(usage_type="inference")
for m in models.data:
print(m.modelName, m.provider)
# Filter by provider and capability
models = client.model_lab.list(
provider="dashscope",
capability="text_to_text",
inputs=["text"],
outputs=["text"],
)
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| model_name | str | 否 | None | 模型名称(模糊搜索) |
| provider | str | 否 | None | 供应商标识符 |
| series_provider | str | 否 | None | 系列供应商标识符 |
| model_type | str | 否 | None | 模型类型 |
| view_all_flag | int | 否 | None | 查看全部标记,1=查看全部 |
| page_size | int | 否 | None | 每页条数。后端必填,缺少时返回 code=1000。建议默认值:20 |
| page_num | int | 否 | None | 页码。后端必填,缺少时返回 code=1000。建议默认值:1 |
| usage_type | str | 否 | None | 用途类型:"inference" 或 "train" |
| training_method | str | 否 | None | 训练方法:"sft" 或 "dpo" |
| tags | list[str] | 否 | None | 标签筛选列表 |
| capability | str | 否 | None | 能力筛选,例如 "text_to_text" |
| model_type_list | list[str] | 否 | None | 模型类型列表(多选) |
| origin_providers | list[str] | 否 | None | 原始供应商列表(多选) |
| inputs | list[str] | 否 | None | 输入类型列表,例如 ["text"] |
| outputs | list[str] | 否 | None | 输出类型列表,最多 1 项,例如 ["image"] |
响应
返回 BaseResponse[list[ModelInfoVO]]。
ModelInfoVO 字段见上方
client.secrets.model_list()。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
createSession
为多轮对话创建聊天会话。
使用示例
from wlt import WltClient
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
session = client.model_lab.create_session()
session_id = session.data # UUID string
print(f"Session ID: {session_id}")
参数
无。
响应
返回 BaseResponse[str],data 字段包含 UUID 字符串,即会话 ID。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
stream
调用模型流式推理(SSE),将每个 SSE 数据块作为原始 JSON 字符串逐个产出。
使用示例
import json
from wlt import WltClient
from wlt.models.model_lab import OptionsDTO
client = WltClient(api_key="your-api-key", base_url="https://console.example.com")
# Create a session for multi-turn conversation
session = client.model_lab.create_session()
# Stream inference (use a provider + model that is actually served by the
# gateway -- discover via `client.model_lab.list()` / `client.usage.gateway_providers()`.
# On the daily environment, `openrouter` + `openai/gpt-5.4-nano` is a known-good pair).
for chunk in client.model_lab.stream(
provider="openrouter",
model_name="openai/gpt-5.4-nano",
user_prompt="Explain quantum computing in one paragraph.",
session_id=session.data,
options=OptionsDTO(
temperature=0.7,
maxToken=2048,
topP=0.9,
systemPrompt="You are a helpful assistant.",
),
):
data = json.loads(chunk)
# Each chunk contains delta content, finish_reason, usage, etc.
if "content" in data:
print(data["content"], end="", flush=True)
print() # newline after streaming completes
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| provider | str | 是 | -- | 供应商标识符 |
| model_name | str | 是 | -- | 模型名称 |
| user_prompt | str | 是,重试时除外 | None | 用户提示词文本 |
| session_id | str | 否 | None | 会话 ID,用于多轮对话,使用 UUID |
| task_id | str | 否 | None | 任务 ID,用于重试,引用失败任务 |
| task_type | str | 否 | "TEXT" | 任务类型 |
| is_retry | bool | 否 | False | 是否为重试请求,需要 task_id |
| options | OptionsDTO | 否 | None | 模型参数配置,见下文 |
OptionsDTO 字段:
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| temperature | float | 否 | None | 温度,控制输出随机性 |
| maxToken | int | 否 | None | 最大生成 Token 数 |
| topP | float | 否 | None | Top P 采样参数 |
| topK | int | 否 | None | Top K 采样参数 |
| presencePenalty | float | 否 | None | 存在惩罚 |
| frequencyPenalty | float | 否 | None | 频率惩罚 |
| stopSequences | str | 否 | None | 停止序列 |
| systemPrompt | str | 否 | None | 系统提示词 |
| enableThinking | bool | 否 | None | 启用深度思考 |
| enableProgress | bool | 否 | None | 启用进度推送 |
| timeoutSeconds | int | 否 | None | 任务超时时间,单位为秒 |
| enableCache | bool | 否 | None | 启用结果缓存 |
| enableDocumentInlining | bool | 否 | None | 启用文档内联 |
| budgetTokens | int | 否 | None | 预算 Token 数 |
响应
返回生成器,逐个产出原始 JSON 字符串形式的 SSE 数据块。每个数据块遵循 OpenAI 兼容流式格式,流以 [DONE] 标记结束。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | 请求参数无效 |
2000 / 2002 | AuthenticationError | API 密钥无效 |
2007 | PermissionError | 权限不足 |
3001 | AuthenticationError | 令牌过期且刷新失败 |
3002 | RateLimitError | 超过速率限制 |