Skip to main content

Rerank

Rerank models sort text passages / documents by semantic relevance to a specific query. They are often used to sort search results returned by an existing search solution.

Let's try to identify the 3 most relevant documents for a specific question:

import requests

def rerank_openai(query, documents, api_base, model, api_key=None, top_n=None):
url = f"{api_base}/v1/rerank"
headers = {
"Content-Type": "application/json"
}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
payload = {
"model": model,
"query": query,
"documents": documents,
}
if top_n is not None:
payload["top_n"] = top_n

response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.json()

The benefits of reranking are easier to understand when working with a large array of documents. We've prepared a special guide where we improve keyword search accuracy using CompressaRerank.