fastrag.retrieval.language#
Retrieval that prefers documents in the language of the question.
A multilingual site holds each page once per language, and a multilingual
embedder and reranker score a page’s translations about equally. So the top
results for a question are often one passage repeated in several languages,
and the answer cites a translation in a language the reader did not write in.
LanguageAwareRetriever keeps one translation of each page, the one in
the question’s language when it was retrieved, and ranks documents in other
languages below documents of similar relevance in the question’s.
Classes
|
Prefer documents in the question's language, one translation per page. |
Names the language each text is written in. |
|
|
Detects languages offline with Lingua, choosing among a corpus's own. |
- class fastrag.retrieval.language.LanguageDetector[source]#
Bases:
ABCNames the language each text is written in.
- languages#
The ISO 639-1 codes
detect()can return. A URL path segment naming one of them is read as a locale (seeLanguageAwareRetriever).
- class fastrag.retrieval.language.LinguaLanguageDetector(languages, *, minimum_relative_distance=0.0)[source]#
Bases:
LanguageDetectorDetects languages offline with Lingua, choosing among a corpus’s own.
Needs the “language” extra (
lingua-language-detector). Naming only the languages the corpus is written in keeps short questions reliable: a short Catalan question is told apart from Spanish far more often when those are among a handful of candidates rather than 75.detector = LinguaLanguageDetector(["en", "es", "ca", "zh"]) detector.detect(["How can I obtain a credential?"]) # ["en"]
- Parameters:
- class fastrag.retrieval.language.LanguageAwareRetriever(*, retriever, detector, candidate_k=40, other_language_weight=0.8)[source]#
Bases:
RetrieverPrefer documents in the question’s language, one translation per page.
Over-fetches
candidate_kdocuments fromretriever, best first, and detects the language of the question and of each document (a document’s ownmetadata["language"]wins over detection). Then:Translations collapse. Documents whose
source_uridiffers only by a leading locale path segment naming one ofdetector.languages(/docs/a,/en/docs/a,/ca/docs/a) are copies of one page, and only one URL’s documents are kept: one written in the question’s language, preferably under its locale, if any was retrieved, else the best-ranked one. The same file served under every locale collapses too.Documents written in another language have their
scorescaled byother_language_weightand the rest are re-sorted, so they yield to documents of similar relevance in the question’s language but still outrank much weaker ones. Documents without a numericscorekeepretriever’s order.
The first
kare returned with theirrankrestated and theirmetadata["language"]set where known, so a prompt can show it. A question whose language is not detected skips step 2.retriever = LanguageAwareRetriever( retriever=RerankingRetriever(retriever=hybrid, reranker=reranker), detector=LinguaLanguageDetector(["en", "es", "ca", "zh"]), )
- Parameters:
retriever (Retriever) – Ranks the candidates. A
RerankingRetrieverreturns comparable scores across languages.detector (LanguageDetector) – Detects the question’s and the documents’ languages.
candidate_k (int) – How many documents to fetch from
retrieverbefore choosingk.other_language_weight (float) – Factor, in
(0, 1], applied to the score of a document written in another language than the question. A negative score is divided by it instead, so it always demotes.