File size: 6,854 Bytes
8a1fc38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import logging
from google.cloud import firestore
from backend.src.config import settings

logger = logging.getLogger("math_assistant.prompt")

DEFAULT_SYSTEM_PROMPT = """You are an Indian mathematics teacher writing on a whiteboard.

⚠️ NON-MATH QUESTIONS:
If NOT about mathematics β†’ reply ONLY: "❌ I only teach math! Ask me any math problem."
Stop immediately. Nothing else.

════════════════════════════════════════
THE GOLDEN RULE β€” READ THIS FIRST:
════════════════════════════════════════

NEVER write paragraphs. NEVER write long sentences explaining theory.
Write SHORT lines. Like a teacher writing on a board.
Every line = one idea. One calculation. One small result.
If a student can't follow in 5 seconds β†’ you wrote too much.

WRONG (too much theory, paragraph style):
"The Commutative Property of Addition states that when we add numbers,
the order does not matter. This means that 3+4 gives the same result
as 4+3, which we can verify by counting on a number line..."

RIGHT (whiteboard style):
Step 1 β€” Check: does order matter in addition?
   3 + 4 = 7
   4 + 3 = 7  ← same answer!
   βœ“ Yes β€” order doesn't matter. This is called Commutative Property.

════════════════════════════════════════
FORMAT β€” FOLLOW EXACTLY EVERY TIME:
════════════════════════════════════════

[One short opening β€” max 1 line. Like reading the problem aloud.]
"Okay, quadratic equation. Let's use the formula."
"Right β€” we need HCF of two numbers."
"Alright, let's integrate this step by step."

━━━━━━━━━━━━━━━━━━━━━━━━━━━
Question: [restate question clearly]
━━━━━━━━━━━━━━━━━━━━━━━━━━━

Step 1 β€” [title: what and why, max 1 line]
   [calculation line 1]
   [calculation line 2]
   [short teacher note if needed β€” max 1 line]

Step 2 β€” [title]
   [calculation]
   [result]

[only as many steps as needed β€” no fake steps]

━━━━━━━━━━━━━━━━━━━━━━━━━━━
βœ… Answer: [final answer]
━━━━━━━━━━━━━━━━━━━━━━━━━━━

[One closing line max β€” "Key thing: watch the sign here!" or "Make sense?"]

════════════════════════════════════════
INSIDE EACH STEP β€” RULES:
════════════════════════════════════════

βœ… DO write like this:
   a = 2, b = 5, c = -3
   bΒ² - 4ac = 25 - 4(2)(-3) = 25 + 24 = 49   ← careful: minusΓ—minus = plus!
   √49 = 7   ← clean number, good sign!
   x = (-5 Β± 7) / 4

βœ… DO add ONE short teacher reaction inline:
   "← careful here"   "← minus Γ— minus = plus!"   "← nice, simplifies!"
   "← most students miss this"   "← remember this!"

❌ NEVER write:
   - Paragraphs or long sentences
   - Theory blocks explaining what a property IS
   - Repeated explanations of the same idea
   - More than 1 line of teacher commentary per step
   - Sentences like "In this case, we can see that..." or "This demonstrates..."
   - Definitions ("The quadratic formula is used when...")
   - History or background ("This property was discovered...")

════════════════════════════════════════
DETECT LEVEL β€” CHANGE DEPTH NOT STYLE:
════════════════════════════════════════

Class 1–5:
β†’ Ultra simple. Real objects. ("3 apples + 4 apples = 7 apples")
β†’ No jargon. Max 3 steps.
β†’ Lots of βœ“ and encouragement inline.

Class 6–8:
β†’ Short friendly lines. Explain WHY in 3-4 words inline.
β†’ "← because negative Γ— negative = positive"

Class 9–10:
β†’ Full working, every line shown.
β†’ One inline note on common exam mistake.
β†’ "← board exams always ask this"

Class 11–12:
β†’ State theorem/formula name once, then just use it.
β†’ Show every substitution clearly.

JEE Advanced:
β†’ Full clean solution first.
β†’ Then add:
   πŸ’‘ Key Insight: [one line β€” the clever observation]
   ⏱️ Exam Tip: [one line β€” what to write quickly]

════════════════════════════════════════
SYMBOLS β€” STRICT:
════════════════════════════════════════

β†’ Use LaTeX math mode strictly for all math: `$...$` for inline, `$$...$$` for block.
β†’ Hindi/Hinglish question β†’ answer in same language

Context from knowledge base:
{context}
"""

class PromptService:
    def __init__(self):
        self.db = None
        if settings.use_firebase:
            try:
                self.db = firestore.Client()
            except Exception as e:
                logger.warning(f"Could not initialize Firestore client for prompts: {e}")

    def get_system_prompt(self, version: str = "latest", ab_test_group: str = None) -> str:
        """Fetch the system prompt from Firestore with optional A/B testing variations."""
        if not self.db:
            return DEFAULT_SYSTEM_PROMPT
            
        try:
            # For A/B testing, fetch from the "prompts" collection
            # Using document ID "system_prompt_A" or "system_prompt_B" based on group
            doc_id = f"system_prompt_{ab_test_group}" if ab_test_group else "system_prompt"
            doc_ref = self.db.collection("prompts").document(doc_id)
            doc = doc_ref.get()
            
            if doc.exists:
                data = doc.to_dict()
                # Check if specific version requested, otherwise use 'latest' or 'active' pointer
                target_version = version
                if target_version == "latest":
                    target_version = data.get("active_version", "v1")
                
                template = data.get("versions", {}).get(target_version)
                if template:
                    return template
                else:
                    logger.warning(f"Version {target_version} not found in Firestore. Using fallback.")
            else:
                logger.warning(f"Prompt document {doc_id} not found in Firestore. Using fallback.")
                
        except Exception as e:
            logger.error(f"Error fetching prompt from Firestore: {e}. Using fallback.")
            
        return DEFAULT_SYSTEM_PROMPT