Ultra-fast, lightweight ONNX query difficulty classifier for pre-routing LLM cascades
Project description
QueryClassifier
Goal: A pip-installable Python library that classifies a text query into low / medium / hard complexity, for use as a pre-routing step in an LLM cascade (cheap model → medium model → frontier model).
Approach: Fine-tuned answerdotai/ModernBERT-large quantized into int8 ONNX format (query-classifier-onnx). Uses a FastEmbed-inspired architecture powered by pure ONNXRuntime + Rust tokenizers — zero PyTorch, zero transformers library dependencies, and zero heavy GPU requirements.
⚡ Quickstart
Installation
pip install queryclf
Dependencies installed are lightweight (~38MB total): onnxruntime, tokenizers, numpy, huggingface-hub, and python-dotenv.
⚙️ Configuration (.env)
Configure model paths dynamically in .env (refer to .env.example):
# Hugging Face Repository ID (Default: prvn-ramesh/query-classifier-onnx)
QUERY_CLASSIFIER_HF_REPO=prvn-ramesh/query-classifier-onnx
# Optional local model directory override
QUERY_CLASSIFIER_MODEL_PATH=
Resolution Priority:
- Explicit Python Argument (
hf_repo="..."ormodel_path="...") - Environment Variable
QUERY_CLASSIFIER_HF_REPO - Environment Variable
QUERY_CLASSIFIER_MODEL_PATH - Default Hugging Face Repository (
prvn-ramesh/query-classifier-onnx)
🐍 Python API Usage
import asyncio
from query_classifier import QueryClassifier
# Initializes classifier with built-in LRU cache and CPU thread tuning
classifier = QueryClassifier(
cache_size=1000, # Max items in LRU query cache (default: 1000)
intra_op_num_threads=4, # Intra-node CPU parallelism
)
# 1. Single query prediction
result = classifier.predict("Can you summarize this document?")
print(result.label) # Output: "low"
print(result.confidence) # Output: 0.5779
print(result.latency_ms) # Output: 29.8 ms
print(result.scores) # Output: {'low': 0.5779, 'medium': 0.4201, 'hard': 0.0020}
# 2. Batch query prediction
queries = [
"What is the capital of France?",
"Calculate the integral of x^2 * sin(x) dx",
"Write a lock-free multi-threaded queue in C++ using atomics"
]
batch_results = classifier.predict_batch(queries)
for q, res in zip(queries, batch_results):
print(f"[{res.label.upper()}] ({res.latency_ms:.1f}ms) -> {q}")
# 3. Async prediction (for FastAPI / AsyncIO frameworks)
async def main():
async_res = await classifier.predict_async("Explain quantum computing")
print(f"Async prediction: {async_res.label} ({async_res.confidence:.2f})")
asyncio.run(main())
# 4. Cache statistics & management
print(classifier.cache_info()) # e.g., {'hits': 1, 'misses': 4, 'maxsize': 1000, 'currsize': 4}
classifier.clear_cache()
💻 CLI Command Usage
# Single query classification
queryclf "What is 2 + 2?"
# JSON output mode
queryclf "Write a Rust lock-free SPMC queue" --json
# Batch mode from file
queryclf --file queries.txt
📊 Benchmark & Accuracy Results
The underlying model prvn-ramesh/query-classifier-onnx is evaluated on an independent held-out test set of 301 unseen queries (balanced across low, medium, and hard buckets). For full details, see the Hugging Face Model README.
Accuracy & F1-Score Breakdown
- Overall Accuracy: 88.04%
- Macro F1-Score: 0.8797
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
low |
100.00% | 81.00% | 0.8950 | 100 |
medium |
82.18% | 83.00% | 0.8259 | 100 |
hard |
84.87% | 100.00% | 0.9182 | 101 |
| Overall | 88.04% Acc | — | 0.8797 Macro-F1 | 301 total |
Latency & Throughput Profile (CPU Execution)
| Metric | Measurement |
|---|---|
| Median Latency (p50) | 67.68 ms |
| Mean Latency | 81.44 ms |
| p90 Latency | 134.69 ms |
| p99 Latency | 161.74 ms |
| Throughput | 12.3 queries/sec |
🛠️ Project Roadmap Status
- Phase 0 — Environment & Scaffold: Package directory layout (
src/query_classifier,tests/,pyproject.toml). - Phase 2 — Labeled Dataset:
dataset.csvwith query samples. - Phase 4 & 5 — Fine-Tuned ModernBERT & ONNX int8 Quantization: Quantized model published on Hugging Face at
prvn-ramesh/query-classifier-onnx. - Phase 6 — Inference Engine: Pure ONNXRuntime + Tokenizers inference engine (
onnx_engine.py,classifier.py,cli.py).
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 queryclf-0.1.0.tar.gz.
File metadata
- Download URL: queryclf-0.1.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c1042a025347e5f53e352521cb1d0f5b520548a04ba72c8518c68dedd13a0ba
|
|
| MD5 |
f713960fe86a13c32b9eb0caf5d3b769
|
|
| BLAKE2b-256 |
7bf49ad814cae1a66a5a327f218a65a365ceb27d2b348c3fa6d04be48385b7d2
|
Provenance
The following attestation bundles were made for queryclf-0.1.0.tar.gz:
Publisher:
release.yml on prvn-ramesh/QueryClassifier
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
queryclf-0.1.0.tar.gz -
Subject digest:
1c1042a025347e5f53e352521cb1d0f5b520548a04ba72c8518c68dedd13a0ba - Sigstore transparency entry: 2246092245
- Sigstore integration time:
-
Permalink:
prvn-ramesh/QueryClassifier@7e2109bc1f3d22485bd3dc67c14dc9b00a910463 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/prvn-ramesh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7e2109bc1f3d22485bd3dc67c14dc9b00a910463 -
Trigger Event:
push
-
Statement type:
File details
Details for the file queryclf-0.1.0-py3-none-any.whl.
File metadata
- Download URL: queryclf-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7f99dc0cbcc2818f936e3351419b4c07f2ed40926ad846a1a52e22a0e81c92c
|
|
| MD5 |
2074f8f2fb762885ee8eb33d06f212c4
|
|
| BLAKE2b-256 |
f3f7dcca0a8bc57fdc749f740fc520ad44f7dda1ec54629573593be01729c892
|
Provenance
The following attestation bundles were made for queryclf-0.1.0-py3-none-any.whl:
Publisher:
release.yml on prvn-ramesh/QueryClassifier
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
queryclf-0.1.0-py3-none-any.whl -
Subject digest:
c7f99dc0cbcc2818f936e3351419b4c07f2ed40926ad846a1a52e22a0e81c92c - Sigstore transparency entry: 2246092729
- Sigstore integration time:
-
Permalink:
prvn-ramesh/QueryClassifier@7e2109bc1f3d22485bd3dc67c14dc9b00a910463 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/prvn-ramesh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@7e2109bc1f3d22485bd3dc67c14dc9b00a910463 -
Trigger Event:
push
-
Statement type: