feat: per-signature LM router (lm[<SigName>] config blocks + predict factory)
His ask: one general construction door so a signature's model/settings change by editing config alone — no call-site ifs, now or ever after. lm[ExtractSignature]: as a config.yaml sibling of lm: (provider: inherits base_url + transport ceiling; model: builds a dedicated cached LM; other keys -> per-call lm_kwargs through Predict config, honored by dspy 3.4.1 _forward_preprocess). predict() is the door: the 13 construction sites swapped mechanically. First block shipped: ExtractSignature max_tokens=1800 (the 4096 cap was 3x headroom that only extended the worst case on slow local models — the 40-min arithmetic). run.log reports active blocks at startup. TDD: tests/lm_signature_router.py 5/5 (parse, no-block defaults, settings-only override, dedicated+cached LM, provenance listing).
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
"""The per-signature LM router tests (lm[<SignatureName>] config blocks).
|
||||
|
||||
Law: any dspy.Signature may carry its own settings block in config.yaml —
|
||||
`lm[ExtractSignature]:` — inheriting its provider's transport ceiling and
|
||||
overriding model/max_tokens/temperature. The factory `floop.llm.predict()`
|
||||
applies the block at CONSTRUCTION: no call-site if's; a signature can
|
||||
change model by editing config alone, forever after (his ask, 26/10: one
|
||||
general function so no more config edits mid-code as the pipeline grows).
|
||||
|
||||
Pinned:
|
||||
1. The config block parses and carries only the intended overrides.
|
||||
2. predict() with no block -> plain dspy.Predict (defaults, untouched).
|
||||
3. predict() with a settings-only block -> Predict whose config carries
|
||||
the overrides (dspy merges self.config as lm_kwargs per call —
|
||||
verified against dspy 3.4.1 _forward_preprocess).
|
||||
4. predict() with a model: override -> Predict bound to a DEDICATED
|
||||
cached dspy.LM (same provider base_url/ceiling, its own model); a
|
||||
second predict() returns the SAME LM object (no rebuild per call).
|
||||
5. A block for a signature that is never constructed breaks nothing.
|
||||
6. signature_overrides() lists the active blocks for the run.log line.
|
||||
|
||||
Run: .venv/bin/python tests/lm_signature_router.py
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
import floop.llm as llm
|
||||
from floop.llm import predict, signature_overrides
|
||||
from knowledge.extract import ExtractSignature
|
||||
from floop.dspy_modules import InitSignature
|
||||
|
||||
|
||||
def test_config_block_parses():
|
||||
"""config.yaml carries lm[ExtractSignature] with the intended override."""
|
||||
import yaml
|
||||
cfg = yaml.safe_load((Path(__file__).resolve().parent.parent
|
||||
/ "config.yaml").read_text())
|
||||
assert "lm[ExtractSignature]" in cfg, (
|
||||
"the per-signature block must be a top-level key `lm[<SigName>]`")
|
||||
block = cfg["lm[ExtractSignature]"]
|
||||
assert block.get("max_tokens") == 1800, block
|
||||
assert "model" not in block, "no model override yet (his call: default)"
|
||||
assert "lm[NoSuchSignature]" not in cfg
|
||||
print("ok: config.yaml carries the lm[ExtractSignature] block, model-free")
|
||||
|
||||
|
||||
def test_no_block_means_plain_predict():
|
||||
"""A signature without a block gets the defaults, untouched."""
|
||||
p = predict(InitSignature)
|
||||
assert type(p).__name__ == "Predict"
|
||||
assert not p.config, f"no overrides expected, got {p.config}"
|
||||
assert p.lm is None, "no dedicated LM without a model override"
|
||||
print("ok: no block -> plain dspy.Predict, defaults untouched")
|
||||
|
||||
|
||||
def test_settings_block_becomes_predict_config():
|
||||
"""Settings-only block -> per-call lm_kwargs (dspy honors self.config)."""
|
||||
p = predict(ExtractSignature)
|
||||
assert p.config.get("max_tokens") == 1800, p.config
|
||||
assert p.lm is None, "settings-only block must not build a new LM"
|
||||
print("ok: lm[ExtractSignature].max_tokens -> Predict config (lm_kwargs)")
|
||||
|
||||
|
||||
def test_model_override_builds_dedicated_cached_lm():
|
||||
"""A model: override binds a dedicated LM; the factory caches it."""
|
||||
orig_load = llm._load_yaml
|
||||
llm._load_yaml = lambda: { # noqa: E731 — monkeypatch, test-only
|
||||
"lm": {"default": "local",
|
||||
"local": {"base_url": "http://127.0.0.1:11435/v1",
|
||||
"model": "qwen3.8-27b-uc-64k:latest",
|
||||
"max_tokens": 4096, "temperature": 0.0,
|
||||
"read_timeout_s": 1800, "num_retries": 1}},
|
||||
"lm[InitSignature]": {"provider": "local", "model": "qwen3.8:27b",
|
||||
"max_tokens": 1024},
|
||||
}
|
||||
llm._SIGNATURE_LMS.clear()
|
||||
try:
|
||||
p = predict(InitSignature)
|
||||
assert p.lm is not None, "model override must bind a dedicated LM"
|
||||
assert p.lm.model == "openai/qwen3.8:27b", p.lm.model
|
||||
assert p.lm.kwargs["timeout"] == 1800, "inherits provider ceiling"
|
||||
assert p.lm.kwargs["max_tokens"] == 1024, "block max_tokens on the LM"
|
||||
assert not p.config, "dedicated LM carries the settings, not config"
|
||||
# cached: a second construction reuses the SAME LM object
|
||||
p2 = predict(InitSignature)
|
||||
assert p2.lm is p.lm, "the factory must cache LMs per provider+model"
|
||||
finally:
|
||||
llm._load_yaml = orig_load
|
||||
llm._SIGNATURE_LMS.clear()
|
||||
print("ok: model override -> dedicated cached LM (provider ceiling inherited)")
|
||||
|
||||
|
||||
def test_signature_overrides_lists_active_blocks():
|
||||
ov = signature_overrides()
|
||||
block = ov.get("ExtractSignature")
|
||||
assert block and block.get("max_tokens") == 1800 and block.get("provider") == "local", ov
|
||||
assert "NoSuchSignature" not in ov
|
||||
print(f"ok: signature_overrides lists active blocks: {list(ov)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_config_block_parses()
|
||||
test_no_block_means_plain_predict()
|
||||
test_settings_block_becomes_predict_config()
|
||||
test_model_override_builds_dedicated_cached_lm()
|
||||
test_signature_overrides_lists_active_blocks()
|
||||
print("\nlm_signature_router: 5/5 ok")
|
||||
Reference in New Issue
Block a user