Update generator_engine.py
Browse files- generator_engine.py +127 -80
generator_engine.py
CHANGED
|
@@ -1,82 +1,129 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
+
from typing import List, Optional
|
| 4 |
+
|
| 5 |
+
try:
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
except Exception:
|
| 8 |
+
pipeline = None
|
| 9 |
+
|
| 10 |
+
from models import RetrievedChunk
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class GeneratorEngine:
|
| 14 |
+
def __init__(self, model_name: str = "google/flan-t5-small"):
|
| 15 |
+
self.model_name = model_name
|
| 16 |
+
self.pipe = None
|
| 17 |
+
|
| 18 |
+
if pipeline is not None:
|
| 19 |
+
try:
|
| 20 |
+
self.pipe = pipeline("text2text-generation", model=model_name)
|
| 21 |
+
except Exception:
|
| 22 |
+
self.pipe = None
|
| 23 |
+
|
| 24 |
+
def available(self) -> bool:
|
| 25 |
+
return self.pipe is not None
|
| 26 |
+
|
| 27 |
+
def _notes_block(self, retrieval_context: List[RetrievedChunk]) -> str:
|
| 28 |
+
if not retrieval_context:
|
| 29 |
+
return ""
|
| 30 |
+
lines = []
|
| 31 |
+
for chunk in retrieval_context[:3]:
|
| 32 |
+
text = (chunk.text or "").strip().replace("\n", " ")
|
| 33 |
+
if len(text) > 220:
|
| 34 |
+
text = text[:217].rstrip() + "…"
|
| 35 |
+
lines.append(f"- {chunk.topic}: {text}")
|
| 36 |
+
return "\n".join(lines)
|
| 37 |
+
|
| 38 |
+
def _template_fallback(
|
| 39 |
+
self,
|
| 40 |
+
user_text: str,
|
| 41 |
+
question_text: Optional[str],
|
| 42 |
+
topic: str,
|
| 43 |
+
intent: str,
|
| 44 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 45 |
+
) -> str:
|
| 46 |
+
notes = self._notes_block(retrieval_context or [])
|
| 47 |
+
|
| 48 |
+
if intent == "hint":
|
| 49 |
+
base = "Start by identifying the exact relationship between the quantities before doing any arithmetic."
|
| 50 |
+
elif intent in {"instruction", "method"}:
|
| 51 |
+
base = "Translate the wording into an equation, ratio, or percent relationship, then solve one step at a time."
|
| 52 |
+
elif intent in {"walkthrough", "step_by_step", "explain", "concept"}:
|
| 53 |
+
base = "First identify what the question is asking, then map the values into the correct quantitative structure, and only then compute."
|
| 54 |
+
else:
|
| 55 |
+
base = "This does not match a strong solver rule yet, so begin by identifying the target quantity and the relationship connecting the numbers."
|
| 56 |
+
|
| 57 |
+
if notes:
|
| 58 |
+
return f"{base}\n\nRelevant notes:\n{notes}"
|
| 59 |
+
return base
|
| 60 |
+
|
| 61 |
+
def _build_prompt(
|
| 62 |
+
self,
|
| 63 |
+
user_text: str,
|
| 64 |
+
question_text: Optional[str],
|
| 65 |
+
topic: str,
|
| 66 |
+
intent: str,
|
| 67 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 68 |
+
) -> str:
|
| 69 |
+
question = (question_text or user_text or "").strip()
|
| 70 |
+
notes = self._notes_block(retrieval_context or [])
|
| 71 |
+
|
| 72 |
+
prompt = [
|
| 73 |
+
"You are a concise GMAT tutor.",
|
| 74 |
+
f"Topic: {topic or 'general'}",
|
| 75 |
+
f"Intent: {intent or 'answer'}",
|
| 76 |
+
"",
|
| 77 |
+
f"Question: {question}",
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
if notes:
|
| 81 |
+
prompt.extend(["", "Relevant teaching notes:", notes])
|
| 82 |
+
|
| 83 |
+
prompt.extend(
|
| 84 |
+
[
|
| 85 |
+
"",
|
| 86 |
+
"Respond briefly and clearly.",
|
| 87 |
+
"If the problem is not fully solvable from the parse, give the next best method step.",
|
| 88 |
+
"Do not invent facts.",
|
| 89 |
+
]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return "\n".join(prompt)
|
| 93 |
+
|
| 94 |
+
def generate(
|
| 95 |
+
self,
|
| 96 |
+
user_text: str,
|
| 97 |
+
question_text: Optional[str] = None,
|
| 98 |
+
topic: str = "",
|
| 99 |
+
intent: str = "answer",
|
| 100 |
+
retrieval_context: Optional[List[RetrievedChunk]] = None,
|
| 101 |
+
chat_history=None,
|
| 102 |
+
max_new_tokens: int = 96,
|
| 103 |
+
**kwargs,
|
| 104 |
+
) -> Optional[str]:
|
| 105 |
+
prompt = self._build_prompt(
|
| 106 |
+
user_text=user_text,
|
| 107 |
+
question_text=question_text,
|
| 108 |
+
topic=topic,
|
| 109 |
+
intent=intent,
|
| 110 |
+
retrieval_context=retrieval_context or [],
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if self.pipe is not None:
|
| 114 |
+
try:
|
| 115 |
+
out = self.pipe(prompt, max_new_tokens=max_new_tokens, do_sample=False)
|
| 116 |
+
if out and isinstance(out, list):
|
| 117 |
+
text = str(out[0].get("generated_text", "")).strip()
|
| 118 |
+
if text:
|
| 119 |
+
return text
|
| 120 |
+
except Exception:
|
| 121 |
+
pass
|
| 122 |
+
|
| 123 |
+
return self._template_fallback(
|
| 124 |
+
user_text=user_text,
|
| 125 |
+
question_text=question_text,
|
| 126 |
+
topic=topic,
|
| 127 |
+
intent=intent,
|
| 128 |
+
retrieval_context=retrieval_context or [],
|
| 129 |
+
)
|