Reranking - quickstart

Cohere’s reranking models are available via the Rerank endpoint. This endpoint provides a powerful semantic boost to the search quality of any keyword or vector search system.

This quickstart guide shows you how to perform reranking with the Rerank endpoint.

1

Setup

First, install the Cohere Python SDK with the following command.

$pip install -U cohere

Next, import the library and create a client.

PYTHON
1import cohere
2
3co = cohere.ClientV2(
4 "COHERE_API_KEY"
5) # Get your free API key here: https://dashboard.cohere.com/api-keys
2

Retrieved Documents

First, define the list of documents to be reranked.

PYTHON
1documents = [
2 "Reimbursing Travel Expenses: Easily manage your travel expenses by submitting them through our finance tool. Approvals are prompt and straightforward.",
3 "Working from Abroad: Working remotely from another country is possible. Simply coordinate with your manager and ensure your availability during core hours.",
4 "Health and Wellness Benefits: We care about your well-being and offer gym memberships, on-site yoga classes, and comprehensive health insurance.",
5 "Performance Reviews Frequency: We conduct informal check-ins every quarter and formal performance reviews twice a year.",
6]
3

Reranking

Then, perform reranking by passing the documents and the user query to the Rerank endpoint.

PYTHON
1# Add the user query
2query = "Are there fitness-related perks?"
3
4# Rerank the documents
5results = co.rerank(
6 model="rerank-v3.5", query=query, documents=documents, top_n=2
7)
8
9for result in results.results:
10 print(result)

Further Resources

Built with