| # π LLM Query Classifier β General vs Real-Time |
|
|
| A fine-tuned NLP model that classifies whether a user query requires a **static LLM response** or a **real-time data source**. Built on top of `MiniLM-L6` using Hugging Face Transformers. |
|
|
| --- |
|
|
| ## π‘ Why This Exists |
|
|
| Modern AI assistants face a core routing problem: |
|
|
| - *"What is the capital of France?"* β A static LLM can answer this perfectly. |
| - *"What is the price of Bitcoin right now?"* β A static LLM will hallucinate or give outdated info. |
|
|
| Without a classifier, you either always call an expensive real-time API (slow + costly), or you let the LLM guess (unreliable). This model solves that by routing queries intelligently **before** any expensive call is made. |
|
|
| --- |
|
|
| ## π§ Model & Approach |
|
|
| | Detail | Value | |
| |---|---| |
| | Base Model | `nreimers/MiniLM-L6-H384-uncased` | |
| | Task | Binary Text Classification | |
| | Framework | Hugging Face Transformers + PyTorch | |
| | Training Set | ~260 labeled queries | |
| | Validation Set | ~63 queries (20% split) | |
| | Classes | `0 = general`, `1 = realtime` | |
|
|
| --- |
|
|
| ## π Dataset |
|
|
| 323 manually curated queries split across two classes: |
|
|
| - **General (0):** 145 examples β facts, definitions, history, science, explanations |
| - **Real-time (1):** 178 examples β current events, prices, weather, live scores, breaking news |
|
|
| **Sample queries:** |
|
|
| | Query | Label | |
| |---|---| |
| | "Explain the theory of relativity" | general | |
| | "Who wrote Pride and Prejudice?" | general | |
| | "What is the current price of Bitcoin?" | realtime | |
| | "Latest news on the Ukraine war" | realtime | |
| | "Who is the Prime Minister of the UK right now?" | realtime | |
|
|
| --- |
|
|
| ## π How to Use |
|
|
| ### 1. Install dependencies |
| ```bash |
| pip install transformers torch |
| ``` |
|
|
| ### 2. Load and run the classifier |
| ```python |
| from query_classifier import QueryClassifier |
| |
| classifier = QueryClassifier() |
| |
| query = "What is the weather in Islamabad today?" |
| category, confidence = classifier.classify(query) |
| |
| print(f"Category: {category}") # realtime |
| print(f"Confidence: {confidence:.2f}") # e.g. 0.97 |
| ``` |
|
|
| ### 3. Output |
| ``` |
| Category: realtime |
| Confidence: 0.97 |
| ``` |
|
|
| --- |
|
|
| ## π Project Structure |
|
|
| ``` |
| query-classifier/ |
| βββ train_classifier.py # Fine-tuning script |
| βββ query_classifier.py # Inference class (plug-and-play) |
| βββ training_data.csv # Labeled dataset |
| βββ trained_model/ # Saved model weights (after training) |
| β βββ config.json |
| β βββ tokenizer_config.json |
| β βββ model.safetensors |
| βββ README.md |
| ``` |
|
|
| --- |
|
|
| ## ποΈ Training |
|
|
| ```bash |
| python train_classifier.py |
| ``` |
|
|
| Trains for 3 epochs with AdamW optimizer (lr=5e-5), saves the best checkpoint based on validation accuracy. |
|
|
| --- |
|
|
| ## π Integration Example |
|
|
| This classifier is designed to sit **in front of your LLM pipeline**: |
|
|
| ```python |
| category, confidence = classifier.classify(user_query) |
| |
| if category == "realtime": |
| response = call_search_api(user_query) # Tavily, Serper, etc. |
| else: |
| response = call_llm(user_query) # GPT-4, Claude, etc. |
| ``` |
|
|
| --- |
|
|
| ## π οΈ Built With |
|
|
| - [Hugging Face Transformers](https://huggingface.co/transformers/) |
| - [PyTorch](https://pytorch.org/) |
| - [MiniLM](https://huggingface.co/nreimers/MiniLM-L6-H384-uncased) β lightweight and fast |
|
|
| --- |
|
|
| ## π€ Author |
|
|
| Built by [Your Name] β NLP & LLM Fine-tuning specialist. |
| Open to freelance projects β [Your Upwork / LinkedIn link] |