LA24AI

Learning & research

Sentence Transformers: test retrieval before adding generated answers

Build a known question-and-passage set, measure whether the correct passage appears in the top results, and separate retrieval, reranking and answer generation.

Sentence Transformers: test retrieval before adding generated answers

Semantic retrieval is not answer generation

Sentence Transformers provides text embeddings and retrieval tooling, and documents a retrieve-then-rerank pattern. Retrieval produces candidates; reranking evaluates question-passage pairs. Neither stage makes an unsupported document fact true.

Start with ten short passages and questions whose evidence location is known. Leave out generation initially. A polished answer cannot repair a retrieval stage that never supplied the relevant passage.

Write accepted evidence before looking at rankings

Give each passage a stable ID and record accepted evidence IDs for each question. The example calculates hit@3 on synthetic rankings: whether at least one accepted passage appears in the first three. It is a specific limited metric, not a measure of general understanding.

Include literal wording and paraphrases. For Arabic, add mixed-script names and alternative spellings. A model that works on English is not automatically suitable for Arabic just because the library accepts Unicode input.

A concrete examplepython
cases = [
    {"accepted": {"p2"}, "ranked": ["p8", "p2", "p4"]},
    {"accepted": {"p5"}, "ranked": ["p1", "p3", "p7"]},
    {"accepted": {"p9", "p10"}, "ranked": ["p9", "p4", "p6"]},
]
hits = sum(bool(case["accepted"] & set(case["ranked"][:3]))
           for case in cases)
print(f"hit@3: {hits}/{len(cases)} = {hits / len(cases):.1%}")

Two of three synthetic cases contain accepted evidence in the first three: 66.7%. This is not a vendor benchmark.

Download example text ↓

Select the representation before scaling the index

Check the model card for languages, input formatting and text-length limits. Truncation can remove the end of a passage, including its answer. That is a data-preparation problem rather than a defect in the search interface.

Do not combine document vectors from one model with query vectors from another. Equal dimensions do not establish a shared vector space. Save the embedding-model identity with the index so updating a file cannot silently change retrieval semantics.

Measure the reranking tradeoff

A reranker commonly scores query-passage pairs from a candidate set. Increase candidates when evidence is absent, but measure cost before unlimited expansion. Better ordering of ten candidates cannot recover a passage that was never retrieved.

Keep rankings before and after reranking. Inspect improved and regressed questions instead of publishing one aggregate metric without its test set. Average improvement can hide worse handling of the Arabic terminology most important to your readers.

Generate only after evidence is available

Once relevant passages reach the answer stage, require traceable citations. Include an unanswerable question too. The system should recognize missing evidence instead of choosing a vaguely related passage and guessing.

No external embedding model or library was benchmarked here. The Python example is a local calculation on educational data. Replace the synthetic lists with your own measured rankings to compare retrieval stages without claiming tests you have not performed.

Sources & further reading

Sources support concepts and features; proposed trials are educational examples.

Prepared with AI assistance. Educational examples are not product tests; actual measurements identify their method and scope. Editorial approach ↗

NEXT

One idea leads to another

3 min read

Distinguish vector space from vector length, retain the index model identity, and test a versioned migration instead of mixing incompatible representations.

3 min read

Give evidence a document ID, revision and passage boundaries, then reject citations that do not support the adjacent claim even when their links work.