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:
+71
-3
@@ -24,6 +24,8 @@ from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
|
||||
class LLMConfig(BaseModel):
|
||||
"""一个 LLM 档位的接入参数。"""
|
||||
|
||||
provider: str = "openai_compat"
|
||||
model: str = "deepseek-v4.1-flash"
|
||||
base_url: str = "https://api.deepseek.com/v1"
|
||||
@@ -31,6 +33,55 @@ class LLMConfig(BaseModel):
|
||||
timeout: float = 60.0
|
||||
temperature: float | None = 0.85
|
||||
max_tokens: int | None = None
|
||||
languages: list[str] = Field(default_factory=lambda: ["zh", "en"])
|
||||
|
||||
|
||||
def _default_tiers() -> dict[str, LLMConfig]:
|
||||
"""三个档位的默认值。
|
||||
|
||||
high 旗舰。主对话、主动联系、危机。
|
||||
standard 同一个模型但更短上下文,或更便宜的型号。
|
||||
low 本地小模型。后台压缩、抽取、生活事件。
|
||||
|
||||
"none" 档不需要配置——它代表「不调用模型」。
|
||||
"""
|
||||
return {
|
||||
"high": LLMConfig(),
|
||||
"standard": LLMConfig(),
|
||||
"low": LLMConfig(
|
||||
provider="openai_compat",
|
||||
model="qwen3.5-4b-instruct",
|
||||
base_url="http://127.0.0.1:8000/v1",
|
||||
api_key_env=None,
|
||||
timeout=20.0,
|
||||
temperature=0.4,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def _default_routing() -> dict[str, str]:
|
||||
"""场景 → 档位。与 think/scenes.py 的 DEFAULT_ROUTING 保持一致。"""
|
||||
return {
|
||||
"dialogue_main": "high",
|
||||
"dialogue_followup": "standard",
|
||||
"proactive_message": "high",
|
||||
"crisis": "high",
|
||||
"life_event": "low",
|
||||
"memory_gist": "low",
|
||||
"fact_extract": "low",
|
||||
"output_guard": "low",
|
||||
"pattern_detect": "low",
|
||||
"filler": "none",
|
||||
"tick": "none",
|
||||
"reach_out": "none",
|
||||
"avatar_drive": "none",
|
||||
}
|
||||
|
||||
|
||||
class BudgetSettings(BaseModel):
|
||||
daily_tokens: int = 2_000_000
|
||||
warn_ratio: float = 0.7
|
||||
hard_stop_ratio: float = 0.95
|
||||
|
||||
|
||||
class ASRConfig(BaseModel):
|
||||
@@ -93,9 +144,14 @@ class AvatarConfig(BaseModel):
|
||||
|
||||
|
||||
class ModelsConfig(BaseModel):
|
||||
llm: LLMConfig = Field(default_factory=LLMConfig)
|
||||
llm_fast: LLMConfig | None = None
|
||||
llm_background: LLMConfig | None = None
|
||||
tiers: dict[str, LLMConfig] = Field(default_factory=_default_tiers)
|
||||
"""档位 → 接入参数。键必须是 high / standard / low。"""
|
||||
|
||||
routing: dict[str, str] = Field(default_factory=_default_routing)
|
||||
"""场景 → 档位。可在 yaml 里覆盖单条。"""
|
||||
|
||||
budget: BudgetSettings = Field(default_factory=BudgetSettings)
|
||||
|
||||
asr: ASRConfig = Field(default_factory=ASRConfig)
|
||||
paralinguistic: ParalinguisticConfig = Field(default_factory=ParalinguisticConfig)
|
||||
tts: TTSConfig = Field(default_factory=TTSConfig)
|
||||
@@ -104,6 +160,18 @@ class ModelsConfig(BaseModel):
|
||||
embedding: EmbeddingConfig = Field(default_factory=EmbeddingConfig)
|
||||
avatar: AvatarConfig = Field(default_factory=AvatarConfig)
|
||||
|
||||
@field_validator("tiers")
|
||||
@classmethod
|
||||
def _valid_tier_names(cls, v: dict[str, LLMConfig]) -> dict[str, LLMConfig]:
|
||||
allowed = {"high", "standard", "low", "none"}
|
||||
bad = set(v) - allowed
|
||||
if bad:
|
||||
raise ValueError(
|
||||
f"未知档位 {sorted(bad)};允许 {sorted(allowed)}。"
|
||||
"档位是架构概念,不能随意新增——新增需要同时定义降级阶梯。"
|
||||
)
|
||||
return v
|
||||
|
||||
|
||||
# ------------------------------------------------------------ character.yaml
|
||||
|
||||
|
||||
Reference in New Issue
Block a user