A flexible, adaptive classification system that allows for dynamic addition of new classes and continuous learning from examples. Built on top of transformers from HuggingFace, this library provides an easy-to-use interface for creating and updating text classifiers.
- π Works with any transformer classifier model
- π Continuous learning capabilities
- π― Dynamic class addition
- πΎ Safe and efficient state persistence
- π Prototype-based learning
- π§ Neural adaptation layer
pip install adaptive-classifier
from adaptive_classifier import AdaptiveClassifier
# Initialize with any HuggingFace model
classifier = AdaptiveClassifier("bert-base-uncased")
# Add some examples
texts = [
"The product works great!",
"Terrible experience",
"Neutral about this purchase"
]
labels = ["positive", "negative", "neutral"]
classifier.add_examples(texts, labels)
# Make predictions
predictions = classifier.predict("This is amazing!")
print(predictions) # [('positive', 0.85), ('neutral', 0.12), ('negative', 0.03)]
# Save the classifier
classifier.save("./my_classifier")
# Load it later
loaded_classifier = AdaptiveClassifier.load("./my_classifier")
# The library is also integrated with Hugging Face. So you can push and load from HF Hub.
# Save to Hub
classifier.push_to_hub("adaptive-classifier/model-name")
# Load from Hub
classifier = AdaptiveClassifier.from_pretrained("adaptive-classifier/model-name")
# Add a completely new class
new_texts = [
"Error code 404 appeared",
"System crashed after update"
]
new_labels = ["technical"] * 2
classifier.add_examples(new_texts, new_labels)
# Add more examples to existing classes
more_examples = [
"Best purchase ever!",
"Highly recommend this"
]
more_labels = ["positive"] * 2
classifier.add_examples(more_examples, more_labels)
The system combines three key components:
-
Transformer Embeddings: Uses state-of-the-art language models for text representation
-
Prototype Memory: Maintains class prototypes for quick adaptation to new examples
-
Adaptive Neural Layer: Learns refined decision boundaries through continuous training
- Python β₯ 3.8
- PyTorch β₯ 2.0
- transformers β₯ 4.30.0
- safetensors β₯ 0.3.1
- faiss-cpu β₯ 1.7.4 (or faiss-gpu for GPU support)
The adaptive classifier can also be used to predict optimal configurations for Language Models. Our research shows that model configurations, particularly temperature settings, can significantly impact response quality. Using the adaptive classifier, we can automatically predict the best temperature range for different types of queries:
- DETERMINISTIC (T: 0.0-0.1): For queries requiring precise, factual responses
- FOCUSED (T: 0.2-0.5): For structured, technical responses with slight flexibility
- BALANCED (T: 0.6-1.0): For natural, conversational responses
- CREATIVE (T: 1.1-1.5): For more varied and imaginative outputs
- EXPERIMENTAL (T: 1.6-2.0): For maximum variability and unconventional responses
Our evaluation on the LLM Arena dataset demonstrates:
- 69.8% success rate in finding optimal configurations
- Consistent response quality (avg. similarity score: 0.64)
- Balanced distribution across temperature classes, with each class finding its appropriate use cases
- BALANCED and CREATIVE temperatures producing the most reliable results (scores: 0.649 and 0.645)
This classifier can be used to automatically optimize LLM configurations based on query characteristics, leading to more consistent and higher-quality responses while reducing the need for manual configuration tuning.
from adaptive_classifier import AdaptiveClassifier
# Load the configuration optimizer
classifier = AdaptiveClassifier.from_pretrained("adaptive-classifier/llm-config-optimizer")
# Get optimal temperature class for a query
predictions = classifier.predict("Your query here")
# Returns: [('BALANCED', 0.85), ('CREATIVE', 0.10), ...]
The classifier continuously learns from new examples, adapting its predictions as it processes more queries and observes their performance.
The adaptive classifier can be used to intelligently route queries between different LLM models based on query complexity and requirements. The classifier learns to categorize queries into:
-
HIGH: Complex queries requiring advanced reasoning, multi-step problem solving, or deep expertise. Examples include:
- Code generation and debugging
- Complex analysis tasks
- Multi-step mathematical problems
- Technical explanations
- Creative writing tasks
-
LOW: Straightforward queries that can be handled by smaller, faster models. Examples include:
- Simple factual questions
- Basic clarifications
- Formatting tasks
- Short definitions
- Basic sentiment analysis
The router can be used to optimize costs and latency while maintaining response quality:
from adaptive_classifier import AdaptiveClassifier
# Load the router classifier
classifier = AdaptiveClassifier.from_pretrained("adaptive-classifier/llm-router")
# Get routing prediction for a query
predictions = classifier.predict("Write a function to calculate the Fibonacci sequence")
# Returns: [('HIGH', 0.92), ('LOW', 0.08)]
# Example routing logic
def route_query(query: str, classifier: AdaptiveClassifier):
predictions = classifier.predict(query)
top_class = predictions[0][0]
if top_class == 'HIGH':
return use_advanced_model(query) # e.g., GPT-4
else:
return use_basic_model(query) # e.g., GPT-3.5-Turbo
We evaluate the effectiveness of adaptive classification in optimizing LLM routing decisions. Using the arena-hard-auto-v0.1 dataset with 500 queries, we compared routing performance with and without adaptation while maintaining consistent overall success rates.
Metric | Without Adaptation | With Adaptation | Impact |
---|---|---|---|
High Model Routes | 113 (22.6%) | 98 (19.6%) | 0.87x |
Low Model Routes | 387 (77.4%) | 402 (80.4%) | 1.04x |
High Model Success Rate | 40.71% | 29.59% | 0.73x |
Low Model Success Rate | 16.54% | 20.15% | 1.22x |
Overall Success Rate | 22.00% | 22.00% | 1.00x |
Cost Savings* | 25.60% | 32.40% | 1.27x |
*Cost savings calculation assumes high-cost model is 2x the cost of low-cost model
The results highlight several key benefits of adaptive classification:
-
Improved Cost Efficiency: While maintaining the same overall success rate (22%), the adaptive classifier achieved 32.40% cost savings compared to 25.60% without adaptation - a relative improvement of 1.27x in cost efficiency.
-
Better Resource Utilization: The adaptive system routed more queries to the low-cost model (402 vs 387) while reducing high-cost model usage (98 vs 113), demonstrating better resource allocation.
-
Learning from Experience: Through adaptation, the system improved the success rate of low-model routes from 16.54% to 20.15% (1.22x increase), showing effective learning from successful cases.
-
ROI on Adaptation: The system adapted to 110 new examples during evaluation, leading to a 6.80% improvement in cost savings while maintaining quality - demonstrating significant return on the adaptation investment.
This real-world evaluation demonstrates that adaptive classification can significantly improve cost efficiency in LLM routing without compromising overall performance.
- RouteLLM: Learning to Route LLMs with Preference Data
- Transformer^2: Self-adaptive LLMs
- Lamini Classifier Agent Toolkit
- Protoformer: Embedding Prototypes for Transformers
- Overcoming catastrophic forgetting in neural networks
If you use this library in your research, please cite:
@software{adaptive_classifier,
title = {Adaptive Classifier: Dynamic Text Classification with Continuous Learning},
author = {Asankhaya Sharma},
year = {2025},
publisher = {GitHub},
url = {https://github.com/codelion/adaptive-classifier}
}