j-js commited on
Commit
a08b987
·
verified ·
1 Parent(s): 35cb4eb

Update generator_engine.py

Browse files
Files changed (1) hide show
  1. 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
- try:
6
- from transformers import pipeline
7
- except Exception:
8
- pipeline = None
9
 
10
 
11
  class GeneratorEngine:
12
- def __init__(self, model_name: str = "google/flan-t5-small"):
13
- self.model_name = model_name
14
- self.pipe = None
15
- if pipeline is not None:
16
- try:
17
- self.pipe = pipeline("text2text-generation", model=model_name)
18
- except Exception:
19
- self.pipe = None
20
-
21
- def available(self) -> bool:
22
- return self.pipe is not None
23
-
24
- def generate(self, prompt: str, max_new_tokens: int = 96) -> Optional[str]:
25
- if self.pipe is None:
26
- return None
27
- try:
28
- out = self.pipe(prompt, max_new_tokens=max_new_tokens, do_sample=False)
29
- if out and isinstance(out, list):
30
- return str(out[0].get("generated_text", "")).strip()
31
- except Exception:
32
- return None
33
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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."