Reranking is a powerful technique that enhances the relevance of search results. It works by taking a query and a list of documents, then scoring each document based on how relevant it is to the query. This is especially useful in multi-step search pipelines where you:

  1. Use a traditional or vector-based search system to retrieve candidate documents.
  2. Use the Rerank API to reorder those candidates based on relevance.

Reranking helps boost precision in semantic search, chatbots, RAG systems, and knowledge retrieval systems.

The reranker takes:

  • A query (user input or search question)
  • A list of documents (initially retrieved search results)
  • Returns a ranked list with relevance scores

Code Snippet

import cohere

BASE_URL = "https://{controlPlaneUrl}/api/llm"
API_KEY = "your-truefoundry-api-key"

# Configure Cohere client with TrueFoundry settings
client = cohere.ClientV2(
	api_key=API_KEY,
	base_url=BASE_URL
)

response = client.rerank(
    model="test-ss-cohere/rerank-multilingual-v3-0",
    query="Where is New Delhi?",
    documents=["New Delhi is capital of India","The sky is blue"],
    top_n=1,
)

print(response)

Expected Output

{
  "id": "a9279c7a-1e34-4bee-9892-db1d38ca9be0",
  "results": [
    {
      "index": 0,
      "relevance_score": 0.783542
    }
  ]
}

In this example, the document “New Delhi is the capital of India.” has a higher relevance score to the query compared to “The sky is blue.”, and is therefore ranked higher.