Update generator_engine.py
Browse files- generator_engine.py +67 -27
generator_engine.py
CHANGED
|
@@ -1,33 +1,73 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
-
from typing import Optional
|
| 4 |
|
| 5 |
-
|
| 6 |
-
from transformers import pipeline
|
| 7 |
-
except Exception:
|
| 8 |
-
pipeline = None
|
| 9 |
|
| 10 |
|
| 11 |
class GeneratorEngine:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
from typing import List, Optional
|
| 4 |
|
| 5 |
+
from models import RetrievedChunk
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
class GeneratorEngine:
|
| 9 |
+
|
| 10 |
+
def __init__(self):
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
def _build_prompt(
|
| 14 |
+
self,
|
| 15 |
+
user_text: str,
|
| 16 |
+
question_text: Optional[str],
|
| 17 |
+
topic: str,
|
| 18 |
+
intent: str,
|
| 19 |
+
retrieval_context: List[RetrievedChunk],
|
| 20 |
+
) -> str:
|
| 21 |
+
|
| 22 |
+
context_lines = []
|
| 23 |
+
for chunk in retrieval_context:
|
| 24 |
+
context_lines.append(f"- {chunk.text}")
|
| 25 |
+
|
| 26 |
+
context_block = "\n".join(context_lines)
|
| 27 |
+
|
| 28 |
+
prompt = f"""
|
| 29 |
+
You are a GMAT quantitative tutor.
|
| 30 |
+
|
| 31 |
+
Topic: {topic}
|
| 32 |
+
Intent: {intent}
|
| 33 |
+
|
| 34 |
+
Question:
|
| 35 |
+
{question_text or user_text}
|
| 36 |
+
|
| 37 |
+
Relevant teaching notes:
|
| 38 |
+
{context_block}
|
| 39 |
+
|
| 40 |
+
Explain how to approach the problem without immediately revealing the answer.
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
return prompt.strip()
|
| 44 |
+
|
| 45 |
+
def generate(
|
| 46 |
+
self,
|
| 47 |
+
user_text: str,
|
| 48 |
+
question_text: Optional[str] = None,
|
| 49 |
+
topic: str = "",
|
| 50 |
+
intent: str = "answer",
|
| 51 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 52 |
+
chat_history=None,
|
| 53 |
+
**kwargs,
|
| 54 |
+
) -> str:
|
| 55 |
+
|
| 56 |
+
retrieval_context = retrieval_context or []
|
| 57 |
+
|
| 58 |
+
prompt = self._build_prompt(
|
| 59 |
+
user_text,
|
| 60 |
+
question_text,
|
| 61 |
+
topic,
|
| 62 |
+
intent,
|
| 63 |
+
retrieval_context,
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# simple fallback explanation system
|
| 67 |
+
if intent == "hint":
|
| 68 |
+
return "Focus on identifying the relationship between the quantities before calculating."
|
| 69 |
+
|
| 70 |
+
if intent in {"method", "walkthrough", "instruction"}:
|
| 71 |
+
return "Start by translating the wording of the problem into an equation or numerical relationship."
|
| 72 |
+
|
| 73 |
+
return "Let’s work through the structure of the problem before jumping to the calculation."
|