Views
No views yet
| Metric | Score |
|---|---|
| Accuracy | 96.50% |
| F1 Score (Weighted) | 0.9649 |
| F1 Score (Macro) | 0.9679 |
| Precision (Macro) | 0.97 |
| Recall (Macro) | 0.97 |
| Domain | Precision | Recall | F1-Score |
|---|---|---|---|
| coding | 0.86 | 0.92 | 0.89 |
| api_generation | 1.00 | 0.90 | 0.95 |
| mathematics | 1.00 | 1.00 | 1.00 |
| data_analysis | 0.92 | 1.00 | 0.96 |
| science | 1.00 | 1.00 | 1.00 |
| medicine | 0.93 | 1.00 | 0.96 |
| business | 0.88 | 1.00 | 0.93 |
| law | 0.91 | 1.00 | 0.95 |
| technology | 1.00 | 1.00 | 1.00 |
| literature | 1.00 | 1.00 | 1.00 |
| creative_content | 1.00 | 1.00 | 1.00 |
| education | 1.00 | 0.93 | 0.96 |
| general_knowledge | 1.00 | 0.84 | 0.91 |
| ambiguous | 1.00 | 1.00 | 1.00 |
| sensitive | 1.00 | 1.00 | 1.00 |
1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4import json
5
6# Load model
7base_model = AutoModelForCausalLM.from_pretrained(
8 "microsoft/Phi-3-mini-4k-instruct",
9 torch_dtype=torch.bfloat16,
10 device_map="auto",
11 trust_remote_code=True
12)
13
14model = PeftModel.from_pretrained(
15 base_model,
16 "YOUR_USERNAME/phi3-domain-classifier"
17)
18
19tokenizer = AutoTokenizer.from_pretrained(
20 "YOUR_USERNAME/phi3-domain-classifier",
21 trust_remote_code=True
22)
23
24# Configure for inference
25model.config.use_cache = False
26model.eval()
27
28# Classify a query
29def classify_domain(query):
30 messages = [
31 {"role": "system", "content": "You are a domain classifier. Respond with JSON."},
32 {"role": "user", "content": f"Classify this query: {query}"}
33 ]
34
35 inputs = tokenizer.apply_chat_template(
36 messages,
37 add_generation_prompt=True,
38 return_tensors="pt"
39 ).to(model.device)
40
41 with torch.no_grad():
42 outputs = model.generate(
43 inputs,
44 max_new_tokens=100,
45 temperature=0.1,
46 do_sample=True,
47 pad_token_id=tokenizer.pad_token_id,
48 eos_token_id=tokenizer.eos_token_id,
49 use_cache=False
50 )
51
52 response = tokenizer.decode(
53 outputs[0][inputs.shape[-1]:],
54 skip_special_tokens=True
55 )
56
57 return json.loads(response)
58
59# Example
60result = classify_domain("Write a Python function to calculate factorial")
61print(result)
62# Output: {"primary_domain": "coding", "confidence": "high"}1class SmartAPIRouter:
2 """Route queries to specialized LLM providers"""
3
4 def __init__(self):
5 self.classifier = DomainClassifier()
6 self.provider_mapping = {
7 "coding": "anthropic", # Claude for code
8 "api_generation": "anthropic", # Claude for APIs
9 "mathematics": "anthropic", # Claude for math
10 "creative_content": "openai", # GPT-4 for creativity
11 "general_knowledge": "openai", # GPT-4 for general Q&A
12 # ... customize as needed
13 }
14
15 def route(self, query):
16 result = self.classifier.classify(query)
17 domain = result["primary_domain"]
18 provider = self.provider_mapping.get(domain, "openai")
19
20 return {
21 "domain": domain,
22 "routed_to": provider,
23 "confidence": result["confidence"]
24 }
25
26# Usage
27router = SmartAPIRouter()
28routing_info = router.route("Explain quantum entanglement")
29# Routes to appropriate LLM provider based on domain1@misc{phi3-domain-classifier,
2 author = {Your Name},
3 title = {Phi-3 Domain Classifier for Intelligent API Routing},
4 year = {2024},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/YOUR_USERNAME/phi3-domain-classifier}},
7}