Forge better rankings from candidate documents with LLM reranking.
Project description
ranksmith
Forge better rankings from candidate documents.
ranksmith is a small Python package for LLM-based reranking. Version 1 focuses
on Azure OpenAI powered zero-shot listwise reranking for candidate documents.
Install
pip install ranksmith
Quick Start
from ranksmith import AzureOpenAIReranker, Document
reranker = AzureOpenAIReranker(
api_key="...",
azure_endpoint="https://example.openai.azure.com",
azure_deployment="gpt-4o-mini",
)
results = reranker.rerank(
query="What is listwise reranking?",
documents=[
Document(id="a", text="Listwise reranking compares candidates together."),
Document(id="b", text="Vector search retrieves candidate documents."),
],
top_k=2,
)
for result in results:
print(result.rank, result.original_index, result.document.id)
rank is 1-based for display. original_index is 0-based so it maps back to
the input list.
Strategy
The default strategy is listwise reranking with automatic sliding-window handling for longer candidate lists.
from ranksmith import ListwiseStrategy
strategy = ListwiseStrategy(
algorithm="sliding_window",
window_size=20,
stride=10,
max_document_chars=4000,
)
Version 1 supports direct and sliding_window algorithms. Pointwise,
pairwise, tournament, bayesian, and confidence-style algorithms are left for
future versions.
Async Support
ranksmith provides first-class asynchronous support for high-throughput environments like FastAPI.
from ranksmith import AsyncAzureOpenAIReranker
reranker = AsyncAzureOpenAIReranker(
api_key="...",
azure_endpoint="https://example.openai.azure.com",
azure_deployment="gpt-4o-mini",
)
results = await reranker.rerank("query", documents)
Examples
Ready-to-use example code for integrating the RankGPT algorithm into your production environment can be found in the examples/ directory.
examples/rankgpt_sync.py: Synchronous RankGPT integration guideexamples/rankgpt_async.py: High-performance asynchronous RankGPT integration guide
Result Model
result.document # Document
result.rank # 1-based rank
result.original_index # 0-based input index
result.metadata # strategy-specific metadata
Error Handling
ranksmith fails fast. It does not silently truncate long documents, repair
invalid rankings, or return unvalidated LLM output.
from ranksmith import DocumentTooLongError, RerankParseError, RerankProviderError
try:
results = reranker.rerank("query", documents)
except DocumentTooLongError:
...
except RerankParseError:
...
except RerankProviderError:
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ranksmith-0.1.0.tar.gz.
File metadata
- Download URL: ranksmith-0.1.0.tar.gz
- Upload date:
- Size: 1.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee75e150364abdc9af646ca46af134d479912da7716e1a0b580efdcb4c06bdbc
|
|
| MD5 |
e99200a54433e722ef568de516c4a50d
|
|
| BLAKE2b-256 |
1f05341ca7f85c0770f96b59ce23e9bc0ace0a2f8a9315b81fe7b998dd0b9b21
|
File details
Details for the file ranksmith-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ranksmith-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ffb7a9ad9c914047dcf0101e14475b5c0107d2f9706af363f34ff6a7c485ee0
|
|
| MD5 |
fb7da038f4d69e43eac1711f8a12824d
|
|
| BLAKE2b-256 |
03e90b50d2923941dd0309593cfe6e23878a0641acd38c3cdf4b01196a3c1c66
|