Update conversation_logic.py
Browse files- conversation_logic.py +138 -0
conversation_logic.py
CHANGED
|
@@ -1131,6 +1131,144 @@ class ConversationEngine:
|
|
| 1131 |
question_topic = _normalize_classified_topic(classification.get("topic"), inferred_category, solver_input)
|
| 1132 |
|
| 1133 |
resolved_intent = intent or detect_intent(user_text, help_mode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1134 |
if input_type in {"hint", "next_hint"}:
|
| 1135 |
hint_lines: List[str] = []
|
| 1136 |
support_is_strong = _support_pack_is_strong(fallback_pack)
|
|
|
|
| 1131 |
question_topic = _normalize_classified_topic(classification.get("topic"), inferred_category, solver_input)
|
| 1132 |
|
| 1133 |
resolved_intent = intent or detect_intent(user_text, help_mode)
|
| 1134 |
+
if input_type == "next_hint":
|
| 1135 |
+
resolved_intent = "hint"
|
| 1136 |
+
elif input_type == "confusion":
|
| 1137 |
+
resolved_intent = "method"
|
| 1138 |
+
elif input_type in {"solve", "question"} and resolved_intent in {"hint", "walkthrough", "step_by_step"}:
|
| 1139 |
+
resolved_intent = "answer"
|
| 1140 |
+
|
| 1141 |
+
resolved_help_mode = help_mode or intent_to_help_mode(resolved_intent)
|
| 1142 |
+
if input_type in {"hint", "next_hint"}:
|
| 1143 |
+
resolved_help_mode = "hint"
|
| 1144 |
+
elif input_type == "confusion":
|
| 1145 |
+
resolved_help_mode = "explain"
|
| 1146 |
+
elif resolved_help_mode == "step_by_step":
|
| 1147 |
+
resolved_help_mode = "walkthrough"
|
| 1148 |
+
|
| 1149 |
+
prior_hint_stage = int(state.get("hint_stage", 0) or 0)
|
| 1150 |
+
history_hint_stage = _history_hint_stage(chat_history)
|
| 1151 |
+
hint_stage = _compute_hint_stage(input_type, prior_hint_stage, history_hint_stage)
|
| 1152 |
+
|
| 1153 |
+
is_quant = bool(solver_input) and (
|
| 1154 |
+
inferred_category == "Quantitative" or is_quant_question(solver_input)
|
| 1155 |
+
)
|
| 1156 |
+
|
| 1157 |
+
result = SolverResult(
|
| 1158 |
+
domain="quant" if is_quant else "general",
|
| 1159 |
+
solved=False,
|
| 1160 |
+
help_mode=resolved_help_mode,
|
| 1161 |
+
topic=question_topic if is_quant else "general",
|
| 1162 |
+
used_retrieval=False,
|
| 1163 |
+
used_generator=False,
|
| 1164 |
+
steps=[],
|
| 1165 |
+
teaching_chunks=[],
|
| 1166 |
+
meta={},
|
| 1167 |
+
)
|
| 1168 |
+
|
| 1169 |
+
solver_result: Optional[SolverResult] = None
|
| 1170 |
+
if _should_try_solver(is_quant, resolved_help_mode, solver_input):
|
| 1171 |
+
try:
|
| 1172 |
+
solver_result = route_solver(solver_input)
|
| 1173 |
+
except Exception:
|
| 1174 |
+
solver_result = None
|
| 1175 |
+
_apply_safe_step_sanitization(solver_result)
|
| 1176 |
+
|
| 1177 |
+
explainer_result = None
|
| 1178 |
+
explainer_understood = False
|
| 1179 |
+
explainer_scaffold: Dict[str, Any] = {}
|
| 1180 |
+
if solver_input:
|
| 1181 |
+
try:
|
| 1182 |
+
explainer_result = route_explainer(solver_input)
|
| 1183 |
+
except Exception:
|
| 1184 |
+
explainer_result = None
|
| 1185 |
+
if explainer_result is not None and getattr(explainer_result, "understood", False):
|
| 1186 |
+
explainer_understood = True
|
| 1187 |
+
explainer_scaffold = _extract_explainer_scaffold(explainer_result)
|
| 1188 |
+
|
| 1189 |
+
fallback_reply_core = ""
|
| 1190 |
+
fallback_pack: Dict[str, Any] = {}
|
| 1191 |
+
if solver_input:
|
| 1192 |
+
fallback_reply_core, fallback_pack = _build_fallback_reply(
|
| 1193 |
+
question_id=question_id,
|
| 1194 |
+
question_text=solver_input,
|
| 1195 |
+
options_text=options_text,
|
| 1196 |
+
topic=question_topic,
|
| 1197 |
+
category=inferred_category,
|
| 1198 |
+
help_mode=resolved_help_mode,
|
| 1199 |
+
hint_stage=hint_stage,
|
| 1200 |
+
verbosity=verbosity,
|
| 1201 |
+
)
|
| 1202 |
+
|
| 1203 |
+
question_specific_reply_core = _build_question_specific_reply(
|
| 1204 |
+
question_text=solver_input,
|
| 1205 |
+
options_text=options_text,
|
| 1206 |
+
classified_topic=question_topic,
|
| 1207 |
+
help_mode=resolved_help_mode,
|
| 1208 |
+
input_type=input_type,
|
| 1209 |
+
user_text=user_text,
|
| 1210 |
+
)
|
| 1211 |
+
|
| 1212 |
+
if solver_result is not None:
|
| 1213 |
+
result.meta = result.meta or {}
|
| 1214 |
+
solver_topic = getattr(solver_result, "topic", None) or "unknown"
|
| 1215 |
+
|
| 1216 |
+
compatible_topics = {
|
| 1217 |
+
question_topic,
|
| 1218 |
+
"general_quant",
|
| 1219 |
+
"general",
|
| 1220 |
+
"unknown",
|
| 1221 |
+
}
|
| 1222 |
+
|
| 1223 |
+
if question_topic == "algebra":
|
| 1224 |
+
compatible_topics.update({"ratio"})
|
| 1225 |
+
elif question_topic == "ratio":
|
| 1226 |
+
compatible_topics.update({"algebra"})
|
| 1227 |
+
elif question_topic == "percent":
|
| 1228 |
+
compatible_topics.update({"ratio", "algebra"})
|
| 1229 |
+
|
| 1230 |
+
if solver_topic in compatible_topics:
|
| 1231 |
+
result = solver_result
|
| 1232 |
+
result.domain = "quant"
|
| 1233 |
+
result.meta = result.meta or {}
|
| 1234 |
+
result.topic = question_topic if question_topic else solver_topic
|
| 1235 |
+
result.meta["solver_topic_accepted"] = solver_topic
|
| 1236 |
+
else:
|
| 1237 |
+
result.meta["solver_topic_rejected"] = solver_topic
|
| 1238 |
+
result.meta["solver_topic_expected"] = question_topic
|
| 1239 |
+
result.topic = question_topic if is_quant else result.topic
|
| 1240 |
+
else:
|
| 1241 |
+
result.meta = result.meta or {}
|
| 1242 |
+
result.topic = question_topic if is_quant else result.topic
|
| 1243 |
+
|
| 1244 |
+
_apply_safe_step_sanitization(result)
|
| 1245 |
+
solver_steps = _get_result_steps(result)
|
| 1246 |
+
solver_has_steps = bool(solver_steps)
|
| 1247 |
+
prefer_question_support = _should_prefer_question_support(resolved_help_mode, fallback_pack)
|
| 1248 |
+
direct_solve_request = _is_direct_solve_request(user_text or solver_input, resolved_intent)
|
| 1249 |
+
solver_topic_ok = result.meta.get("solver_topic_rejected") is None
|
| 1250 |
+
|
| 1251 |
+
result.help_mode = resolved_help_mode
|
| 1252 |
+
result.meta = result.meta or {}
|
| 1253 |
+
result.meta["hint_stage"] = hint_stage
|
| 1254 |
+
result.meta["resolved_intent"] = resolved_intent
|
| 1255 |
+
result.meta["input_type"] = input_type
|
| 1256 |
+
result.meta["built_on_previous_turn"] = built_on_previous_turn
|
| 1257 |
+
result.meta["question_topic"] = question_topic
|
| 1258 |
+
result.meta["inferred_category"] = inferred_category
|
| 1259 |
+
result.meta["question_id"] = question_id
|
| 1260 |
+
result.meta["solver_used"] = solver_result is not None
|
| 1261 |
+
result.meta["solver_topic_ok"] = solver_topic_ok
|
| 1262 |
+
result.meta["explainer_used"] = False
|
| 1263 |
+
result.meta["explainer_understood"] = explainer_understood
|
| 1264 |
+
result.meta["question_support_used"] = False
|
| 1265 |
+
result.meta["question_support_topic"] = fallback_pack.get("topic") if fallback_pack else None
|
| 1266 |
+
result.meta["question_support_source"] = fallback_pack.get("support_source") if fallback_pack else None
|
| 1267 |
+
result.meta["question_support_match"] = fallback_pack.get("support_match") if fallback_pack else None
|
| 1268 |
+
result.meta["question_support_strong"] = _support_pack_is_strong(fallback_pack)
|
| 1269 |
+
result.meta["prefer_question_support"] = prefer_question_support
|
| 1270 |
+
result.meta["explainer_scaffold"] = explainer_scaffold
|
| 1271 |
+
|
| 1272 |
if input_type in {"hint", "next_hint"}:
|
| 1273 |
hint_lines: List[str] = []
|
| 1274 |
support_is_strong = _support_pack_is_strong(fallback_pack)
|