j-js commited on
Commit
09c4b5d
·
verified ·
1 Parent(s): 28ef36e

Update conversation_logic.py

Browse files
Files changed (1) hide show
  1. conversation_logic.py +133 -103
conversation_logic.py CHANGED
@@ -6,7 +6,7 @@ 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, format_explainer_response
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
@@ -85,7 +85,6 @@ def _normalize_classified_topic(topic: Optional[str], category: Optional[str], q
85
  or bool(re.search(r"\b[a-z]\s*[\+\-\*/=]", q))
86
  )
87
 
88
- # override suspicious ratio classifications caused by prompt punctuation like "Solve:"
89
  if t == "ratio" and not has_ratio_form and has_algebra_form:
90
  t = "algebra"
91
 
@@ -96,7 +95,7 @@ def _normalize_classified_topic(topic: Optional[str], category: Optional[str], q
96
  return "percent"
97
  if "ratio" in q or has_ratio_form:
98
  return "ratio"
99
- if "probability" in q or "chosen at random" in q:
100
  return "probability"
101
  if "divisible" in q or "remainder" in q or "prime" in q or "factor" in q:
102
  return "number_theory"
@@ -186,18 +185,18 @@ def _compose_reply(
186
  ])
187
  if topic == "percent":
188
  return "\n".join([
189
- "- Identify the percent relationship.",
190
- "- Work out which value is the base.",
191
  ])
192
  if topic == "ratio":
193
  return "\n".join([
194
- "- Set up the ratio relationship.",
195
- "- Keep the comparison consistent.",
196
  ])
197
  if topic == "probability":
198
  return "\n".join([
199
- "- Identify the total number of possible outcomes.",
200
- "- Then work out how many outcomes fit the condition.",
201
  ])
202
  if topic == "statistics":
