Update conversation_logic.py
Browse files- conversation_logic.py +39 -18
conversation_logic.py
CHANGED
|
@@ -114,15 +114,41 @@ def _teaching_lines(chunks: List[RetrievedChunk]) -> List[str]:
|
|
| 114 |
return lines
|
| 115 |
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
def _compose_reply(
|
| 118 |
result: SolverResult,
|
| 119 |
intent: str,
|
| 120 |
-
reveal_answer: bool,
|
| 121 |
verbosity: float,
|
| 122 |
category: Optional[str] = None,
|
| 123 |
) -> str:
|
| 124 |
-
steps = result.steps or []
|
| 125 |
-
internal = result.internal_answer or result.answer_value or ""
|
| 126 |
|
| 127 |
if intent == "hint":
|
| 128 |
return steps[0] if steps else "Start by identifying what the question is really asking."
|
|
@@ -139,21 +165,10 @@ def _compose_reply(
|
|
| 139 |
|
| 140 |
if intent in {"walkthrough", "step_by_step", "explain", "method", "concept"}:
|
| 141 |
if not steps:
|
| 142 |
-
if reveal_answer and internal:
|
| 143 |
-
return f"The result is {internal}."
|
| 144 |
return "I can explain the method, but I do not have enough structured steps yet."
|
| 145 |
|
| 146 |
shown_steps = steps if verbosity >= 0.66 else steps[: min(3, len(steps))]
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
if reveal_answer and internal:
|
| 150 |
-
return f"{body}\n\nThat gives {internal}."
|
| 151 |
-
return body
|
| 152 |
-
|
| 153 |
-
if reveal_answer and internal:
|
| 154 |
-
if result.answer_value:
|
| 155 |
-
return f"The answer is {result.answer_value}."
|
| 156 |
-
return f"The result is {internal}."
|
| 157 |
|
| 158 |
if steps:
|
| 159 |
return steps[0]
|
|
@@ -338,7 +353,6 @@ def _build_retrieval_query(
|
|
| 338 |
raw = (raw_user_text or "").strip()
|
| 339 |
question = (question_text or "").strip()
|
| 340 |
|
| 341 |
-
# Prefer the actual math content, not the conversational wrapper
|
| 342 |
if question:
|
| 343 |
parts.append(question)
|
| 344 |
elif raw:
|
|
@@ -389,6 +403,7 @@ def _build_retrieval_query(
|
|
| 389 |
|
| 390 |
return " ".join(parts).strip()
|
| 391 |
|
|
|
|
| 392 |
class ConversationEngine:
|
| 393 |
def __init__(
|
| 394 |
self,
|
|
@@ -429,8 +444,9 @@ class ConversationEngine:
|
|
| 429 |
|
| 430 |
resolved_intent = intent or detect_intent(user_text, help_mode)
|
| 431 |
resolved_help_mode = help_mode or intent_to_help_mode(resolved_intent)
|
| 432 |
-
reveal_answer = resolved_help_mode == "answer" or transparency >= 0.8
|
| 433 |
|
|
|
|
|
|
|
| 434 |
result = SolverResult(
|
| 435 |
domain="general",
|
| 436 |
solved=False,
|
|
@@ -460,7 +476,6 @@ class ConversationEngine:
|
|
| 460 |
reply = _compose_reply(
|
| 461 |
result=result,
|
| 462 |
intent=resolved_intent,
|
| 463 |
-
reveal_answer=reveal_answer,
|
| 464 |
verbosity=verbosity,
|
| 465 |
category=inferred_category,
|
| 466 |
)
|
|
@@ -533,6 +548,12 @@ class ConversationEngine:
|
|
| 533 |
|
| 534 |
reply = format_reply(reply, tone, verbosity, transparency, resolved_help_mode)
|
| 535 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
result.reply = reply
|
| 537 |
result.help_mode = resolved_help_mode
|
| 538 |
result.meta = {
|
|
|
|
| 114 |
return lines
|
| 115 |
|
| 116 |
|
| 117 |
+
def _safe_steps(steps: List[str]) -> List[str]:
|
| 118 |
+
"""
|
| 119 |
+
Defensive cleanup in case any solver step accidentally contains
|
| 120 |
+
explicit final-answer phrasing.
|
| 121 |
+
"""
|
| 122 |
+
cleaned: List[str] = []
|
| 123 |
+
banned_patterns = [
|
| 124 |
+
r"\bthe answer is\b",
|
| 125 |
+
r"\banswer:\b",
|
| 126 |
+
r"\bthat gives\b",
|
| 127 |
+
r"\btherefore\b",
|
| 128 |
+
r"\bso x\s*=",
|
| 129 |
+
r"\bso y\s*=",
|
| 130 |
+
r"\bx\s*=",
|
| 131 |
+
r"\by\s*=",
|
| 132 |
+
r"\bresult is\b",
|
| 133 |
+
]
|
| 134 |
+
|
| 135 |
+
for step in steps:
|
| 136 |
+
s = (step or "").strip()
|
| 137 |
+
lowered = s.lower()
|
| 138 |
+
if any(re.search(p, lowered) for p in banned_patterns):
|
| 139 |
+
continue
|
| 140 |
+
cleaned.append(s)
|
| 141 |
+
|
| 142 |
+
return cleaned
|
| 143 |
+
|
| 144 |
+
|
| 145 |
def _compose_reply(
|
| 146 |
result: SolverResult,
|
| 147 |
intent: str,
|
|
|
|
| 148 |
verbosity: float,
|
| 149 |
category: Optional[str] = None,
|
| 150 |
) -> str:
|
| 151 |
+
steps = _safe_steps(result.steps or [])
|
|
|
|
| 152 |
|
| 153 |
if intent == "hint":
|
| 154 |
return steps[0] if steps else "Start by identifying what the question is really asking."
|
|
|
|
| 165 |
|
| 166 |
if intent in {"walkthrough", "step_by_step", "explain", "method", "concept"}:
|
| 167 |
if not steps:
|
|
|
|
|
|
|
| 168 |
return "I can explain the method, but I do not have enough structured steps yet."
|
| 169 |
|
| 170 |
shown_steps = steps if verbosity >= 0.66 else steps[: min(3, len(steps))]
|
| 171 |
+
return "\n".join(f"- {s}" for s in shown_steps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
if steps:
|
| 174 |
return steps[0]
|
|
|
|
| 353 |
raw = (raw_user_text or "").strip()
|
| 354 |
question = (question_text or "").strip()
|
| 355 |
|
|
|
|
| 356 |
if question:
|
| 357 |
parts.append(question)
|
| 358 |
elif raw:
|
|
|
|
| 403 |
|
| 404 |
return " ".join(parts).strip()
|
| 405 |
|
| 406 |
+
|
| 407 |
class ConversationEngine:
|
| 408 |
def __init__(
|
| 409 |
self,
|
|
|
|
| 444 |
|
| 445 |
resolved_intent = intent or detect_intent(user_text, help_mode)
|
| 446 |
resolved_help_mode = help_mode or intent_to_help_mode(resolved_intent)
|
|
|
|
| 447 |
|
| 448 |
+
# Critical change:
|
| 449 |
+
# never reveal answers from this layer, regardless of transparency/help mode
|
| 450 |
result = SolverResult(
|
| 451 |
domain="general",
|
| 452 |
solved=False,
|
|
|
|
| 476 |
reply = _compose_reply(
|
| 477 |
result=result,
|
| 478 |
intent=resolved_intent,
|
|
|
|
| 479 |
verbosity=verbosity,
|
| 480 |
category=inferred_category,
|
| 481 |
)
|
|
|
|
| 548 |
|
| 549 |
reply = format_reply(reply, tone, verbosity, transparency, resolved_help_mode)
|
| 550 |
|
| 551 |
+
# Critical change:
|
| 552 |
+
# never expose final answer fields to Unity/client
|
| 553 |
+
result.answer_letter = None
|
| 554 |
+
result.answer_value = None
|
| 555 |
+
result.internal_answer = None
|
| 556 |
+
|
| 557 |
result.reply = reply
|
| 558 |
result.help_mode = resolved_help_mode
|
| 559 |
result.meta = {
|