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))
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
- Use embeddings to find the 20 to 50 closest documents.
- Rerank them against the question and keep the top few.
- 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.