Decision Models in Hugging Face
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Decision Models in Hugging Face
Large Language Models (LLMs) are exceptionally good at generating text. We use them to answer questions, write code, reason about problems, and so much more. But not every problem requires generating text. Consider a customer support system receiving the following message: “I was charged twice for my subscription. I need a refund today.” The system needs to determine which team should handle the request, how urgent it is, and whether the customer is requesting a refund. We could ask an LLM to return a JSON object containing these answers.
大型语言模型(LLM)在生成文本方面表现极其出色。我们使用它们来回答问题、编写代码、推理问题等等。但并非每个问题都需要生成文本。考虑一个客户支持系统收到以下消息:“我的订阅被扣了两次费。我今天需要退款。”系统需要确定应该由哪个团队处理该请求、紧急程度如何,以及客户是否在要求退款。我们可以要求 LLM 返回一个包含这些答案的 JSON 对象。
{
"team_to_handle": "Billing",
"urgency": "High",
"request_refund": true
}
But what if the model could directly evaluate the available options and return a probability distribution over them, without generating any text?
但如果模型可以直接评估可用选项并返回其概率分布,而无需生成任何文本,会怎样呢?
from transformers import pipeline
decide = pipeline("decision", model="autotrust/GEV-26B-Decide")
decision = decide(
{
"state": "I was charged twice for my order and want a refund.",
"questions": {
"department": {
"type": "choice",
"instructions": "Which team should handle this?",
"criteria": {
"billing": "Payments and refunds",
"technical": "Bugs and technical issues",
"sales": "Pricing and subscriptions"
}
},
}
}
)
This is the key idea behind decision models. What are decision models? A decision model takes some input context (state), evaluates a question with a set of predefined possible answers (primitives), and returns a structured decision along with probabilities. Instead of asking a model to generate an answer token by token, we ask it to assign probabilities to the answers that our application already understands.
这就是决策模型背后的核心思想。什么是决策模型?决策模型接收一些输入上下文(状态),用一组预定义的可能答案(原语)评估一个问题,并返回一个结构化的决策以及概率。我们不是要求模型逐个 token 地生成答案,而是要求它为应用程序已经理解的答案分配概率。
TypeSafe AI introduced Jev, describing it as a System One model. The name draws inspiration from the distinction between fast, intuitive thinking (System 1) and slower, deliberative thinking (System 2). The community has converged to the term decision model, because it describes the model’s computational role rather than invoking a particular theory of cognition. In the rest of the post we will refer to the models as “decision models” instead of “system one models”.
TypeSafe AI 推出了 Jev,并将其描述为“系统一”(System One)模型。这个名字的灵感来源于快速、直觉的思维(系统 1)与较慢、审慎的思维(系统 2)之间的区别。社区最终采用了“决策模型”这一术语,因为它描述了模型的计算角色,而不是援引某种特定的认知理论。在本文的其余部分,我们将把这些模型称为“决策模型”,而不是“系统一模型”。
For a decision model, the fundamental interface is: State + Questions + Allowed Answers → Decisions + Probabilities.
对于决策模型,其基本接口是:状态 + 问题 + 允许的答案 → 决策 + 概率。
Why not use LLMs for everything? We could simply prompt an LLM to return a JSON object containing our decisions. In fact, this is how many applications already use language models for classification, routing, and other structured tasks. However, there are a few problems with this approach. LLMs are not guaranteed to follow the requested schema: Even with explicit instructions, a model might produce invalid JSON, introduce unexpected keys, or return an answer outside the allowed choices.
为什么不把所有事情都交给 LLM 处理?我们可以简单地提示 LLM 返回一个包含我们决策的 JSON 对象。事实上,许多应用程序已经在分类、路由和其他结构化任务中使用语言模型了。然而,这种方法存在一些问题。LLM 无法保证遵循所要求的模式:即使有明确的指令,模型也可能生成无效的 JSON、引入意外的键,或返回超出允许范围的答案。
Constrained decoding helps, but does not eliminate the computational overhead: Libraries such as Outlines and XGrammar can enforce grammars or JSON schemas by restricting which tokens the model is allowed to generate at each step. This ensures the output follows the supported structural constraints, but introduces additional work. Why generate an entire response: LLMs are designed for open-ended generation, but when our application only needs to choose between predefined answers, autoregressive decoding can be unnecessary work.
受限解码(Constrained decoding)有所帮助,但并不能消除计算开销:诸如 Outlines 和 XGrammar 之类的库可以通过限制模型在每一步允许生成的 token 来强制执行语法或 JSON 模式。这确保了输出遵循支持的结构约束,但也引入了额外的工作。为什么要生成完整的响应:LLM 是为开放式生成而设计的,但当我们的应用程序只需要在预定义的答案之间进行选择时,自回归解码可能会造成不必要的工作。
How are decision models different from classification models? Before understanding the interface of a decision model, it is useful to revisit two closely related approaches namely traditional classification and zero-shot classification.
决策模型与分类模型有何不同?在理解决策模型的接口之前,回顾两种密切相关的方法(即传统分类和零样本分类)是很有用的。
Traditional classification: Suppose we train a classifier that routes customer-support requests to three departments: Billing, Technical support, Sales. A conventional classification model learns a mapping from an input to a predefined set of labels. During training, the model learns to predict these labels from examples. A classification head typically produces one logit per class, and softmax turns those logits into a probability distribution. The limitation here is that the output classes are baked into the model’s classification head. If we introduce a fourth department, such as Legal, the existing classifier does not automatically acquire a corresponding output class. We may need to modify the head, collect training data, and retrain or adapt the model.
传统分类:假设我们训练一个分类器,将客户支持请求路由到三个部门:账单、技术支持、销售。传统的分类模型学习从输入到预定义标签集的映射。在训练过程中,模型通过示例学习预测这些标签。分类头通常为每个类别生成一个 logit,而 softmax 将这些 logit 转换为概率分布。这里的局限性在于输出类别被固定在模型的分类头中。如果我们引入第四个部门(例如法律部),现有的分类器不会自动获得相应的输出类别。我们可能需要修改分类头、收集训练数据并重新训练或调整模型。
Zero-shot classification: Zero-shot classification addresses an important part of this limitation. Instead of requiring the classifier to be trained specifically on every output category, we supply the candidate labels at inference time. Unlike a conventional fixed-label classifier, the categories can change from one request to another. Let’s add another department, such as “Legal”, and see the model outputs.
零样本分类:零样本分类解决了这一局限性的重要部分。我们不需要分类器针对每个输出类别进行专门训练,而是在推理时提供候选标签。与传统的固定标签分类器不同,类别可以随请求的不同而改变。让我们添加另一个部门(例如“法律部”),看看模型的输出。