Spaces:
Runtime error
Runtime error
File size: 3,339 Bytes
63bcd5a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | # src/recommendation_engine/intent_classifier.py
import logging
from typing import Dict
from src.recommendation_engine.llm_client import generate_text
logger = logging.getLogger(__name__)
# =========================================
# VALID INTENTS
# =========================================
VALID_INTENTS = {
"idea",
"feature",
"full_project",
"chat"
}
# =========================================
# SAFE NORMALIZER
# =========================================
def normalize_intent(text: str) -> str:
if not text:
return "chat"
text = text.lower().strip().split()[0]
if text in VALID_INTENTS:
return text
return "chat"
# =========================================
# LLM INTENT CLASSIFIER
# =========================================
def classify_with_llm(user_input: str, state: Dict) -> str:
prompt = f"""
You are an intent classifier for a graduation project assistant.
Return ONLY ONE word from:
idea
feature
description
full_project
chat
Context:
Project Title: {state.get("project_title") or "None"}
Has Features: {"yes" if state.get("features") else "no"}
Has Description: {"yes" if state.get("description") else "no"}
User:
"{user_input}"
Rules:
- Asking for ideas β idea
- Asking for another idea β idea
- Giving a project idea β feature
- Asking for features β feature
- Asking for description β description
- Asking for full project β full_project
- Otherwise β chat
"""
try:
result = generate_text(prompt, task="intent")
return normalize_intent(result)
except Exception as e:
logger.warning(f"[INTENT ERROR] {e}")
return "chat"
# =========================================
# HYBRID DETECTION (FINAL π₯)
# =========================================
def detect_intent(text: str, state: dict = None) -> str:
if state is None:
state = {}
text_clean = text.lower().strip()
has_project = bool(state.get("project_title"))
has_features = bool(state.get("features"))
# =========================================
# HARD RULES (NO FLOW FORCING β)
# =========================================
# explicit idea requests
if any(x in text_clean for x in [
"idea", "project idea", "new idea", "another idea", "suggest"
]):
return "idea"
# explicit feature requests
if "feature" in text_clean:
return "feature"
# full project
if any(x in text_clean for x in [
"full project", "complete", "all details"
]):
return "full_project"
# =========================================
# USER PROVIDED IDEA (π₯ important)
# =========================================
# If user writes sentence like a project idea
if not has_project and len(text_clean.split()) >= 3:
return "feature"
# =========================================
# LLM fallback
# =========================================
intent = classify_with_llm(text, state)
logger.info(f"[INTENT] {intent}")
# =========================================
# SAFETY FIXES
# =========================================
if intent == "feature" and not has_project:
return "feature" # user likely gave idea
if intent == "description" and not has_project:
return "idea"
return intent |