fastrag.retrieval.reranking.cohere#
CohereReranker: reranking against any endpoint speaking the Cohere protocol.
Classes
|
Rerank against any endpoint speaking the Cohere rerank protocol. |
- class fastrag.retrieval.reranking.cohere.CohereReranker(*, model, base_url='https://api.cohere.com/v1', api_key, timeout=30.0, extra_body=None)[source]#
Bases:
RerankerRerank against any endpoint speaking the Cohere rerank protocol.
Posts JSON straight to
{base_url}/rerankwithhttpxrather than importing thecohereSDK, so reranking needs no extra. LiteLLM, Jina, TEI, Infinity, and vLLM all speak the same{model, query, documents, top_n}request andresults: [{index, relevance_score}]response — only the model name andbase_urlchange. ReturnsReranker: same HTTP contract, same error translation, adapted to returnDocument(withscore/rankmetadata).import os from fastrag.retrieval import CohereReranker reranker = CohereReranker( model="rerank-v3.5", base_url=os.environ["RERANK_BASE_URL"], api_key=os.environ["COHERE_API_KEY"], )
- Parameters:
model (str) – Model name, e.g.
bge-reranker-v2-m3orrerank-v3.5.base_url (str) – Endpoint base URL. Requests go to
{base_url}/rerank— Cohere’s own API needs the/v1already carried in the default; a server that mounts the route elsewhere needs the full prefix here.api_key (str) – Bearer token.
extra_body (Mapping[str, Any] | None) – Additional JSON request fields; matching fields override defaults.
timeout (float) – Seconds per call.
- async rerank(query, documents, *, top_k)[source]#
Score
documentsagainstquerythrough the rerank endpoint.No provider exception escapes: everything is translated into the Fastrag hierarchy, with timeouts, rate limits, and server errors marked transient so a caller wrapping this in retries gets correct behaviour for free.
- Raises:
fastrag.exceptions.ConfigurationError –
top_kis less than 1.fastrag.exceptions.RerankError – The request failed non-transiently, or the response was malformed.
fastrag.exceptions.TransientRerankError – The request failed for a reason that may succeed on retry.
- Parameters:
- Return type: