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
+34 -1
View File
@@ -84,7 +84,8 @@ tests/traces.py exporter tests (no network)
stored answers, and article-by-article gathering with early exit. Focused methods
handle stored-answer gating, query composition/search, cached fetching, skimming,
and post-ingestion answer checks. Stateless module-level helpers build probe
seeds, rank article titles, and filter goal-relevant skim notices. Run-wide page
seeds and filter goal-relevant skim notices; `knowledge/ranking.py` ranks search
results. Run-wide page
and link caches survive calls; extraction entities, skim notices, and pending
skim leads reset per call after the proof path falls through.
@@ -94,6 +95,38 @@ Offline orchestration regression tests (no Wikipedia, embedder, or LLM required)
.venv/bin/python knowledge/tests/answer_question.py
```
### Wikipedia candidate ranking
`WikipediaClient.search` returns `WikiSearchResult` objects with `title` and
plain-text `snippet` (MediaWiki's query-specific search excerpt, not the full
introductory summary). HTML highlight tags are removed and entities decoded;
missing snippets leave the title available for ranking without an extra fetch.
All searches finish before fetching articles. Results are deduplicated by title,
retaining distinct snippets from different queries. Every unique candidate is
ranked against the **original question**, using **title + snippet**, not the
composed search queries:
- BM25 lexical relevance (k1=1.5, b=0.75) over the candidate pool, using the
existing word tokenizer and stemmer.
- Cosine similarity from the configured embedding service (`embed_model` /
`embed_url`), reusing the cached question vector and batch-embedding candidates.
- Equal-weight reciprocal rank fusion, `1/(60 + lexical_rank) +
1/(60 + semantic_rank)`. Ties share ranks; final ties retain encounter order.
There is **no query-winner priority**. `max_articles` is applied only after global
ranking. If embeddings are unavailable or malformed, the failure is logged and
ranking falls back to BM25 alone. Search events retain titles and snippets;
`rank_articles` events record the question, candidates, ranking method, BM25,
cosine, and final scores. Snippets only guide retrieval; fetched-page containment
checks still govern which facts may enter the KG.
Offline API, ranking, embedding-validation and fallback tests:
```bash
.venv/bin/python knowledge/tests/semantic_fetch.py
```
## Viewing a run as traces (Jaeger, 2026-10-27)
Every run records `events.jsonl`; `floop/traces.py` replays it as one