feat(think): four-tier model routing with budget guard

Corrects a common misconception: the 60s heartbeat must not call any LLM.
It runs the mood decay equation -- microseconds of float math. Giving it a
model is an architecture error, not a resource problem.

Real token spend lands in two places:
  - per-turn input context (ontology + state + dyad + memory)
  - background task frequency

So route by scene, not by time:

  none      rule/math     tick, reach-out decision, fillers, avatar drive
  low       local small   life events, memory gist, fact extract
  standard  mid + short   speculative-response second segment
  high      flagship      main dialogue, proactive message, crisis

Hard constraints:
  - dialogue floor is LOW, never NONE (can get dumber, cannot lose lines)
  - crisis never degrades (product constraint, not perf tuning)
  - degradation is predictable: one rung per request, floor only at
    the hard budget threshold

Cost levers, by actual payoff:
  1. prompt caching (fixed prefix) -- by far the largest
  2. upstream compression (small model condenses 20 memories to 3)
  3. batching (one batch per 30min, not 20 single calls)
  4. swapping to a smaller model -- smallest payoff, hurts persona

Also: cost degradation is narratable. 'Net error -> I zoned out' already
exists in the projection table, so being throttled is in-character and true.

- config.py: tiers / routing / budget, replacing llm+llm_fast+llm_background
- think/: scenes, budget, router, wiring
- models.yaml restructured around tiers
- SPEC gains 9.5
- 20 new tests; 78 total pass; ruff clean
This commit is contained in:
mobai
2026-10-19 20:14:41 +08:00
parent 1012d8bb79
commit 8dea68a25b
10 changed files with 1129 additions and 33 deletions
+69 -25
View File
@@ -2,36 +2,80 @@
# 模型选型 —— 全项目唯一允许写模型版本号的地方
# =============================================================================
#
# 为什么集中在这里:
# 任何写死版本号的代码,三个月后都会过时。把"会过期的东西"压缩到
# 这一个文件,换模型时只改这里,其余部分不动。
# 三层架构:大模型按「关键时刻」分配,不按时间分配。
#
# none 纯规则。心跳、主动性判定、填充词、表情。不调用任何模型。
# low 本地小模型。生活事件、记忆压缩、事实抽取、输出初筛。
# standard 中档。投机响应的第二段、短上下文场景。
# high 旗舰。主对话、主动联系、危机场景。
#
# 一个 60 秒心跳只做四则运算(情绪衰减方程),给它模型是架构错误。
#
# 版本依据:2026-10 调研。每季度复核一次。
#
# 换模型时的纪律:
# 1. 新模型发布到入选,至少间隔一个季度(除非它解决的是你的阻塞问题)
# 2. 换完跑一遍 tests/test_providers_contract.py
# 3. 记录降级情况:如果新 provider 少了某项能力,日志里会有 [degraded]
# =============================================================================
# ---- 主对话 ----
# OpenAI 兼容 /v1/chat/completions。DeepSeek / Qwen / vLLM / Ollama 都通用。
llm:
provider: openai_compat
model: deepseek-v4.1-flash
base_url: https://api.deepseek.com/v1
api_key_env: DEEPSEEK_API_KEY
timeout: 60.0
temperature: 0.85
tiers:
# ---- 旗舰:人设浓度最高的时刻 ----
high:
provider: openai_compat
model: deepseek-v4.1-flash
base_url: https://api.deepseek.com/v1
api_key_env: DEEPSEEK_API_KEY
timeout: 60.0
temperature: 0.85
# ---- 快反 / 后台(本地小模型,可选)----
# 用不到就整段注释掉,代码会回退到 llm。
# llm_fast:
# provider: openai_compat
# model: qwen3.5-4b-instruct
# base_url: http://127.0.0.1:8000/v1
# api_key_env: null
# timeout: 20.0
# ---- 中档:可以指向同一个模型,但配更短的上下文预算 ----
# 省钱的杠杆通常不是换更小的模型,而是少喂上下文。
standard:
provider: openai_compat
model: deepseek-v4.1-flash
base_url: https://api.deepseek.com/v1
api_key_env: DEEPSEEK_API_KEY
timeout: 40.0
temperature: 0.7
# ---- 本地小模型:后台任务的主力 ----
# 没有本地服务时,把它指向 openai_compat 的远端小模型即可。
low:
provider: openai_compat
model: qwen3.5-4b-instruct
base_url: http://127.0.0.1:8000/v1
api_key_env: null
timeout: 20.0
temperature: 0.4
# ---- 场景 → 档位 ----
# 改这里就能调成本结构,不用改代码。
routing:
# 对话
dialogue_main: high # 主对话台词
dialogue_followup: standard # 投机响应的第二段
proactive_message: high # 主动联系的措辞
crisis: high # 危机场景,永不降级
# 后台
life_event: low # 每天 1–4 条生活事件
memory_gist: low # 记忆压缩(量最大)
fact_extract: low # 语义事实抽取
output_guard: low # 输出校验
pattern_detect: low # 关系模式识别
# 无模型(这些写 dead 也应该是 none)
filler: none # 填充词:从状态核心查表
tick: none # 心跳:纯数学
reach_out: none # 主动性判断:纯规则
avatar_drive: none # 表情姿态:由 State 直接算
# ---- 预算 ----
# 没有预算控制的架构,在第一个月账单出来时就会崩。
budget:
daily_tokens: 2000000
warn_ratio: 0.7
hard_stop_ratio: 0.95
# =============================================================================
# 语音链路
# =============================================================================
# ---- 主 ASR ----
# 内部契约是「事件流」:partial → final。