{"id":28561108,"url":"https://github.com/codelion/adaptive-classifier","last_synced_at":"2025-06-10T10:14:17.726Z","repository":{"id":272207400,"uuid":"915830201","full_name":"codelion/adaptive-classifier","owner":"codelion","description":"A flexible, adaptive classification system for dynamic text classification","archived":false,"fork":false,"pushed_at":"2025-06-08T15:09:31.000Z","size":3439,"stargazers_count":203,"open_issues_count":0,"forks_count":14,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-08T16:20:57.965Z","etag":null,"topics":["adaptive-learning","adaptive-neural-network","bert","classifier","continous-learning","distilbert","elastic-weight-consolidation","embeddings","faiss","large-language-models","llms","machine-learning","multi-class-classification","multi-label-classification","neural-layers","neural-networks","online-learning","roberta","text-classification","transformers"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codelion.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-01-12T22:49:54.000Z","updated_at":"2025-06-08T15:09:07.000Z","dependencies_parsed_at":"2025-01-12T23:29:38.250Z","dependency_job_id":"818c2079-4258-447e-b8e1-fbf42293ad74","html_url":"https://github.com/codelion/adaptive-classifier","commit_stats":null,"previous_names":["codelion/adaptive-classifier"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codelion%2Fadaptive-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codelion%2Fadaptive-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codelion%2Fadaptive-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codelion%2Fadaptive-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codelion","download_url":"https://codeload.github.com/codelion/adaptive-classifier/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codelion%2Fadaptive-classifier/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258783514,"owners_count":22757274,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adaptive-learning","adaptive-neural-network","bert","classifier","continous-learning","distilbert","elastic-weight-consolidation","embeddings","faiss","large-language-models","llms","machine-learning","multi-class-classification","multi-label-classification","neural-layers","neural-networks","online-learning","roberta","text-classification","transformers"],"created_at":"2025-06-10T10:14:16.934Z","updated_at":"2025-06-10T10:14:17.694Z","avatar_url":"https://github.com/codelion.png","language":"Python","readme":"# Adaptive Classifier\n\nA 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.\n\n[![GitHub Discussions](https://img.shields.io/github/discussions/codelion/adaptive-classifier)](https://github.com/codelion/adaptive-classifier/discussions)\n\n## Features\n\n- 🚀 Works with any transformer classifier model\n- 📈 Continuous learning capabilities\n- 🎯 Dynamic class addition\n- 💾 Safe and efficient state persistence\n- 🔄 Prototype-based learning\n- 🧠 Neural adaptation layer\n\n## Try Now\n\n| Use Case | Demonstrates | Link |\n|----------|----------|-------|\n| Basic Example (Cat or Dog)  | Continuous learning | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1Zmvtb3XUFtUImEmYdKpkuqmxKVlRxzt9?usp=sharing) |\n| Support Ticket Classification| Realistic examples | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1yeVCi_Cdx2jtM7HI0gbU6VlZDJsg_m8u?usp=sharing) |\n| Query Classification  | Different configurations | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1b2q303CLDRQAkC65Rtwcoj09ovR0mGwz?usp=sharing) |\n| Multilingual Sentiment Analysis | Ensemble of classifiers | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/14tfRi_DtL-QgjBMgVRrsLwcov-zqbKBl?usp=sharing) |\n| Product Category Classification | Batch processing | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1VyxVubB8LXXES6qElEYJL241emkV_Wxc?usp=sharing) |\n| Multi-label Classification | Extensibility | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1MDL_45QWvGoM2N8NRfUQSy2J7HKmmTsv?usp=sharing) |\n\n## Installation\n\n```bash\npip install adaptive-classifier\n```\n\n## Quick Start\n\n```python\nfrom adaptive_classifier import AdaptiveClassifier\n\n# Initialize with any HuggingFace model\nclassifier = AdaptiveClassifier(\"bert-base-uncased\")\n\n# Add some examples\ntexts = [\n    \"The product works great!\",\n    \"Terrible experience\",\n    \"Neutral about this purchase\"\n]\nlabels = [\"positive\", \"negative\", \"neutral\"]\n\nclassifier.add_examples(texts, labels)\n\n# Make predictions\npredictions = classifier.predict(\"This is amazing!\")\nprint(predictions)  # [('positive', 0.85), ('neutral', 0.12), ('negative', 0.03)]\n\n# Save the classifier\nclassifier.save(\"./my_classifier\")\n\n# Load it later\nloaded_classifier = AdaptiveClassifier.load(\"./my_classifier\")\n\n# The library is also integrated with Hugging Face. So you can push and load from HF Hub.\n\n# Save to Hub\nclassifier.push_to_hub(\"adaptive-classifier/model-name\")\n\n# Load from Hub\nclassifier = AdaptiveClassifier.from_pretrained(\"adaptive-classifier/model-name\")\n```\n\n## Advanced Usage\n\n### Adding New Classes Dynamically\n\n```python\n# Add a completely new class\nnew_texts = [\n    \"Error code 404 appeared\",\n    \"System crashed after update\"\n]\nnew_labels = [\"technical\"] * 2\n\nclassifier.add_examples(new_texts, new_labels)\n```\n\n### Continuous Learning\n\n```python\n# Add more examples to existing classes\nmore_examples = [\n    \"Best purchase ever!\",\n    \"Highly recommend this\"\n]\nmore_labels = [\"positive\"] * 2\n\nclassifier.add_examples(more_examples, more_labels)\n```\n\n## How It Works\n\nThe system combines three key components:\n\n1. **Transformer Embeddings**: Uses state-of-the-art language models for text representation\n\n2. **Prototype Memory**: Maintains class prototypes for quick adaptation to new examples\n\n3. **Adaptive Neural Layer**: Learns refined decision boundaries through continuous training\n\n## Requirements\n\n- Python ≥ 3.8\n- PyTorch ≥ 2.0\n- transformers ≥ 4.30.0\n- safetensors ≥ 0.3.1\n- faiss-cpu ≥ 1.7.4 (or faiss-gpu for GPU support)\n\n## Adaptive Classification with LLMs\n\n### Hallucination Detector\n\nThe adaptive classifier can detect hallucinations in language model outputs, especially in Retrieval-Augmented Generation (RAG) scenarios. Despite incorporating external knowledge sources, LLMs often still generate content that isn't supported by the provided context. Our hallucination detector identifies when a model's output contains information that goes beyond what's present in the source material.\n\nThe classifier categorizes text into:\n\n- **HALLUCINATED**: Output contains information not supported by or contradictory to the provided context\n- **NOT_HALLUCINATED**: Output is faithfully grounded in the provided context\n\nOur hallucination detector has been trained and evaluated on the RAGTruth benchmark, which provides a standardized dataset for assessing hallucination detection across different task types:\n\n#### Performance Across Tasks\n\n| Task Type      | Precision | Recall | F1 Score |\n|----------------|-----------|--------|----------|\n| QA             | 35.50%    | 45.11% | 39.74%   |\n| Summarization  | 22.18%    | 96.91% | 36.09%   |\n| Data-to-Text   | 65.00%    | 100.0% | 78.79%   |\n| **Overall**    | **40.89%**| **80.68%** | **51.54%** |\n\nThe detector shows particularly high recall (80.68% overall), making it effective at catching potential hallucinations, with strong performance on data-to-text generation tasks. The adaptive nature of the classifier means it continues to improve as it processes more examples, making it ideal for production environments where user feedback can be incorporated.\n\n```python\nfrom adaptive_classifier import AdaptiveClassifier\n\n# Load the hallucination detector\ndetector = AdaptiveClassifier.from_pretrained(\"adaptive-classifier/llm-hallucination-detector\")\n\n# Detect hallucinations in RAG output\ncontext = \"France is a country in Western Europe. Its capital is Paris. The population of France is about 67 million people.\"\nquery = \"What is the capital of France and its population?\"\nresponse = \"The capital of France is Paris. The population is 70 million.\"\n\n# Format input as expected by the model\ninput_text = f\"Context: {context}\\nQuestion: {query}\\nAnswer: {response}\"\n\n# Get hallucination prediction\nprediction = detector.predict(input_text)\n# Returns: [('HALLUCINATED', 0.72), ('NOT_HALLUCINATED', 0.28)]\n\n# Example handling logic\nif prediction[0][0] == 'HALLUCINATED' and prediction[0][1] \u003e 0.6:\n    print(\"Warning: Response may contain hallucinations\")\n    # Implement safeguards: request human review, add disclaimer, etc.\n```\n\nThis system can be integrated into RAG pipelines as a safety layer, LLM evaluation frameworks, or content moderation systems. The ability to detect hallucinations helps build more trustworthy AI systems, particularly for applications in domains like healthcare, legal, finance, and education where factual accuracy is critical.\n\nThe detector can be easily fine-tuned on domain-specific data, making it adaptable to specialized use cases where the definition of hallucination may differ from general contexts.\n\n### LLM Configuration Optimization\n\nThe 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:\n\n- **DETERMINISTIC** (T: 0.0-0.1): For queries requiring precise, factual responses\n- **FOCUSED** (T: 0.2-0.5): For structured, technical responses with slight flexibility\n- **BALANCED** (T: 0.6-1.0): For natural, conversational responses\n- **CREATIVE** (T: 1.1-1.5): For more varied and imaginative outputs\n- **EXPERIMENTAL** (T: 1.6-2.0): For maximum variability and unconventional responses\n\nOur evaluation on the LLM Arena dataset demonstrates:\n- 69.8% success rate in finding optimal configurations\n- Consistent response quality (avg. similarity score: 0.64)\n- Balanced distribution across temperature classes, with each class finding its appropriate use cases\n- BALANCED and CREATIVE temperatures producing the most reliable results (scores: 0.649 and 0.645)\n\nThis 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.\n\n```python\nfrom adaptive_classifier import AdaptiveClassifier\n\n# Load the configuration optimizer\nclassifier = AdaptiveClassifier.from_pretrained(\"adaptive-classifier/llm-config-optimizer\")\n\n# Get optimal temperature class for a query\npredictions = classifier.predict(\"Your query here\")\n# Returns: [('BALANCED', 0.85), ('CREATIVE', 0.10), ...]\n```\n\nThe classifier continuously learns from new examples, adapting its predictions as it processes more queries and observes their performance.\n\n### LLM Router\n\nThe 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:\n\n- **HIGH**: Complex queries requiring advanced reasoning, multi-step problem solving, or deep expertise. Examples include:\n  - Code generation and debugging\n  - Complex analysis tasks\n  - Multi-step mathematical problems\n  - Technical explanations\n  - Creative writing tasks\n\n- **LOW**: Straightforward queries that can be handled by smaller, faster models. Examples include:\n  - Simple factual questions\n  - Basic clarifications\n  - Formatting tasks\n  - Short definitions\n  - Basic sentiment analysis\n\nThe router can be used to optimize costs and latency while maintaining response quality:\n\n```python\nfrom adaptive_classifier import AdaptiveClassifier\n\n# Load the router classifier\nclassifier = AdaptiveClassifier.from_pretrained(\"adaptive-classifier/llm-router\")\n\n# Get routing prediction for a query\npredictions = classifier.predict(\"Write a function to calculate the Fibonacci sequence\")\n# Returns: [('HIGH', 0.92), ('LOW', 0.08)]\n\n# Example routing logic\ndef route_query(query: str, classifier: AdaptiveClassifier):\n    predictions = classifier.predict(query)\n    top_class = predictions[0][0]\n    \n    if top_class == 'HIGH':\n        return use_advanced_model(query)  # e.g., GPT-4\n    else:\n        return use_basic_model(query)     # e.g., GPT-3.5-Turbo\n```\n\nWe 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.\n\n#### Key Results\n\n| Metric | Without Adaptation | With Adaptation | Impact |\n|--------|-------------------|-----------------|---------|\n| High Model Routes | 113 (22.6%) | 98 (19.6%) | 0.87x |\n| Low Model Routes | 387 (77.4%) | 402 (80.4%) | 1.04x |\n| High Model Success Rate | 40.71% | 29.59% | 0.73x |\n| Low Model Success Rate | 16.54% | 20.15% | 1.22x |\n| Overall Success Rate | 22.00% | 22.00% | 1.00x |\n| Cost Savings* | 25.60% | 32.40% | 1.27x |\n\n*Cost savings calculation assumes high-cost model is 2x the cost of low-cost model\n\n#### Analysis\n\nThe results highlight several key benefits of adaptive classification:\n\n1. **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.\n\n2. **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.\n\n3. **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.\n\n4. **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.\n\nThis real-world evaluation demonstrates that adaptive classification can significantly improve cost efficiency in LLM routing without compromising overall performance.\n\n## References\n\n- [RouteLLM: Learning to Route LLMs with Preference Data](https://arxiv.org/abs/2406.18665)\n- [Transformer^2: Self-adaptive LLMs](https://arxiv.org/abs/2501.06252)\n- [Lamini Classifier Agent Toolkit](https://www.lamini.ai/blog/classifier-agent-toolkit)\n- [Protoformer: Embedding Prototypes for Transformers](https://arxiv.org/abs/2206.12710)\n- [Overcoming catastrophic forgetting in neural networks](https://arxiv.org/abs/1612.00796)\n- [RAGTruth: A Hallucination Corpus for Developing Trustworthy Retrieval-Augmented Language Models](https://arxiv.org/abs/2401.00396)\n- [LettuceDetect: A Hallucination Detection Framework for RAG Applications](https://arxiv.org/abs/2502.17125)\n\n## Citation\n\nIf you use this library in your research, please cite:\n\n```bibtex\n@software{adaptive-classifier,\n  title = {Adaptive Classifier: Dynamic Text Classification with Continuous Learning},\n  author = {Asankhaya Sharma},\n  year = {2025},\n  publisher = {GitHub},\n  url = {https://github.com/codelion/adaptive-classifier}\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodelion%2Fadaptive-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodelion%2Fadaptive-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodelion%2Fadaptive-classifier/lists"}