Cohere
Rerank retrieved documents with Cohere
Send a query and candidate documents to Cohere’s Rerank API or use Cohere’s Python client. The response orders candidates by relevance and can return only the top results needed by a retrieval pipeline.
Documentation reviewed · 2 methods with sources
Ways to do it
API
POST /v2/rerank
Submit a query and list of documents, then read ordered results with relevance scores.
Read source: Cohere Rerank API v2- Requirements
- A Cohere bearer token, a supported rerank model, a query string, and candidate documents.
- Limitations
- Cohere recommends sending no more than 1,000 documents in one request. Documents longer than max_tokens_per_doc are truncated, with a default of 4,096 tokens.
Example shell
curl --fail -X POST https://api.cohere.com/v2/rerank \
-H "Authorization: Bearer $COHERE_API_KEY" -H "Content-Type: application/json" \
-d '{"model":"rerank-v5.0-pro","query":"incident response","documents":["Runbook A","Runbook B"],"top_n":2}'SDK
co.rerank()
Call the Python ClientV2 rerank method with the same query and candidate documents.
Read source: Cohere Rerank model overview- Requirements
- Python, the cohere package, a COHERE_API_KEY environment variable, and a supported rerank model.
- Limitations
- Set top_n to bound the result count. The returned indexes refer to documents in the original input order.
Example text
import cohere
co = cohere.ClientV2()
results = co.rerank(model="rerank-v5.0-pro", query="incident response", documents=["Runbook A", "Runbook B"], top_n=2)