203
  return "\n".join([
@@ -298,26 +297,29 @@ def _compose_reply(
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 _normalize_text(text: str) -> str:
@@ -359,8 +361,8 @@ def _infer_structure_terms(question_text: str, topic: Optional[str], question_ty
359
  terms.extend(["mean", "average"])
360
  if "median" in q:
361
  terms.extend(["median"])
362
- if "probability" in q:
363
- terms.extend(["probability"])
364
  if "remainder" in q or "divisible" in q:
365
  terms.extend(["remainder", "divisible"])
366
 
@@ -389,12 +391,26 @@ def _is_direct_solve_request(text: str, intent: str) -> bool:
389
  return False
390
 
391
 
392
- def should_retrieve(intent: str, solved: bool, raw_user_text: str, category: Optional[str] = None) -> bool:
 
 
 
 
 
 
 
393
  normalized_category = normalize_category(category)
 
 
394
 
395
  if intent == "hint":
396
  return False
397
 
 
 
 
 
 
398
  if intent in {"walkthrough", "step_by_step", "method", "explain", "concept", "definition", "instruction"}:
399
  return True
400
 
@@ -685,11 +701,18 @@ def _looks_question_specific(text: str, question_text: str) -> bool:
685
  "plug in numbers",
686
  "backsolving",
687
  "working backwards",
 
 
688
  ]
689
  if any(p in t for p in banned_phrases):
690
  return True
691
 
692
- # reject chunks that look like worked examples or another problem
 
 
 
 
 
693
  if len(re.findall(r"\d+", t)) >= 3:
694
  q_numbers = set(re.findall(r"\d+", q))
695
  t_numbers = set(re.findall(r"\d+", t))
@@ -712,7 +735,6 @@ def _looks_question_specific(text: str, question_text: str) -> bool:
712
  if mentioned - q_vars:
713
  return True
714
 
715
- # too long usually means it is a pasted worked solution, not a teaching line
716
  if len(t.split()) > 35:
717
  return True
718
 
@@ -821,6 +843,9 @@ class ConversationEngine:
821
  solver_input = (question_text or raw_user_text or "").strip()
822
  user_text = (raw_user_text or "").strip()
823
 
 
 
 
824
  category = normalize_category(kwargs.get("category"))
825
  classification = classify_question(question_text=solver_input, category=category)
826
  inferred_category = normalize_category(classification.get("category") or category)
@@ -835,8 +860,10 @@ class ConversationEngine:
835
  resolved_intent = intent or detect_intent(user_text, help_mode)
836
  resolved_help_mode = help_mode or intent_to_help_mode(resolved_intent)
837
 
 
 
838
  result = SolverResult(
839
- domain="general",
840
  solved=False,
841
  help_mode=resolved_help_mode,
842
  answer_letter=None,
@@ -850,19 +877,10 @@ class ConversationEngine:
850
  meta={},
851
  )
852
 
853
- selected_chunks: List[RetrievedChunk] = []
854
-
855
-
856
- # 1. set up input / classification / intent
857
- solver_input = (question_text or raw_user_text or "").strip()
858
- user_text = (raw_user_text or "").strip()
859
-
860
- is_quant = inferred_category == "Quantitative" or is_quant_question(solver_input)
861
-
862
- # 2. explainer path FIRST
863
  if is_explainer_request(user_text or solver_input):
864
  explainer_result = route_explainer(solver_input)
865
-
866
  if explainer_result is not None and getattr(explainer_result, "understood", False):
867
  reply = format_explainer_response(
868
  result=explainer_result,
@@ -870,29 +888,30 @@ class ConversationEngine:
870
  verbosity=verbosity,
871
  transparency=transparency,
872
  )
873
- result.domain = "quant" if is_quant else "general"
874
- result.solved = False
875
- result.help_mode = "explain"
876
- result.topic = getattr(explainer_result, "topic", None) or question_topic
877
- result.answer_letter = None
878
- result.answer_value = None
879
- result.internal_answer = None
880
- result.used_retrieval = False
881
- result.used_generator = False
882
- result.reply = reply
883
- result.meta = {
884
- "intent": "explain_question",
885
- "question_text": question_text or "",
886
- "options_count": len(options_text or []),
887
- "category": inferred_category,
888
- "question_type": question_type,
889
- "classified_topic": question_topic,
890
- "explainer_used": True,
891
- }
892
- return result
893
-
894
- # 3. normal solver path AFTER
895
- if inferred_category == "Quantitative" or is_quant_question(solver_input):
 
896
  solved_result = route_solver(solver_input)
897
 
898
  if solved_result is not None:
@@ -905,61 +924,64 @@ class ConversationEngine:
905
 
906
  result.domain = "quant"
907
 
908
- # ✅ IMPORTANT: use solver formatter (NOT explainer)
909
  reply = _compose_reply(
910
  result=result,
911
  intent=resolved_intent,
912
  verbosity=verbosity,
913
  category=inferred_category,
914
  )
915
-
916
 
 
917
  allow_retrieval = should_retrieve(
918
  intent=resolved_intent,
919
  solved=bool(result.solved),
920
  raw_user_text=user_text or solver_input,
921
  category=inferred_category,
 
 
922
  )
923
 
924
- if allow_retrieval and retrieval_context:
925
- filtered = _filter_retrieved_chunks(
926
- chunks=retrieval_context,
927
- intent=resolved_intent,
928
- topic=result.topic,
929
- question_text=solver_input,
930
- question_type=question_type,
931
- )
932
- if filtered:
933
- selected_chunks = filtered
934
- result.used_retrieval = True
935
- result.teaching_chunks = filtered
936
-
937
- elif allow_retrieval and self.retriever is not None:
938
- retrieved = self.retriever.search(
939
- query=_build_retrieval_query(
940
- raw_user_text=user_text,
941
  question_text=solver_input,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942
  intent=resolved_intent,
943
  topic=result.topic,
944
- solved=bool(result.solved),
945
  question_type=question_type,
946
- category=inferred_category,
947
- ),
948
- topic=result.topic or "",
949
- intent=resolved_intent,
950
- k=6,
951
- )
952
- filtered = _filter_retrieved_chunks(
953
- chunks=retrieved,
954
- intent=resolved_intent,
955
- topic=result.topic,
956
- question_text=solver_input,
957
- question_type=question_type,
958
- )
959
- if filtered:
960
- selected_chunks = filtered
961
- result.used_retrieval = True
962
- result.teaching_chunks = filtered
963
 
964
  if selected_chunks and resolved_help_mode in {"walkthrough", "step_by_step", "method", "explain", "concept"}:
965
  teaching_line = _pick_teaching_line(
@@ -971,6 +993,7 @@ class ConversationEngine:
971
  if teaching_line:
972
  reply = f"{reply}\n\nKey idea: {teaching_line}"
973
 
 
974
  should_try_generator = (
975
  self.generator is not None
976
  and not result.solved
@@ -1015,12 +1038,19 @@ class ConversationEngine:
1015
  intent=resolved_intent,
1016
  )
1017
 
 
 
 
 
 
 
 
 
1018
  reply = format_reply(reply, tone, verbosity, transparency, resolved_help_mode)
1019
 
1020
  result.answer_letter = None
1021
  result.answer_value = None
1022
  result.internal_answer = None
1023
-
1024
  result.reply = reply
1025
  result.help_mode = resolved_help_mode
1026
  result.meta = {
 
6
  from context_parser import detect_intent, intent_to_help_mode
7
  from formatting import format_reply, format_explainer_response
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 explainers.explainer_router import route_explainer
 
85
  or bool(re.search(r"\b[a-z]\s*[\+\-\*/=]", q))
86
  )
87
 
 
88
  if t == "ratio" and not has_ratio_form and has_algebra_form:
89
  t = "algebra"
90
 
 
95
  return "percent"
96
  if "ratio" in q or has_ratio_form:
97
  return "ratio"
98
+ if "probability" in q or "chosen at random" in q or "odds" in q or "chance" in q:
99
  return "probability"
100
  if "divisible" in q or "remainder" in q or "prime" in q or "factor" in q:
101
  return "number_theory"
 
185
  ])
186
  if topic == "percent":
187
  return "\n".join([
188
+ "- Identify whether you need the part, the whole, or the percent.",
189
+ "- Then set up the percent relationship carefully.",
190
  ])
191
  if topic == "ratio":
192
  return "\n".join([
193
+ "- Identify which quantities are being compared.",
194
+ "- Keep the ratio in the correct order throughout.",
195
  ])
196
  if topic == "probability":
197
  return "\n".join([
198
+ "- Identify what counts as a successful outcome.",
199
+ "- Then compare favorable outcomes to total possible outcomes.",
200
  ])
201
  if topic == "statistics":
202
  return "\n".join([
 
297
 
298
 
299
  def is_explainer_request(text: str) -> bool:
300
+ t = (text or "").strip().lower()
301
 
302
+ explainer_signals = [
303
+ "how do i solve",
304
+ "how to solve",
305
+ "explain this",
306
+ "walk me through",
307
+ "walkthrough",
308
+ "show me how",
309
+ "what is the method",
310
+ "how would you do this",
311
+ "help me understand",
312
+ "what's the approach",
313
+ "what is the approach",
314
+ "how should i think about this",
315
  "what is this asking",
316
+ "how do i approach this",
317
+ "can you explain",
318
+ "explain how",
319
+ "explain why",
 
 
 
 
 
 
 
 
 
320
  ]
321
 
322
+ return any(p in t for p in explainer_signals)
323
 
324
 
325
  def _normalize_text(text: str) -> str:
 
361
  terms.extend(["mean", "average"])
362
  if "median" in q:
363
  terms.extend(["median"])
364
+ if "probability" in q or "odds" in q or "chance" in q:
365
+ terms.extend(["probability", "outcome", "event"])
366
  if "remainder" in q or "divisible" in q:
367
  terms.extend(["remainder", "divisible"])
368
 
 
391
  return False
392
 
393
 
394
+ def should_retrieve(
395
+ intent: str,
396
+ solved: bool,
397
+ raw_user_text: str,
398
+ category: Optional[str] = None,
399
+ domain: Optional[str] = None,
400
+ topic: Optional[str] = None,
401
+ ) -> bool:
402
  normalized_category = normalize_category(category)
403
+ normalized_domain = (domain or "").strip().lower()
404
+ normalized_topic = (topic or "").strip().lower()
405
 
406
  if intent == "hint":
407
  return False
408
 
409
+ if normalized_domain == "quant":
410
+ if intent in {"walkthrough", "step_by_step", "method", "explain", "concept"}:
411
+ return normalized_topic not in {"", "general", "unknown", "general_quant"}
412
+ return False
413
+
414
  if intent in {"walkthrough", "step_by_step", "method", "explain", "concept", "definition", "instruction"}:
415
  return True
416
 
 
701
  "plug in numbers",
702
  "backsolving",
703
  "working backwards",
704
+ "chapter",
705
+ "note:",
706
  ]
707
  if any(p in t for p in banned_phrases):
708
  return True
709
 
710
+ if "gmat" in t[:25]:
711
+ return True
712
+
713
+ if "..." in t:
714
+ return True
715
+
716
  if len(re.findall(r"\d+", t)) >= 3:
717
  q_numbers = set(re.findall(r"\d+", q))
718
  t_numbers = set(re.findall(r"\d+", t))
 
735
  if mentioned - q_vars:
736
  return True
737
 
 
738
  if len(t.split()) > 35:
739
  return True
740
 
 
843
  solver_input = (question_text or raw_user_text or "").strip()
844
  user_text = (raw_user_text or "").strip()
845
 
846
+ reply: Optional[str] = None
847
+ selected_chunks: List[RetrievedChunk] = []
848
+
849
  category = normalize_category(kwargs.get("category"))
850
  classification = classify_question(question_text=solver_input, category=category)
851
  inferred_category = normalize_category(classification.get("category") or category)
 
860
  resolved_intent = intent or detect_intent(user_text, help_mode)
861
  resolved_help_mode = help_mode or intent_to_help_mode(resolved_intent)
862
 
863
+ is_quant = inferred_category == "Quantitative" or is_quant_question(solver_input)
864
+
865
  result = SolverResult(
866
+ domain="quant" if is_quant else "general",
867
  solved=False,
868
  help_mode=resolved_help_mode,
869
  answer_letter=None,
 
877
  meta={},
878
  )
879
 
880
+ # 1. explainer path first
 
 
 
 
 
 
 
 
 
881
  if is_explainer_request(user_text or solver_input):
882
  explainer_result = route_explainer(solver_input)
883
+
884
  if explainer_result is not None and getattr(explainer_result, "understood", False):
885
  reply = format_explainer_response(
886
  result=explainer_result,
 
888
  verbosity=verbosity,
889
  transparency=transparency,
890
  )
891
+
892
+ result.domain = "quant" if is_quant else "general"
893
+ result.solved = False
894
+ result.help_mode = "explain"
895
+ result.topic = getattr(explainer_result, "topic", None) or question_topic
896
+ result.answer_letter = None
897
+ result.answer_value = None
898
+ result.internal_answer = None
899
+ result.used_retrieval = False
900
+ result.used_generator = False
901
+ result.reply = reply
902
+ result.meta = {
903
+ "intent": "explain_question",
904
+ "question_text": question_text or "",
905
+ "options_count": len(options_text or []),
906
+ "category": inferred_category,
907
+ "question_type": question_type,
908
+ "classified_topic": question_topic,
909
+ "explainer_used": True,
910
+ }
911
+ return result
912
+
913
+ # 2. normal solver path
914
+ if is_quant:
915
  solved_result = route_solver(solver_input)
916
 
917
  if solved_result is not None:
 
924
 
925
  result.domain = "quant"
926
 
927
+ # 3. compose base reply
928
  reply = _compose_reply(
929
  result=result,
930
  intent=resolved_intent,
931
  verbosity=verbosity,
932
  category=inferred_category,
933
  )
 
934
 
935
+ # 4. optional retrieval
936
  allow_retrieval = should_retrieve(
937
  intent=resolved_intent,
938
  solved=bool(result.solved),
939
  raw_user_text=user_text or solver_input,
940
  category=inferred_category,
941
+ domain=result.domain,
942
+ topic=result.topic,
943
  )
944
 
945
+ if allow_retrieval and reply and len(reply) < 220:
946
+ if retrieval_context:
947
+ filtered = _filter_retrieved_chunks(
948
+ chunks=retrieval_context,
949
+ intent=resolved_intent,
950
+ topic=result.topic,
 
 
 
 
 
 
 
 
 
 
 
951
  question_text=solver_input,
952
+ question_type=question_type,
953
+ )
954
+ if filtered:
955
+ selected_chunks = filtered
956
+ result.used_retrieval = True
957
+ result.teaching_chunks = filtered
958
+
959
+ elif self.retriever is not None:
960
+ retrieved = self.retriever.search(
961
+ query=_build_retrieval_query(
962
+ raw_user_text=user_text,
963
+ question_text=solver_input,
964
+ intent=resolved_intent,
965
+ topic=result.topic,
966
+ solved=bool(result.solved),
967
+ question_type=question_type,
968
+ category=inferred_category,
969
+ ),
970
+ topic=result.topic or "",
971
+ intent=resolved_intent,
972
+ k=6,
973
+ )
974
+ filtered = _filter_retrieved_chunks(
975
+ chunks=retrieved,
976
  intent=resolved_intent,
977
  topic=result.topic,
978
+ question_text=solver_input,
979
  question_type=question_type,
980
+ )
981
+ if filtered:
982
+ selected_chunks = filtered
983
+ result.used_retrieval = True
984
+ result.teaching_chunks = filtered
 
 
 
 
 
 
 
 
 
 
 
 
985
 
986
  if selected_chunks and resolved_help_mode in {"walkthrough", "step_by_step", "method", "explain", "concept"}:
987
  teaching_line = _pick_teaching_line(
 
993
  if teaching_line:
994
  reply = f"{reply}\n\nKey idea: {teaching_line}"
995
 
996
+ # 5. generator only for non-quant
997
  should_try_generator = (
998
  self.generator is not None
999
  and not result.solved
 
1038
  intent=resolved_intent,
1039
  )
1040
 
1041
+ # 6. final fallback
1042
+ if not reply:
1043
+ reply = _fallback_more_info_reply(
1044
+ category=inferred_category,
1045
+ topic=result.topic,
1046
+ intent=resolved_intent,
1047
+ )
1048
+
1049
  reply = format_reply(reply, tone, verbosity, transparency, resolved_help_mode)
1050
 
1051
  result.answer_letter = None
1052
  result.answer_value = None
1053
  result.internal_answer = None
 
1054
  result.reply = reply
1055
  result.help_mode = resolved_help_mode
1056
  result.meta = {