Laya Python 指南:Choice、Score 与 Noul

通过 Python 示例学习 Laya 的 choice、score、noul、Router、confidence 使用方式与生产环境实践。

最后更新: 2026/9/22

Laya 的 Python API 核心是:一个 state,加上一组 typed questions。

先安装:

pip install laya

创建 Router:

from laya import Router

router = Router(preload=True)

choice

当结果应该从指定标签中选一个时使用。

questions = {
    "department": {
        "type": "choice",
        "instructions": "Which team should handle this ticket?",
        "criteria": {
            "billing": "payments, invoices, refunds",
            "technical": "bugs, outages, errors",
            "sales": "pricing and contracts"
        }
    }
}

上游已经明确记录:默认设置下,单个 choice 中包含大量 label 时可能明显退化。

score

适合有顺序的等级判断。

questions = {
    "urgency": {
        "type": "score",
        "instructions": "How urgent is this request?",
        "criteria": [
            "not urgent",
            "needs attention soon",
            "critical or blocking"
        ]
    }
}

noul

适合判断一个命题是否成立,并返回概率。

questions = {
    "churn_risk": {
        "type": "noul",
        "instructions": "Does the user threaten to cancel or leave?"
    }
}

不要直接复制别人给出的 confidence 阈值用于自动化。生产前应使用自己的数据验证和校准。

一个请求里回答多个问题

Laya 的设计目标之一,是在一次 forward pass 中评估多个 typed questions。

推荐的生产模式

  1. Laya 先做结构化决策。
  2. 已验证的高置信度场景自动处理。
  3. 模糊场景 fallback 到更大的模型或人工。
  4. 持续记录结果,重新评估阈值。

想看一个真实任务从第一次调用到阈值设置、效果评估的完整过程,见 Laya 使用教程。

来源:Laya 上游仓库。

最后核验:2026 年 9 月 22 日。