j-js commited on
Commit
b63ce3c
·
verified ·
1 Parent(s): 462a39a

Update generator_engine.py

Browse files
Files changed (1) hide show
  1. generator_engine.py +61 -52
generator_engine.py CHANGED
@@ -1,73 +1,82 @@
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."
 
 
 
 
 
 
 
1
  from __future__ import annotations
2
 
 
3
 
4
+ def normalize_category(category: str | None) -> str:
5
+ c = (category or "").strip().lower()
6
 
7
+ if c in {"quantitative", "quant", "q", "math"}:
8
+ return "Quantitative"
9
+ if c in {"datainsight", "data_insight", "data insight", "di", "data"}:
10
+ return "DataInsight"
11
+ if c in {"verbal", "v"}:
12
+ return "Verbal"
13
+ if c in {"general", "", "unknown", "none", "null"}:
14
+ return "General"
15
 
16
+ return category or "General"
17
 
 
 
18
 
19
+ def classify_question(question_text: str, category: str | None = None) -> dict:
20
+ q = (question_text or "").lower()
21
+ normalized = normalize_category(category)
 
 
 
 
 
22
 
23
+ if normalized == "Quantitative":
24
+ if ("percent" in q or "%" in q) and any(
25
+ k in q for k in ["then", "after", "followed by", "successive", "increase", "decrease", "discount"]
26
+ ):
27
+ return {"category": normalized, "topic": "percent", "type": "successive_percent"}
28
 
29
+ if "percent" in q or "%" in q:
30
+ return {"category": normalized, "topic": "percent", "type": "percent_change"}
31
 
32
+ if "ratio" in q or ":" in q:
33
+ return {"category": normalized, "topic": "ratio", "type": "ratio_total"}
34
 
35
+ if "probability" in q or "chosen at random" in q:
36
+ return {"category": normalized, "topic": "probability", "type": "simple_probability"}
37
 
38
+ if "divisible" in q or "remainder" in q or "mod" in q:
39
+ return {"category": normalized, "topic": "number_theory", "type": "remainder_or_divisibility"}
40
 
41
+ if "|" in q:
42
+ return {"category": normalized, "topic": "algebra", "type": "absolute_value"}
43
 
44
+ if any(k in q for k in ["circle", "radius", "circumference", "triangle", "perimeter", "area"]):
45
+ return {"category": normalized, "topic": "geometry", "type": "geometry"}
46
 
47
+ if any(k in q for k in ["average", "mean", "median"]):
48
+ return {"category": normalized, "topic": "statistics", "type": "average"}
49
 
50
+ if "sequence" in q:
51
+ return {"category": normalized, "topic": "sequence", "type": "sequence"}
 
 
 
 
 
 
 
 
52
 
53
+ if "=" in q:
54
+ return {"category": normalized, "topic": "algebra", "type": "equation"}
55
 
56
+ return {"category": normalized, "topic": "quant", "type": "general"}
 
 
 
 
 
 
57
 
58
+ if normalized == "DataInsight":
59
+ if "percent" in q or "%" in q:
60
+ return {"category": normalized, "topic": "percent", "type": "percent_change"}
61
+ if any(k in q for k in ["mean", "median", "distribution"]):
62
+ return {"category": normalized, "topic": "statistics", "type": "distribution"}
63
+ if any(k in q for k in ["correlation", "scatter", "trend", "table", "chart"]):
64
+ return {"category": normalized, "topic": "data", "type": "correlation_or_graph"}
65
+ return {"category": normalized, "topic": "data", "type": "general"}
66
 
67
+ if normalized == "Verbal":
68
+ if "meaning" in q or "definition" in q:
69
+ return {"category": normalized, "topic": "vocabulary", "type": "definition"}
70
+ if "grammatically" in q or "sentence correction" in q:
71
+ return {"category": normalized, "topic": "grammar", "type": "sentence_correction"}
72
+ if "argument" in q or "author" in q:
73
+ return {"category": normalized, "topic": "reasoning", "type": "argument_analysis"}
74
+ return {"category": normalized, "topic": "verbal", "type": "general"}
75
 
76
+ if any(k in q for k in ["percent", "%", "ratio", "remainder", "divisible", "probability", "circle", "triangle", "="]):
77
+ return classify_question(question_text, "Quantitative")
78
+
79
+ if any(k in q for k in ["table", "chart", "scatter", "trend", "distribution"]):
80
+ return classify_question(question_text, "DataInsight")
81
+
82
+ return {"category": "General", "topic": "unknown", "type": "unknown"}