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
|
|
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# Adaptive Classifier
|
| 11 |
+
|
| 12 |
+
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.
|
| 13 |
+
|
| 14 |
+
## Features
|
| 15 |
+
|
| 16 |
+
- π Works with any transformer classifier model
|
| 17 |
+
- π Continuous learning capabilities
|
| 18 |
+
- π― Dynamic class addition
|
| 19 |
+
- πΎ Safe and efficient state persistence
|
| 20 |
+
- π Prototype-based learning
|
| 21 |
+
- π§ Neural adaptation layer
|
| 22 |
+
|
| 23 |
+
## Try Now
|
| 24 |
+
|
| 25 |
+
| Use Case | Demonstrates | Link |
|
| 26 |
+
|----------|----------|-------|
|
| 27 |
+
| Basic Example (Cat or Dog) | Continuous learning | [](https://colab.research.google.com/drive/1Zmvtb3XUFtUImEmYdKpkuqmxKVlRxzt9?usp=sharing) |
|
| 28 |
+
| Support Ticket Classification| Realistic examples | [](https://colab.research.google.com/drive/1yeVCi_Cdx2jtM7HI0gbU6VlZDJsg_m8u?usp=sharing) |
|
| 29 |
+
| Query Classification | Different configurations | [](https://colab.research.google.com/drive/1b2q303CLDRQAkC65Rtwcoj09ovR0mGwz?usp=sharing) |
|
| 30 |
+
| Multilingual Sentiment Analysis | Ensemble of classifiers | [](https://colab.research.google.com/drive/14tfRi_DtL-QgjBMgVRrsLwcov-zqbKBl?usp=sharing) |
|
| 31 |
+
| Product Category Classification | Batch processing | [](https://colab.research.google.com/drive/1VyxVubB8LXXES6qElEYJL241emkV_Wxc?usp=sharing) |
|
| 32 |
+
|
| 33 |
+
## Installation
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
pip install adaptive-classifier
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
## Quick Start
|
| 40 |
+
|
| 41 |
+
```python
|
| 42 |
+
from adaptive_classifier import AdaptiveClassifier
|
| 43 |
+
|
| 44 |
+
# Initialize with any HuggingFace model
|
| 45 |
+
classifier = AdaptiveClassifier("bert-base-uncased")
|
| 46 |
+
|
| 47 |
+
# Add some examples
|
| 48 |
+
texts = [
|
| 49 |
+
"The product works great!",
|
| 50 |
+
"Terrible experience",
|
| 51 |
+
"Neutral about this purchase"
|
| 52 |
+
]
|
| 53 |
+
labels = ["positive", "negative", "neutral"]
|
| 54 |
+
|
| 55 |
+
classifier.add_examples(texts, labels)
|
| 56 |
+
|
| 57 |
+
# Make predictions
|
| 58 |
+
predictions = classifier.predict("This is amazing!")
|
| 59 |
+
print(predictions) # [('positive', 0.85), ('neutral', 0.12), ('negative', 0.03)]
|
| 60 |
+
|
| 61 |
+
# Save the classifier
|
| 62 |
+
classifier.save("./my_classifier")
|
| 63 |
+
|
| 64 |
+
# Load it later
|
| 65 |
+
loaded_classifier = AdaptiveClassifier.load("./my_classifier")
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
## Advanced Usage
|
| 69 |
+
|
| 70 |
+
### Adding New Classes Dynamically
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
# Add a completely new class
|
| 74 |
+
new_texts = [
|
| 75 |
+
"Error code 404 appeared",
|
| 76 |
+
"System crashed after update"
|
| 77 |
+
]
|
| 78 |
+
new_labels = ["technical"] * 2
|
| 79 |
+
|
| 80 |
+
classifier.add_examples(new_texts, new_labels)
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
### Continuous Learning
|
| 84 |
+
|
| 85 |
+
```python
|
| 86 |
+
# Add more examples to existing classes
|
| 87 |
+
more_examples = [
|
| 88 |
+
"Best purchase ever!",
|
| 89 |
+
"Highly recommend this"
|
| 90 |
+
]
|
| 91 |
+
more_labels = ["positive"] * 2
|
| 92 |
+
|
| 93 |
+
classifier.add_examples(more_examples, more_labels)
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
## How It Works
|
| 97 |
+
|
| 98 |
+
The system combines three key components:
|
| 99 |
+
|
| 100 |
+
1. **Transformer Embeddings**: Uses state-of-the-art language models for text representation
|
| 101 |
+
|
| 102 |
+
2. **Prototype Memory**: Maintains class prototypes for quick adaptation to new examples
|
| 103 |
+
|
| 104 |
+
3. **Adaptive Neural Layer**: Learns refined decision boundaries through continuous training
|
| 105 |
+
|
| 106 |
+
## Requirements
|
| 107 |
+
|
| 108 |
+
- Python β₯ 3.8
|
| 109 |
+
- PyTorch β₯ 2.0
|
| 110 |
+
- transformers β₯ 4.30.0
|
| 111 |
+
- safetensors β₯ 0.3.1
|
| 112 |
+
- faiss-cpu β₯ 1.7.4 (or faiss-gpu for GPU support)
|
| 113 |
+
|
| 114 |
+
## Benefits of Adaptive Classification in LLM Routing
|
| 115 |
+
|
| 116 |
+
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.
|
| 117 |
+
|
| 118 |
+
### Key Results
|
| 119 |
+
|
| 120 |
+
| Metric | Without Adaptation | With Adaptation | Impact |
|
| 121 |
+
|--------|-------------------|-----------------|---------|
|
| 122 |
+
| High Model Routes | 113 (22.6%) | 98 (19.6%) | 0.87x |
|
| 123 |
+
| Low Model Routes | 387 (77.4%) | 402 (80.4%) | 1.04x |
|
| 124 |
+
| High Model Success Rate | 40.71% | 29.59% | 0.73x |
|
| 125 |
+
| Low Model Success Rate | 16.54% | 20.15% | 1.22x |
|
| 126 |
+
| Overall Success Rate | 22.00% | 22.00% | 1.00x |
|
| 127 |
+
| Cost Savings* | 25.60% | 32.40% | 1.27x |
|
| 128 |
+
|
| 129 |
+
*Cost savings calculation assumes high-cost model is 2x the cost of low-cost model
|
| 130 |
+
|
| 131 |
+
### Analysis
|
| 132 |
+
|
| 133 |
+
The results highlight several key benefits of adaptive classification:
|
| 134 |
+
|
| 135 |
+
1. **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.
|
| 136 |
+
|
| 137 |
+
2. **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.
|
| 138 |
+
|
| 139 |
+
3. **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.
|
| 140 |
+
|
| 141 |
+
4. **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.
|
| 142 |
+
|
| 143 |
+
This real-world evaluation demonstrates that adaptive classification can significantly improve cost efficiency in LLM routing without compromising overall performance.
|
| 144 |
+
|
| 145 |
+
## References
|
| 146 |
+
|
| 147 |
+
- [RouteLLM: Learning to Route LLMs with Preference Data](https://arxiv.org/abs/2406.18665)
|
| 148 |
+
- [Transformer^2: Self-adaptive LLMs](https://arxiv.org/abs/2501.06252)
|
| 149 |
+
- [Lamini Classifier Agent Toolkit](https://www.lamini.ai/blog/classifier-agent-toolkit)
|
| 150 |
+
- [Protoformer: Embedding Prototypes for Transformers](https://arxiv.org/abs/2206.12710)
|
| 151 |
+
- [Overcoming catastrophic forgetting in neural networks](https://arxiv.org/abs/1612.00796)
|
| 152 |
+
|
| 153 |
+
## Citation
|
| 154 |
+
|
| 155 |
+
If you use this library in your research, please cite:
|
| 156 |
+
|
| 157 |
+
```bibtex
|
| 158 |
+
@software{adaptive_classifier,
|
| 159 |
+
title = {Adaptive Classifier: Dynamic Text Classification with Continuous Learning},
|
| 160 |
+
author = {Asankhaya Sharma},
|
| 161 |
+
year = {2025},
|
| 162 |
+
publisher = {GitHub},
|
| 163 |
+
url = {https://github.com/codelion/adaptive-classifier}
|
| 164 |
+
}
|
| 165 |
+
```
|