fix: add _cosine dimension guard, fix return type annotation, add zero-vector test

This commit is contained in:
pyr0ball 2026-05-07 08:59:24 -07:00
parent 276bdadb92
commit 5ea77da97d
2 changed files with 10 additions and 1 deletions

View file

@ -45,7 +45,7 @@ def _config_file() -> Path:
return _ROOT / "config" / "label_tool.yaml" return _ROOT / "config" / "label_tool.yaml"
def _load_config() -> dict: def _load_config() -> dict[str, Any]:
f = _config_file() f = _config_file()
if not f.exists(): if not f.exists():
return {} return {}
@ -73,6 +73,10 @@ def _ratings_path() -> Path:
def _cosine(a: list[float], b: list[float]) -> float: def _cosine(a: list[float], b: list[float]) -> float:
if len(a) != len(b):
raise ValueError(
f"Embedding dimension mismatch: {len(a)} vs {len(b)}"
)
dot = sum(x * y for x, y in zip(a, b)) dot = sum(x * y for x, y in zip(a, b))
mag_a = math.sqrt(sum(x * x for x in a)) mag_a = math.sqrt(sum(x * x for x in a))
mag_b = math.sqrt(sum(x * x for x in b)) mag_b = math.sqrt(sum(x * x for x in b))

View file

@ -48,3 +48,8 @@ def test_cosine_orthogonal():
def test_cosine_opposite(): def test_cosine_opposite():
from app.eval.embed_bench import _cosine from app.eval.embed_bench import _cosine
assert _cosine([1.0, 0.0], [-1.0, 0.0]) == pytest.approx(-1.0) assert _cosine([1.0, 0.0], [-1.0, 0.0]) == pytest.approx(-1.0)
def test_cosine_zero_vector_returns_zero():
from app.eval.embed_bench import _cosine
assert _cosine([0.0, 0.0], [1.0, 0.0]) == pytest.approx(0.0)