游戏与机器人决策
把 Jev 当 NPC 和机器人每一步的决策开关,约 1 秒返回概率,后面接规则或大模型。
痛点
NPC 和机器人每一步都要做决定。调大模型一次几百毫秒到几秒,玩家等不了。写成 if/else 又太僵,遇到新打法就崩。需要一个又快又能处理复杂 state 的判断层。
Jev 判断什么
这次我跑的是一个像素冒险游戏的城门守卫场景。state(上下文状态)里写了游戏名、NPC 名、NPC 心情、玩家动作。两条题一起问。
第一题 next_move 是 Choice(选择题),问 NPC 下一步怎么做。四个选项摆在面前:warn(再警告一次,继续挡门)、fight(直接动手)、call_guards(叫其他守卫来)、let_in(放行)。返回选了 call_guards,概率分别是 warn=0.27、fight=0.32、call_guards=0.41、let_in=0.0。置信度只有 0.21,说明模型自己也没太拿准,但 let_in 已经 0.0,可以直接排除。代码里拿到这个结果,概率最高的不是 let_in 就走对应分支;如果几个选项概率很接近,就走兜底逻辑。
第二题 hostile 是 Noul(是非题),问玩家行为是否带敌意。返回「是」的概率 0.87,这次 0.87 离 0.5 很远,可以直接视为敌意行为触发。代码里用这个判断要不要走警告流程或战斗预备。
和大模型怎么配合
流程是 Jev 先判断,代码按返回的概率走规则。先看置信度或概率分布是不是够确定,确定就直接走规则,不调大模型。这次 call_guards 置信度只有 0.21,接近需要复核的边界,可以交给大模型或人工再确认一次。如果 hostile 那种 Noul 题直接给出 0.87 高置信度,后面规则就够了,不用再调大模型。
耗时方面,模型 jev-1.14.0 这条调用 1047 毫秒,输入 433 个 token,花费 0.000018 美元,按这个算每 1000 次约 0.0182 美元。Jev 输出不收钱,只有输入 token 算钱。
上线前注意
- 题目用英文写,state 保留中文原文。Jev 对英文 question 训练过,混中文会拉低判断质量。
- 阈值要用自己 20–30 条真实数据校准,文档里的 0.5、0.8 只是起点,不是默认值。
- 置信度低(比如这次的 0.21),或者 Noul 题答案概率在 0.5 附近时,交给人或大模型复核,别让规则硬走。
- state 里要带上上下文的关键信号,比如玩家已经第三次闯门、手里拿着剑,这些直接决定了 hostile 和 next_move 的走向,少了关键信号判断会偏。
真实调用
请求
curl https://api.typesafe.ai/v1/systemone \
-H "Authorization: Bearer $TYPESAFE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "jev-1.14.0",
"state": {
"game": "像素冒险游戏",
"npc": "城门守卫",
"npc_mood": "警惕",
"player_action": "玩家第三次试图不付钱直接闯进城门,手里拿着剑"
},
"questions": {
"next_move": {
"type": "choice",
"instructions": "What should the NPC do next?",
"criteria": {
"warn": "Give a final warning and keep blocking the gate",
"fight": "Attack the player",
"call_guards": "Call other guards for help",
"let_in": "Let the player in"
}
},
"hostile": {
"type": "noul",
"instructions": "Is the player behaving in a hostile way?"
}
}
}'import json, os, urllib.request
payload = json.loads("{\n \"model\": \"jev-1.14.0\",\n \"state\": {\n \"game\": \"像素冒险游戏\",\n \"npc\": \"城门守卫\",\n \"npc_mood\": \"警惕\",\n \"player_action\": \"玩家第三次试图不付钱直接闯进城门,手里拿着剑\"\n },\n \"questions\": {\n \"next_move\": {\n \"type\": \"choice\",\n \"instructions\": \"What should the NPC do next?\",\n \"criteria\": {\n \"warn\": \"Give a final warning and keep blocking the gate\",\n \"fight\": \"Attack the player\",\n \"call_guards\": \"Call other guards for help\",\n \"let_in\": \"Let the player in\"\n }\n },\n \"hostile\": {\n \"type\": \"noul\",\n \"instructions\": \"Is the player behaving in a hostile way?\"\n }\n }\n}")
headers = {
"Authorization": "Bearer " + os.environ["TYPESAFE_API_KEY"],
"Content-Type": "application/json",
}
req = urllib.request.Request(
"https://api.typesafe.ai/v1/systemone",
data=json.dumps(payload).encode("utf-8"),
headers=headers,
method="POST",
)
print(urllib.request.urlopen(req).read().decode("utf-8"))
返回
{
"model": "jev-1.14.0",
"answers": {
"next_move": {
"type": "choice",
"choice": "call_guards",
"confidence": 0.21,
"probabilities": {
"warn": 0.27,
"fight": 0.32,
"call_guards": 0.41,
"let_in": 0
}
},
"hostile": {
"type": "noul",
"noul": 0.87
}
},
"usage": {
"input_tokens": 433,
"output_tokens": 69
}
}人话解读
- Choice:选了 call_guards,置信度 0.21
- Noul:「是」的概率 0.87