Reranking

Reranking sorts a list of documents by how well each one answers a question. Use it to pick the best few results before you pass them to a chat model.

Rerank a shortlist

Send a query and the documents to score. Results come back best first, and each index points to the document's place in your list. Use top_n to keep only the best few.

from pendra import Pendra

client = Pendra(api_key="pdr_sk_...")

result = client.rerank.create(
    model="bge-reranker-v2-m3",
    query="How do I rotate an API key?",
    documents=[
        "Billing is invoiced monthly.",
        "Create a new key, then revoke the old one.",
        "API keys are shown only once at creation.",
    ],
    top_n=2,
)
for r in result.results:
    print(r.index, round(r.relevance_score, 3))
Response
1 8.734    # "Create a new key, then revoke the old one."
0 -4.219   # "Billing is invoiced monthly."

A higher score means more relevant. Scores only make sense compared with others in the same request, and each model has its own scale.

Search with embeddings and reranking

  1. Use embeddings to find the 20 to 50 closest documents.
  2. Rerank them against the question and keep the top few.
  3. Pass those to a chat model as context.

Reranking is more accurate than embeddings, but slower, which is why it sorts a shortlist rather than everything you have.

Models

bge-reranker-v2-m3 works in many languages and handles long documents. See all rerank models in the models list.

For every field and response, see the Rerank API reference.