davidtran999 commited on
Commit
fb8c58d
·
verified ·
1 Parent(s): dc97c5a

Upload backend/hue_portal/chatbot/chatbot.py with huggingface_hub

Browse files
backend/hue_portal/chatbot/chatbot.py CHANGED
@@ -136,7 +136,7 @@ class Chatbot(CoreChatbot):
136
  # tránh trả lại các câu trả lời cũ không có options.
137
  cached_response = None
138
  if intent != "search_legal":
139
- cached_response = EXACT_MATCH_CACHE.get(query, intent)
140
  if cached_response:
141
  cached_response["_cache"] = "exact_match"
142
  cached_response["_source"] = cached_response.get("_source", "cache")
@@ -167,8 +167,6 @@ class Chatbot(CoreChatbot):
167
  # Stage 2: Choose topic/section (if document selected but no topic)
168
  # Stage 3: Choose detail (if topic selected, ask for more details)
169
  # Final: Answer (when user says "Không" or after detail selection)
170
- disable_wizard_flow = os.environ.get("DISABLE_WIZARD_FLOW", "false").lower() == "true"
171
- print(f"[WIZARD] DISABLE_WIZARD_FLOW={os.environ.get('DISABLE_WIZARD_FLOW', 'false')} -> disable_wizard_flow={disable_wizard_flow}")
172
 
173
  has_doc_code_in_query = self._query_has_document_code(query)
174
  wizard_stage = session_metadata.get("wizard_stage") if session_metadata else None
@@ -177,41 +175,11 @@ class Chatbot(CoreChatbot):
177
 
178
  print(f"[WIZARD] Chatbot layer check - intent={intent}, wizard_stage={wizard_stage}, selected_doc_code={selected_doc_code}, selected_topic={selected_topic}, has_doc_code_in_query={has_doc_code_in_query}, query='{query[:50]}'")
179
 
180
- # CRITICAL: If wizard flow is disabled, reset all wizard state immediately
181
- if disable_wizard_flow:
182
- print("[WIZARD] 🚫 Wizard flow DISABLED - resetting all wizard state and skipping wizard stages")
183
- selected_doc_code = None
184
- selected_topic = None
185
- wizard_stage = None
186
- wizard_depth = 0
187
- # Update session metadata to clear wizard state
188
- if session_id:
189
- try:
190
- ConversationContext.update_session_metadata(
191
- session_id,
192
- {
193
- "selected_document_code": None,
194
- "selected_topic": None,
195
- "wizard_stage": None,
196
- "wizard_depth": 0,
197
- }
198
- )
199
- print("[WIZARD] ✅ Wizard state cleared from session metadata")
200
- except Exception as e:
201
- print(f"⚠️ Failed to clear wizard state: {e}")
202
- # Also update session_metadata dict for current function scope
203
- if session_metadata:
204
- session_metadata["selected_document_code"] = None
205
- session_metadata["selected_topic"] = None
206
- session_metadata["wizard_stage"] = None
207
- session_metadata["wizard_depth"] = 0
208
-
209
  # Reset wizard state if new query doesn't have document code and wizard_stage is "answer"
210
  # This handles the case where user asks a new question after completing a previous wizard flow
211
  # CRITICAL: Check conditions and reset BEFORE Stage 1 check
212
  should_reset = (
213
- not disable_wizard_flow
214
- and intent == "search_legal"
215
  and not has_doc_code_in_query
216
  and wizard_stage == "answer"
217
  )
@@ -245,15 +213,10 @@ class Chatbot(CoreChatbot):
245
  session_metadata["wizard_depth"] = 0
246
 
247
  # Stage 1: Choose document (if no document selected and no code in query)
248
- # Use Query Rewrite Strategy from slow_path_handler instead of old LLM suggestions
249
- if (
250
- intent == "search_legal"
251
- and not selected_doc_code
252
- and not has_doc_code_in_query
253
- and not disable_wizard_flow
254
- ):
255
- print("[WIZARD] ✅ Stage 1: Using Query Rewrite Strategy from slow_path_handler")
256
- # Delegate to slow_path_handler which has Query Rewrite Strategy
257
  slow_handler = SlowPathHandler()
258
  response = slow_handler.handle(
259
  query=query,
@@ -303,14 +266,7 @@ class Chatbot(CoreChatbot):
303
 
304
  # Stage 2: Choose topic/section (if document selected but no topic yet)
305
  # Skip if wizard_stage is already "answer" (user wants final answer)
306
- if (
307
- intent == "search_legal"
308
- and selected_doc_code
309
- and not selected_topic
310
- and not has_doc_code_in_query
311
- and wizard_stage != "answer"
312
- and not disable_wizard_flow
313
- ):
314
  print("[WIZARD] ✅ Stage 2 triggered: Choose topic/section")
315
 
316
  # Get document title
 
136
  # tránh trả lại các câu trả lời cũ không có options.
137
  cached_response = None
138
  if intent != "search_legal":
139
+ cached_response = EXACT_MATCH_CACHE.get(query, intent)
140
  if cached_response:
141
  cached_response["_cache"] = "exact_match"
142
  cached_response["_source"] = cached_response.get("_source", "cache")
 
167
  # Stage 2: Choose topic/section (if document selected but no topic)
168
  # Stage 3: Choose detail (if topic selected, ask for more details)
169
  # Final: Answer (when user says "Không" or after detail selection)
 
 
170
 
171
  has_doc_code_in_query = self._query_has_document_code(query)
172
  wizard_stage = session_metadata.get("wizard_stage") if session_metadata else None
 
175
 
176
  print(f"[WIZARD] Chatbot layer check - intent={intent}, wizard_stage={wizard_stage}, selected_doc_code={selected_doc_code}, selected_topic={selected_topic}, has_doc_code_in_query={has_doc_code_in_query}, query='{query[:50]}'")
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  # Reset wizard state if new query doesn't have document code and wizard_stage is "answer"
179
  # This handles the case where user asks a new question after completing a previous wizard flow
180
  # CRITICAL: Check conditions and reset BEFORE Stage 1 check
181
  should_reset = (
182
+ intent == "search_legal"
 
183
  and not has_doc_code_in_query
184
  and wizard_stage == "answer"
185
  )
 
213
  session_metadata["wizard_depth"] = 0
214
 
215
  # Stage 1: Choose document (if no document selected and no code in query)
216
+ # Use direct semantic search from slow_path_handler (Query Rewrite disabled for performance)
217
+ if intent == "search_legal" and not selected_doc_code and not has_doc_code_in_query:
218
+ print("[WIZARD] Stage 1: Using direct semantic search from slow_path_handler")
219
+ # Delegate to slow_path_handler which uses direct semantic search (no query rewrite)
 
 
 
 
 
220
  slow_handler = SlowPathHandler()
221
  response = slow_handler.handle(
222
  query=query,
 
266
 
267
  # Stage 2: Choose topic/section (if document selected but no topic yet)
268
  # Skip if wizard_stage is already "answer" (user wants final answer)
269
+ if intent == "search_legal" and selected_doc_code and not selected_topic and not has_doc_code_in_query and wizard_stage != "answer":
 
 
 
 
 
 
 
270
  print("[WIZARD] ✅ Stage 2 triggered: Choose topic/section")
271
 
272
  # Get document title