j-js commited on
Commit
36e5076
·
verified ·
1 Parent(s): 2583351

Update conversation_logic.py

Browse files
Files changed (1) hide show
  1. conversation_logic.py +120 -2
conversation_logic.py CHANGED
@@ -6,9 +6,10 @@ from typing import Any, Dict, List, Optional, Set
6
  from context_parser import detect_intent, intent_to_help_mode
7
  from formatting import format_reply
8
  from generator_engine import GeneratorEngine
9
- from models import RetrievedChunk, SolverResult
10
  from quant_solver import is_quant_question
11
  from solver_router import route_solver
 
12
  from question_classifier import classify_question, normalize_category
13
  from retrieval_engine import RetrievalEngine
14
  from utils import normalize_spaces
@@ -54,7 +55,7 @@ INTENT_KEYWORDS = {
54
  "step_by_step": ["step", "first step", "next step", "step by step"],
55
  "explain": ["explain", "why", "understand"],
56
  "method": ["method", "approach", "how do i solve", "how to solve", "equation", "formula"],
57
- "hint": ["hint", "nudge", "clue","what do i do"],
58
  "definition": ["define", "definition", "what does", "what is meant by", "meaning"],
59
  "concept": ["concept", "idea", "principle", "rule"],
60
  "instruction": ["how do i", "how to", "what should i do first", "what step", "first step"],
@@ -70,6 +71,7 @@ MISMATCH_TERMS = {
70
  "number_theory": ["circle", "triangle", "median salary"],
71
  }
72
 
 
73
  def _normalize_classified_topic(topic: Optional[str], category: Optional[str], question_text: str) -> str:
74
  t = (topic or "").strip().lower()
75
  q = (question_text or "").lower()
@@ -295,6 +297,83 @@ def _compose_reply(
295
  return "I can help with this, but I need the full question text to guide you properly."
296
 
297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
298
  def _normalize_text(text: str) -> str:
299
  return re.sub(r"\s+", " ", (text or "").strip().lower())
300
 
@@ -630,6 +709,7 @@ def _is_bad_generated_reply(text: str, user_text: str = "") -> bool:
630
 
631
  return False
632
 
 
633
  def _clean_teaching_text(text: str) -> str:
634
  text = normalize_spaces((text or "").replace("\n", " ").strip())
635
  text = re.sub(r"^[\-\•\*\d\.\)\s]+", "", text)
@@ -637,6 +717,7 @@ def _clean_teaching_text(text: str) -> str:
637
  text = text[:157].rstrip() + "..."
638
  return text
639
 
 
640
  def _looks_question_specific(text: str, question_text: str) -> bool:
641
  t = (text or "").strip().lower()
642
  q = (question_text or "").strip().lower()
@@ -691,6 +772,7 @@ def _looks_question_specific(text: str, question_text: str) -> bool:
691
 
692
  return False
693
 
 
694
  def _pick_teaching_line(
695
  chunks: List[RetrievedChunk],
696
  current_reply: str,
@@ -765,6 +847,7 @@ def _pick_teaching_line(
765
 
766
  return best_line
767
 
 
768
  class ConversationEngine:
769
  def __init__(
770
  self,
@@ -823,6 +906,40 @@ class ConversationEngine:
823
 
824
  selected_chunks: List[RetrievedChunk] = []
825
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
826
  if inferred_category == "Quantitative" or is_quant_question(solver_input):
827
  solved_result = route_solver(solver_input)
828
  if solved_result is not None:
@@ -954,6 +1071,7 @@ class ConversationEngine:
954
  "category": inferred_category,
955
  "question_type": question_type,
956
  "classified_topic": question_topic,
 
957
  }
958
 
959
  return result
 
6
  from context_parser import detect_intent, intent_to_help_mode
7
  from formatting import format_reply
8
  from generator_engine import GeneratorEngine
9
+ from models import RetrievedChunk, SolverResult, ExplainerResult
10
  from quant_solver import is_quant_question
11
  from solver_router import route_solver
12
+ from explainers.explainer_router import route_explainer
13
  from question_classifier import classify_question, normalize_category
14
  from retrieval_engine import RetrievalEngine
15
  from utils import normalize_spaces
 
55
  "step_by_step": ["step", "first step", "next step", "step by step"],
56
  "explain": ["explain", "why", "understand"],
57
  "method": ["method", "approach", "how do i solve", "how to solve", "equation", "formula"],
58
+ "hint": ["hint", "nudge", "clue", "what do i do"],
59
  "definition": ["define", "definition", "what does", "what is meant by", "meaning"],
60
  "concept": ["concept", "idea", "principle", "rule"],
61
  "instruction": ["how do i", "how to", "what should i do first", "what step", "first step"],
 
71
  "number_theory": ["circle", "triangle", "median salary"],
72
  }
73
 
74
+
75
  def _normalize_classified_topic(topic: Optional[str], category: Optional[str], question_text: str) -> str:
76
  t = (topic or "").strip().lower()
77
  q = (question_text or "").lower()
 
297
  return "I can help with this, but I need the full question text to guide you properly."
298
 
299
 
300
+ def is_explainer_request(text: str) -> bool:
301
+ t = (text or "").lower()
302
+
303
+ triggers = [
304
+ "what is this asking",
305
+ "what's this asking",
306
+ "what does this question mean",
307
+ "what does this mean",
308
+ "explain the question",
309
+ "explain this question",
310
+ "what do i need to find",
311
+ "what is being asked",
312
+ "what's being asked",
313
+ "decode this question",
314
+ "can you decode this",
315
+ "what information matters",
316
+ "what is the question asking me to find",
317
+ "what is this question asking me to do",
318
+ ]
319
+
320
+ return any(p in t for p in triggers)
321
+
322
+
323
+ def _compose_explainer_reply(
324
+ result: ExplainerResult,
325
+ verbosity: float,
326
+ transparency: float,
327
+ ) -> str:
328
+ lines: List[str] = []
329
+
330
+ if result.plain_english:
331
+ lines.append(result.plain_english)
332
+
333
+ if result.asks_for:
334
+ if transparency >= 0.35:
335
+ lines.append(f"\nThe question is asking for: {result.asks_for}")
336
+
337
+ if result.givens:
338
+ if verbosity >= 0.45:
339
+ lines.append("\nWhat the question gives you:")
340
+ for g in result.givens[:5]:
341
+ lines.append(f"- {g}")
342
+
343
+ if result.constraints:
344
+ if verbosity >= 0.65:
345
+ lines.append("\nConstraints or conditions:")
346
+ for c in result.constraints[:5]:
347
+ lines.append(f"- {c}")
348
+
349
+ if result.relationships:
350
+ if transparency >= 0.45:
351
+ lines.append("\nKey relationship:")
352
+ for r in result.relationships[:4]:
353
+ lines.append(f"- {r}")
354
+
355
+ if result.needed_concepts:
356
+ if transparency >= 0.5:
357
+ lines.append("\nConcepts you will probably need:")
358
+ for c in result.needed_concepts[:4]:
359
+ lines.append(f"- {c}")
360
+
361
+ if result.trap_notes:
362
+ if transparency >= 0.55 or verbosity >= 0.7:
363
+ lines.append("\nWatch out for:")
364
+ for t in result.trap_notes[:4]:
365
+ lines.append(f"- {t}")
366
+
367
+ if result.strategy_hint:
368
+ if verbosity >= 0.35:
369
+ lines.append(f"\nBest starting move: {result.strategy_hint}")
370
+
371
+ if not lines:
372
+ return "I can help explain what the question is asking, but I need the full wording of the question."
373
+
374
+ return "\n".join(lines).strip()
375
+
376
+
377
  def _normalize_text(text: str) -> str:
378
  return re.sub(r"\s+", " ", (text or "").strip().lower())
379
 
 
709
 
710
  return False
711
 
712
+
713
  def _clean_teaching_text(text: str) -> str:
714
  text = normalize_spaces((text or "").replace("\n", " ").strip())
715
  text = re.sub(r"^[\-\•\*\d\.\)\s]+", "", text)
 
717
  text = text[:157].rstrip() + "..."
718
  return text
719
 
720
+
721
  def _looks_question_specific(text: str, question_text: str) -> bool:
722
  t = (text or "").strip().lower()
723
  q = (question_text or "").strip().lower()
 
772
 
773
  return False
774
 
775
+
776
  def _pick_teaching_line(
777
  chunks: List[RetrievedChunk],
778
  current_reply: str,
 
847
 
848
  return best_line
849
 
850
+
851
  class ConversationEngine:
852
  def __init__(
853
  self,
 
906
 
907
  selected_chunks: List[RetrievedChunk] = []
908
 
909
+ # -----------------------------
910
+ # EXPLAINER PATH (runs before solver)
911
+ # -----------------------------
912
+ if is_explainer_request(user_text or solver_input):
913
+ explainer_result = route_explainer(solver_input)
914
+
915
+ if explainer_result is not None and getattr(explainer_result, "understood", False):
916
+ reply = _compose_explainer_reply(
917
+ result=explainer_result,
918
+ verbosity=verbosity,
919
+ transparency=transparency,
920
+ )
921
+
922
+ reply = format_reply(reply, tone, verbosity, transparency, "explain")
923
+
924
+ result.domain = "quant" if inferred_category == "Quantitative" or is_quant_question(solver_input) else "general"
925
+ result.solved = False
926
+ result.help_mode = "explain"
927
+ result.topic = explainer_result.topic or question_topic
928
+ result.answer_letter = None
929
+ result.answer_value = None
930
+ result.internal_answer = None
931
+ result.reply = reply
932
+ result.meta = {
933
+ "intent": "explain_question",
934
+ "question_text": question_text or "",
935
+ "options_count": len(options_text or []),
936
+ "category": inferred_category,
937
+ "question_type": question_type,
938
+ "classified_topic": question_topic,
939
+ "explainer_used": True,
940
+ }
941
+ return result
942
+
943
  if inferred_category == "Quantitative" or is_quant_question(solver_input):
944
  solved_result = route_solver(solver_input)
945
  if solved_result is not None:
 
1071
  "category": inferred_category,
1072
  "question_type": question_type,
1073
  "classified_topic": question_topic,
1074
+ "explainer_used": False,
1075
  }
1076
 
1077
  return result