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:
@@ -40,6 +40,7 @@ providers/ 模型接入 —— 按能力建模
|
||||
core/ 状态核心 —— 独立时钟,不依赖用户输入
|
||||
memory/ 三层记忆 + 遗忘
|
||||
dyad/ 关系账本
|
||||
think/ 模型路由 + 预算 —— 四档分配,按关键时刻
|
||||
config/ models.yaml(版本号) + character.yaml(世界法则)
|
||||
```
|
||||
|
||||
@@ -108,6 +109,43 @@ if planned.lost_paralinguistics:
|
||||
|
||||
这条写在 `core/projection.py` 的注释里,也有对应的测试。
|
||||
|
||||
### 6. 大模型按「关键时刻」分配,不按时间分配
|
||||
|
||||
先纠正一个常见误解:**60 秒心跳不该调用任何 LLM。**
|
||||
|
||||
心跳做的是 `s = b + (s-b)*exp(-dt/τ) + I`,四个 float 一次 exp,
|
||||
微秒级。给它配模型是架构错误,不是资源问题。
|
||||
|
||||
真正的 token 消耗在两处:**每轮对话的输入上下文**,以及**后台任务的频次**。
|
||||
|
||||
所以用四档:
|
||||
|
||||
| 档 | 用什么 | 场景 | 成本 |
|
||||
|---|---|---|---|
|
||||
| `none` | 不调用 | 心跳、主动性判定、填充词、表情驱动 | ≈0 |
|
||||
| `low` | 本地小模型 | 生活事件、记忆压缩、事实抽取、输出初筛 | 电费 |
|
||||
| `standard` | 中大模型 + 短上下文 | 投机响应的第二段 | 低 |
|
||||
| `high` | 旗舰 | 主对话台词、主动联系、危机 | 按 token |
|
||||
|
||||
**路由判定必须便宜** —— 查表,不调模型。为了省 token 先调一次模型,是本末倒置。
|
||||
|
||||
关键约束:
|
||||
|
||||
- **主对话的下限是 `low`,不是 `none`。** 预算耗尽时可以变笨,不能没台词。
|
||||
- **危机场景永不降级。** 这是产品约束,不是性能优化。
|
||||
- **降级要可预测。** 单次请求放不下只降一档;到硬阈值才降到下限。
|
||||
|
||||
省钱的真正杠杆,按收益排序:
|
||||
|
||||
1. **Prompt 缓存** —— 本体/世界法则固定放前缀,命中后便宜一个数量级。杠杆最大。
|
||||
2. **上游压缩** —— 小模型先把 20 条记忆压成 3 条,大模型只收结果。输入从 4000 降到 800 tokens。
|
||||
3. **批处理** —— 记忆整合攒 30 分钟一次批量处理,不是 20 次单条调用。
|
||||
4. **换小模型** —— 收益反而最小,而且伤害人设。
|
||||
|
||||
还有一个漂亮的副作用:**成本降级可以被叙事化。** 大模型不可用时它"有点走神"——
|
||||
这既符合人设,又是真的(实现同构的虚构)。
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 快速开始
|
||||
@@ -138,11 +176,16 @@ python -c "from cybergril import load_settings; print(load_settings('config').ch
|
||||
只改 `config/models.yaml`,不动任何代码。
|
||||
|
||||
```yaml
|
||||
llm:
|
||||
provider: openai_compat
|
||||
model: deepseek-v4.1-flash
|
||||
base_url: https://api.deepseek.com/v1
|
||||
api_key_env: DEEPSEEK_API_KEY
|
||||
tiers:
|
||||
high:
|
||||
provider: openai_compat
|
||||
model: deepseek-v4.1-flash
|
||||
base_url: https://api.deepseek.com/v1
|
||||
api_key_env: DEEPSEEK_API_KEY
|
||||
low:
|
||||
provider: openai_compat
|
||||
model: qwen3.5-4b-instruct
|
||||
base_url: http://127.0.0.1:8000/v1
|
||||
|
||||
tts:
|
||||
provider: cosyvoice # 需要自己实现这个 adapter,见下
|
||||
|
||||
Reference in New Issue
Block a user