testtest123 commited on
Commit
84acb35
·
1 Parent(s): 8f9c9c9

feat: implement Hybrid Intent Detection (Keywords + LLM) for CRAG

Browse files
RAG_FULL_APPLICATION_BACKEND/app/services/advanced_services.py CHANGED
@@ -71,22 +71,37 @@ class CorrectiveRAGService(ICorrectiveRAGService):
71
  else:
72
  max_similarity = max([c.get("similarity", 0.0) for c in retrieved_chunks] or [0.0])
73
 
74
- intent_prompt = f"""Analyze the following user query: "{query}"
75
- Determine if the query is EITHER:
76
- 1. A conversational greeting (e.g., "hi", "namaste", "hello there", "namaskaram") OR
77
- 2. A general summarization or document inquiry (e.g., "what is this document about?", "summarize this", "idi emiti")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- If the query falls into EITHER category 1 OR category 2, you MUST return true. Otherwise, return false.
80
 
81
  Return ONLY a JSON object:
82
  {{"is_conversational": true/false}}
83
  """
84
- try:
85
- intent_res = llm_service.evaluate_json(intent_prompt)
86
- is_conversational = intent_res.get("is_conversational", False)
87
- except Exception:
88
- # Fallback to simple heuristic if LLM fails
89
- is_conversational = len(query.split()) < 3
90
 
91
  is_low_confidence = (max_similarity < 0.50) and not is_conversational
92
 
 
71
  else:
72
  max_similarity = max([c.get("similarity", 0.0) for c in retrieved_chunks] or [0.0])
73
 
74
+ # Hybrid Intent Detection (Keywords + LLM)
75
+ query_lower = query.lower().strip()
76
+
77
+ # 1. Fast, deterministic pattern match for greetings & document summary inquiries
78
+ common_patterns = [
79
+ "hi", "hello", "hey", "namaste", "namaskaram", "namaskar", "greetings",
80
+ "good morning", "good afternoon", "good evening",
81
+ "summarize", "summary", "overview", "main points", "key takeaways", "brief",
82
+ "what is this document", "about this document", "document about", "what is this pdf",
83
+ "idi emiti", "katha emiti", "vishyamanu", "kya hai"
84
+ ]
85
+
86
+ is_conversational = any(p in query_lower for p in common_patterns) or len(query_lower.split()) < 3
87
+
88
+ # 2. If keywords don't match, fall back to LLM intent classification for multi-lingual/complex queries
89
+ if not is_conversational:
90
+ intent_prompt = f"""Analyze the user query: "{query}"
91
+ Determine if it belongs to EITHER of these categories:
92
+ Category 1: Conversational greeting or small talk (any language)
93
+ Category 2: General document summarization request or overall document question (e.g. asking what the document is about)
94
 
95
+ If it belongs to Category 1 OR Category 2, return true. If it is a specific factual search query, return false.
96
 
97
  Return ONLY a JSON object:
98
  {{"is_conversational": true/false}}
99
  """
100
+ try:
101
+ intent_res = llm_service.evaluate_json(intent_prompt)
102
+ is_conversational = intent_res.get("is_conversational", False)
103
+ except Exception:
104
+ pass
 
105
 
106
  is_low_confidence = (max_similarity < 0.50) and not is_conversational
107