PurpleMIST-Mini-2.0实战教程:decide() API完全指南,从文本到JSON再到图片输入
PurpleMIST-Mini-2.0实战教程:decide() API完全指南,从文本到JSON再到图片输入
PurpleMIST-Mini-2.0 是一款 System One 多模态决策模型,它的全部能力浓缩在一个 decide() API 里:传入一段"状态"(消息、工单、记录、收据或截图)和任意多个带类型的问题,一次前向推理即可为每个问题返回校准过的概率分布。本教程带你从安装到部署,完整走通 decide() 的三种输入方式:纯文本、JSON 结构化状态、图片输入。
PurpleMIST-Mini-2.0 是什么?
与传统"生成一段文字"的大语言模型不同,PurpleMIST 是一个决策模型(decision model):它不生成段落,而是为每个选项打分,直接返回概率。核心特点:
- ⚡ 快:单条文本请求中位耗时约 70 ms(RTX PRO 6000 实测)
- 🖼️ 多模态:同时读文本和图片,视觉榜单成绩位列 6B 以下参数模型前 6
- 长上下文:默认单次 98,304 tokens,且从不截断
- 🌍 多语言:19 种语言官方校准(含中文、法语、西班牙语、豪萨语、约鲁巴语等),问题语言可以与状态语言不同
- 概率已校准:每种题型单独拟合温度参数,概率可以直接当概率用
模型基于 Qwen3.5-4B-Base 骨干,约 4.5B 参数,bf16 权重约 8.5 GB(model.safetensors),一张消费级 GPU 即可运行。
三种内置题型
| 类型 | 你提供什么 | 返回什么 |
|---|---|---|
choice |
指令 + 命名选项(criteria:键 → 描述) |
{"choice": 键, "probabilities": {...}} |
noul |
一句"是/否"陈述 | {"noul": p_yes}("是"的概率) |
score |
指令 + 有序等级描述(从最低开始) | {"score": 期望等级, "probabilities": {"0": p, ...}} |
对应业务场景:"这张工单该派给哪个团队?"是 choice;"客户在要求退款吗?"是 noul;"这件事有多紧急?"是 score。
环境安装与模型加载
先安装依赖(Python + PyTorch 环境):
pip install "transformers>=5.22" "torch>=2.12" huggingface_hub pillow
pip install flash-linear-attention causal-conv1d # 可选:加速线性注意力层
然后用入口类 Decider(定义在 purplemist.py)两行代码加载模型:
import os, sys
from huggingface_hub import hf_hub_download
repo = "olaverse/PurpleMIST-Mini-2.0"
sys.path.insert(0, os.path.dirname(hf_hub_download(repo, "purplemist.py")))
from purplemist import Decider
d = Decider.from_pretrained(repo, device="cuda")
💡 提示:建议保持 bf16 精度运行——这正是模型校准时的精度(配置项
inference_dtype)。如需本地获取全部代码文件,可执行git clone https://gitcode.com/hf_mirrors/olaverse/PurpleMIST-Mini-2.0。
文本输入:decide() 最小调用
decide() 的签名是 d.decide(state, questions, image=None):
state:状态,字符串或任意可 JSON 序列化的对象questions:字典,键为问题 ID,值包含type、instructions和可选的criteriaimage:可选,图片或图片列表
一个典型客服工单示例(来自官方 README.md):
state = {"channel": "whatsapp",
"message": "I was charged twice for the same order, please refund me."}
questions = {
"team": {"type": "choice", "instructions": "Which team should handle this?",
"criteria": {"billing": "payments, charges and refunds",
"delivery": "orders in transit",
"technical": "app or account problems"}},
"refund": {"type": "noul", "instructions": "The customer is asking for money back."},
"urgency": {"type": "score", "instructions": "How urgent is this?",
"criteria": ["can wait", "normal queue", "today", "immediately"]},
}
print(d.decide(state, questions))
返回结果是按问题 ID 组织的字典,例如 team 会返回 {"type": "choice", "choice": "billing", "probabilities": {"billing": 0.83, ...}}。你可以直接取 choice 的最高概率选项,也可以基于 probabilities 按自己的阈值分流。
JSON 结构化输入:decide() 的真正威力
state 可以是任何 JSON 对象,推理时会按 json.dumps(state, ensure_ascii=False) 序列化成文本(见 purplemist.py 的 _state_text)。这意味着你可以灵活传:
- 原始字符串:
d.decide("I was charged twice, refund me", q) - 结构化工单:
d.decide({"channel": ..., "message": ...}, q) - 业务字段混合:
{"order_id": 1024, "amount": 99.9, "notes": [...]}
模型在"状态 + 问题"配对上训练,结构化字段不但不会干扰,反而帮助它定位关键信息。此外问题与状态可以使用不同语言(跨语言留出集准确率 0.937),这对多语言客服系统非常实用。
图片输入:收据、截图与前后对比
Mini-2.0 自带视觉编码器,给 decide() 传入 image 参数即可启用图片理解。它接受的格式:文件路径、bytes、base64 字符串、data URL、PIL 图像,或它们的列表(多图按顺序排列):
# 单图:收据核对
q = {"total_ok": {"type": "noul",
"instructions": "The receipt total matches the sum of its line items."},
"category": {"type": "choice", "instructions": "What kind of purchase is this?",
"criteria": {"food": "groceries or restaurant",
"transport": "fuel, taxi or tickets",
"other": "anything else"}}}
print(d.decide("", q, image="receipt.jpg"))
# 多图:修复前后对比
print(d.decide({"note": "before and after the repair"},
{"fixed": {"type": "noul",
"instructions": "The crack visible in the first photo is gone in the second."}},
image=["before.jpg", "after.jpg"]))
两个使用要点:
state可以是空字符串——当图片本身承载全部内容时(如只传一张收据),直接传""即可。- 每张图片有 token 预算:默认 1,024 tokens(1 token = 32×32 像素,约 1 MP),更清晰的图会自动缩放,可通过
image_max_tokens参数调高。
官方评测中,该决策模型在收据任务上准确率达 0.984(2,116 题),截图 0.907,真实照片 0.907,完整数据见 eval/metrics.csv。
用 serve.py 部署 HTTP 决策服务
面向多语言客户端时,仓库自带的 serve.py 可以把模型变成一个本地 HTTP 服务,请求/响应与 System One API 形状兼容:
hf download olaverse/PurpleMIST-Mini-2.0 serve.py purplemist.py --local-dir purplemist
python purplemist/serve.py --model olaverse/PurpleMIST-Mini-2.0 --port 8000
用 curl 发送决策请求;图片请求只需在请求体中额外加一个 "images": [...] 字段(data URL、base64 或路径):
curl -s localhost:8000/v1/systemone -H 'content-type: application/json' -d '{
"state": {"message": "I was charged twice, please refund me"},
"questions": {
"team": {"type": "choice", "instructions": "Which team?", "criteria": {"billing": "payments", "tech": "app problems"}},
"refund": {"type": "noul", "instructions": "The customer asks for money back."}}}'
| 端点 | 说明 |
|---|---|
POST /v1/systemone |
决策请求(别名 /v1/decisions) |
GET /health |
健康检查 |
GET /v1/models |
查看已加载模型 |
响应还会附带 confidence 与 legend 字段,兼容 Ollama 决策 API 客户端;超出上下文窗口的请求返回 HTTP 422 并提示 "maximum context length"。常用命令行参数:
| 参数 | 说明 | 默认 |
|---|---|---|
--max-len |
单次 pass 的 token 数 | 98,304(训练长度) |
--image-max-tokens |
每图 token 上限 | 1,024(约 1 MP) |
--no-truncate |
不截断,装不下直接拒答(2.0 本身即不截断) | 关闭 |
--no-warmup |
跳过启动时的长度分带预热 | 关闭 |
关键参数与常见报错
Decider.from_pretrained 参数速查
| 参数 | 说明 |
|---|---|
device |
cuda / mps / cpu |
max_len |
单次 pass 的 token 预算,默认 98,304,最高可到骨干的 262,144 |
truncate |
设为 False 时绝不缩短状态或选项文本,装不下的请求直接报错 |
image_max_tokens |
每图 token 上限(1 token = 32×32 px) |
dtype |
精度,建议留空使用默认 bf16,与校准精度一致 |
常见报错排查
ValueError: maximum context length is 98304 tokens ...:状态 + 问题装不进单次 pass。可传入更大的max_len;放不下时问题会自动拆分到多次 pass,完全装不下的状态会被拒答(逻辑见 purplemist.py)。ValueError: this model does not take images:当前加载的是纯文本 1.0 模型,升级到 Mini-2.0 即可。- CUDA out of memory:HTTP 服务会返回 422 "too many tokens for the available GPU memory",可调小
max_len或image_max_tokens。
关于概率校准
模型的每个题型都有单独拟合的温度参数:文本 choice 1.074 / noul 1.468 / score 0.955,图片另有一套,均保存在 purplemist_config.json 中。因此返回的 probabilities 可以直接用于阈值判断;分题型指标可参考 eval/metrics_by_kind.csv。
总结
本文带你完成了 PurpleMIST-Mini-2.0 decide() API 的完整实战:
- 文本输入:
state传字符串,一次调用同时回答多个问题 - JSON 输入:
state传结构化对象,字段帮助模型定位信息,且支持跨语言提问 - 图片输入:
image参数接受路径 / bytes / base64 / PIL / 多图列表,覆盖收据、截图、前后对比场景 - 服务化部署:
serve.py一行命令起 HTTP 服务,客户端按 System One 形状调用
一次前向推理、每个问题一份概率分布——这正是 PurpleMIST-Mini-2.0 适合工单分派、内容审核、报销核对、多语言客服等高通量决策场景的原因。