大模型和小模型怎么串联
分工:小模型判断,大模型干活
Jev 是小模型,只负责判断,给它内容和题目,返回带概率和置信度的结构化答案,不写文字。大模型负责写文字、做复杂推理,比 Jev 慢、贵。Jev 每百万输入 token 收 0.042 美元、输出不收钱;本站实测文字题 1.0–1.1 秒。一次请求可以同时问多道题,所以适合做前置分流或后置把关。
先准备一个调用函数
import os
import requests
def ask_jev(state, questions, model="jev-1.14.0"):
r = requests.post(
"https://api.typesafe.ai/v1/systemone",
headers={"Authorization": f"Bearer {os.environ['TYPESAFE_API_KEY']}"},
json={"model": model, "state": state, "questions": questions},
timeout=30,
)
r.raise_for_status()
return r.json()["answers"]
def call_llm(prompt):
"""换成你正在用的大模型调用"""
raise NotImplementedErrorcall_llm 要换成你自己的大模型调用,签名保持接收一个 prompt 字符串、返回一段文本就行。questions 是一个字典,键是你给题目起的名字,答案按同样的键返回。
串法一:小模型先分流
先用 Jev 判断意图或分类,规则能处理的直接处理,剩下的才调大模型。Jev 的判断一次调用就够,文字题本站实测 1.0–1.1 秒,分流代价很低。适合 客服分流 和 内容审核。
message = "我上个月的订单还没收到"
answers = ask_jev(
{"channel": "在线客服", "messages": [{"from": "用户", "text": message}]},
{
"team": {
"type": "choice",
"instructions": "Which team should handle this message?",
"criteria": {
"shipping": "Delivery status, delays, or speeding up a shipment",
"refund": "Refunds, returns, or cancelling an order",
"other": "Anything else",
},
}
},
)
if answers["team"]["choice"] == "shipping":
reply = "已为你查询物流,稍后短信通知" # 规则直接处理,不调大模型
else:
reply = call_llm(f"以客服身份回复这条消息:{message}")所有可能的去向都要列进 criteria,选项描述要能互相区分。上线前拿一批真实消息跑一遍,看 Jev 分得对不对。
串法二:大模型写,小模型把关
大模型先生成内容,再用 Jev 检查合规或质量,过不了就重写或拦下。Noul 题型返回答案为「是」的概率,配合阈值使用。适合 智能体护栏 和 内容审核。
draft = call_llm("为这款咖啡机写一段 80 字以内的商品介绍")
answers = ask_jev(
{"product": "家用咖啡机", "draft": draft},
{
"misleading": {
"type": "noul",
"instructions": "Does `draft` contain false or exaggerated claims about the product?",
}
},
)
if answers["misleading"]["noul"] > 0.5: # 阈值用自己的样本校准
draft = call_llm(f"去掉夸大说法,重写:{draft}")Noul 没有 confidence 字段,只有「是」的概率。0.5 只是示例,阈值要先用样本调。
串法三:大模型出候选,小模型挑
让大模型出几条候选,再用一道 Choice 让 Jev 挑出最合适的一条。候选很多时,也可以每条一道 Score 打分再排序。适合 聊天副驾 和 检索结果过滤。
question = "Jev 支持哪些题型?"
candidates = [call_llm(f"回答:{question}") for _ in range(3)]
keys = [f"c{i}" for i in range(len(candidates))]
answers = ask_jev(
{"question": question, "candidates": dict(zip(keys, candidates))},
{
"best": {
"type": "choice",
"instructions": "Which candidate answers `question` most accurately and completely?",
"criteria": {k: f"The answer in `candidates.{k}`" for k in keys},
}
},
)
best = candidates[keys.index(answers["best"]["choice"])]候选之间差别越明显,Jev 挑得越稳。返回里的 probabilities 能看出前两名差多少,差得少就说明这几条都差不多。
串法四:按置信度分流
看 Jev 返回的 confidence:高就直接用结果,低就交给大模型或人。confidence 表示这次判断的概率分布有多集中,越高越可以直接执行。适合 数据抽取校验 和 聊天副驾。
question = "订单什么时候发货?"
answers = ask_jev(
{"faq": "现货订单 24 小时内发货,预售按页面写的日期发货。", "question": question},
{
"covered": {
"type": "choice",
"instructions": "Can `question` be fully answered from `faq`?",
"criteria": {
"yes": "Yes, the FAQ covers it",
"no": "No, it needs more information",
},
}
},
)
a = answers["covered"]
if a["choice"] == "yes" and a["confidence"] >= 0.75: # 阈值用自己的数据校准
reply = "现货订单 24 小时内发货"
else:
reply = call_llm(f"根据 FAQ 回答,答不了就转人工:{question}")阈值按业务风险定:风险高的场景,宁可多交给大模型或人,也别只靠一个高 confidence 放行。
算一笔账
填上你现在每天的请求量、每次 token 和你用的大模型价格,看看 Jev 能直接处理一部分后每月省多少。Jev 直接处理掉的那部分请求,就不用再花大模型的钱。