fastrag.retrieval.reranking.cohere#

CohereReranker: reranking against any endpoint speaking the Cohere protocol.

Classes

CohereReranker(*, model[, base_url, ...])

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: Reranker

Rerank against any endpoint speaking the Cohere rerank protocol.

Posts JSON straight to {base_url}/rerank with httpx rather than importing the cohere SDK, so reranking needs no extra. LiteLLM, Jina, TEI, Infinity, and vLLM all speak the same {model, query, documents, top_n} request and results: [{index, relevance_score}] response — only the model name and base_url change. Returns Reranker: same HTTP contract, same error translation, adapted to return Document (with score/rank metadata).

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-m3 or rerank-v3.5.

  • base_url (str) – Endpoint base URL. Requests go to {base_url}/rerank — Cohere’s own API needs the /v1 already 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.

property model_id: str#

The configured model name.

async rerank(query, documents, *, top_k)[source]#

Score documents against query through 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:
Parameters:
Return type:

list[Document]

async close()[source]#

Close the shared HTTP client. Safe to call more than once.

Return type:

None