wikipedia fetch Ranking combining BM25 lexical relevance and embedding cosine similarity through reciprocal rank fusion

This commit is contained in:
2026-10-28 17:42:42 +01:00
parent 77fef0a499
commit bc543459c7
7 changed files with 447 additions and 53 deletions
+14 -3
View File
@@ -9,6 +9,7 @@ dead run). No API keys in code.
from __future__ import annotations
import json
import math
import urllib.request
from pathlib import Path
@@ -36,10 +37,20 @@ def embed(texts: list[str], model: str = DEFAULT_MODEL,
data = json.load(r)
except Exception as e: # noqa: BLE001 — one honest exception type
raise EmbedderDown(f"{url}: {e}") from e
vecs = data.get("embeddings")
if not vecs or len(vecs) != len(texts):
vecs = data.get("embeddings") if isinstance(data, dict) else None
if not isinstance(vecs, list) or len(vecs) != len(texts):
raise EmbedderDown(f"{url}: expected {len(texts)} vectors, got "
f"{len(vecs) if vecs else 0}")
f"{len(vecs) if isinstance(vecs, list) else 0}")
dimension = None
for vector in vecs:
if (not isinstance(vector, list) or not vector
or any(type(x) not in (int, float) or not math.isfinite(x)
for x in vector)
or not any(vector)):
raise EmbedderDown(f"{url}: expected finite, nonzero numeric vectors")
if dimension is not None and len(vector) != dimension:
raise EmbedderDown(f"{url}: inconsistent embedding dimensions")
dimension = len(vector)
return vecs