# micro_prompts.py โ€” Textbook JSON / Hebrew Centric # Topic-specific micro-prompts for BuddyMath # ALL TEMPLATES: enforce [{type: "text"|"math", content: "..."}] JSON only. # ZERO mentions of ctx, sp, sympy, MathEngine, or Python. """ Micro-Prompt Library Each entry adds topic-specific focus on TOP of the specialist prompt in prompts.py. The specialist prompt already defines the full Textbook JSON schema โ€” these just focus the LLM on which formula/technique to use. """ import json # needed by get_general_prompt # ==================== MICRO-PROMPT TEMPLATES ==================== # Each template is a SHORT topic-focused instruction. # It is prepended to the full specialist prompt (which already has the JSON schema). # DO NOT add any output format instructions here โ€” the specialist prompt handles that. MICRO_PROMPTS = { # ========== GEOMETRY ========== "CIRCLE_EQUATION": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืฉื•ื•ืืช ืžืขื’ืœ / ืžืงื•ื ื’ื™ืื•ืžื˜ืจื™ ื”ืฉืชืžืฉ ื‘ื ื•ืกื—ื”: (x-a)ยฒ + (y-b)ยฒ = rยฒ ื–ื”ื”: ืžืจื›ื– (a,b) ื•ืจื“ื™ื•ืก r ืžื”ื ืชื•ื ื™ื. ื”ืฆื‘. ืคืฉื˜. ื”ื’ื“ืจ.

""", "tokens": 60, "requires": [] }, "CIRCLE_TANGENT": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืฉื™ืง ืœืžืขื’ืœ ื‘ื ืงื•ื“ื” ืฉืœื‘ 1: ื‘ื“ื•ืง ืฉื”ื ืงื•ื“ื” ืขืœ ื”ืžืขื’ืœ. ืฉืœื‘ 2: ืžืฆื ืฉื™ืคื•ืข ื”ืจื“ื™ื•ืก (ืžื”ืžืจื›ื– ืœื ืงื•ื“ื”). ืฉืœื‘ 3: ืฉื™ืคื•ืข ื”ืžืฉื™ืง = -1 / ืฉื™ืคื•ืข ื”ืจื“ื™ื•ืก (ืžืื•ื ืš). ื›ืชื•ื‘ ืžืฉื•ื•ืืช ื™ืฉืจ.""", "tokens": 70, "requires": [] }, "TRIANGLE_AREA": { "template": """๐ŸŽฏ ื ื•ืฉื: ืฉื˜ื— ืžืฉื•ืœืฉ ื ื•ืกื—ื”: S = ยฝ ร— ื‘ืกื™ืก ร— ื’ื•ื‘ื”. ื”ืฆื‘ ื•ื—ืฉื‘.""", "tokens": 40, "requires": [] }, "LINE_EQUATION": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืฉื•ื•ืืช ื™ืฉืจ ืžืฆื ืฉื™ืคื•ืข m ื•ื ืงื•ื“ืช ื—ื™ืชื•ืš b. ื›ืชื•ื‘: y = mx + b.""", "tokens": 40, "requires": [] }, "LINE_PERPENDICULAR": { "template": """๐ŸŽฏ ื ื•ืฉื: ื™ืฉืจ ืžืื•ื ืš ื›ืœืœ: mโ‚ ร— mโ‚‚ = -1. ืžืฆื ืฉื™ืคื•ืข ื”ืžืื•ื ืš, ื”ืฆื‘ ื ืงื•ื“ื”, ื›ืชื•ื‘ ืžืฉื•ื•ืื”.""", "tokens": 50, "requires": [] }, "DISTANCE_FORMULA": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืจื—ืง ื‘ื™ืŸ ืฉืชื™ ื ืงื•ื“ื•ืช ื ื•ืกื—ื”: d = โˆš[(xโ‚‚-xโ‚)ยฒ + (yโ‚‚-yโ‚)ยฒ]. ื”ืฆื‘ ืžืกืคืจื™ื. ืคืฉื˜.""", "tokens": 50, "requires": [] }, # ========== CALCULUS ========== "DERIVATIVE_POWER": { "template": """๐ŸŽฏ ื ื•ืฉื: ื ื’ื–ืจืช โ€” ื›ืœืœ ื”ื—ื–ืงื” ื›ืœืœ: (xโฟ)' = nยทxโฟโปยน. ื’ื–ื•ืจ ื›ืœ ืื™ื‘ืจ ื‘ื ืคืจื“. ืคืฉื˜.""", "tokens": 50, "requires": [] }, "DERIVATIVE_QUOTIENT": { "template": """๐ŸŽฏ ื ื•ืฉื: ื ื’ื–ืจืช โ€” ื›ืœืœ ื”ืžื ื” ื›ืœืœ: (u/v)' = (u'v - uv') / vยฒ. ื–ื”ื” u ื•-v. ื’ื–ื•ืจ ื›ืœ ืื—ื“. ื”ืฆื‘.""", "tokens": 60, "requires": [] }, "DERIVATIVE_PRODUCT": { "template": """๐ŸŽฏ ื ื•ืฉื: ื ื’ื–ืจืช โ€” ื›ืœืœ ื”ืžื›ืคืœื” ื›ืœืœ: (uยทv)' = u'v + uv'. ื–ื”ื” u ื•-v. ื’ื–ื•ืจ ื›ืœ ืื—ื“. ื”ืฆื‘.""", "tokens": 60, "requires": [] }, "DERIVATIVE_CHAIN": { "template": """๐ŸŽฏ ื ื•ืฉื: ื ื’ื–ืจืช โ€” ื›ืœืœ ื”ืฉืจืฉืจืช ื›ืœืœ: [f(g(x))]' = f'(g(x)) ยท g'(x). ื–ื”ื” ืคื•ื ืงืฆื™ื” ื—ื™ืฆื•ื ื™ืช ื•ืคื ื™ืžื™ืช. ื’ื–ื•ืจ.""", "tokens": 70, "requires": [] }, "INVESTIGATION_EXTREMA": { "template": """๐ŸŽฏ ื ื•ืฉื: ื—ื™ืฉื•ื‘ ื ืงื•ื“ื•ืช ืงื™ืฆื•ืŸ ืฉืœื‘ 1: f'(x) = 0 โ€” ืžืฆื ื ืงื•ื“ื•ืช ืืคืฉืจื™ื•ืช. ืฉืœื‘ 2: ื‘ื“ื•ืง ื‘-f''(x) โ€” f''>0 โ†’ ืžื™ื ื™ืžื•ื, f''<0 โ†’ ืžืงืกื™ืžื•ื. ืฉืœื‘ 3: ื”ืฆื‘ ื‘ื—ื–ืจื” ื‘-f(x) ืœืžืฆื™ืืช ืขืจืš y.""", "tokens": 90, "requires": [] }, "INVESTIGATION_MONOTONICITY": { "template": """๐ŸŽฏ ื ื•ืฉื: ืขืœื™ื” ื•ื™ืจื™ื“ื” ืฉืœ ืคื•ื ืงืฆื™ื” ืžืฆื f'(x). ืคืชื•ืจ f'(x) = 0. ื‘ื“ื•ืง ืกื™ืžืŸ f'(x) ื‘ื›ืœ ืงื˜ืข. f'>0 โ†’ ืขื•ืœื”, f'<0 โ†’ ื™ื•ืจื“ืช.""", "tokens": 80, "requires": [] }, # ========== ALGEBRA ========== "LINEAR_EQUATION": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืฉื•ื•ืื” ืœื™ื ืืจื™ืช ืคืชื•ืจ ืฉืœื‘-ืื—ืจ-ืฉืœื‘: ื‘ื•ื“ื“ x ื‘ืฆื“ ืื—ื“.""", "tokens": 30, "requires": [] }, "QUADRATIC_EQUATION": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืฉื•ื•ืื” ืจื™ื‘ื•ืขื™ืช ื”ืฉืชืžืฉ ื‘ื ื•ืกื—ืช ืคืชืจื•ืŸ: x = [-b ยฑ โˆš(bยฒ-4ac)] / 2a ื—ืฉื‘ ื“ื™ืกืงืจื™ืžื™ื ื ื˜ื”. ืžืฆื ืฉืชื™ ืชืฉื•ื‘ื•ืช (ืื• ืฉื•ืจืฉ ื›ืคื•ืœ).""", "tokens": 60, "requires": [] }, "SYSTEM_EQUATIONS": { "template": """๐ŸŽฏ ื ื•ืฉื: ืžืขืจื›ืช ืžืฉื•ื•ืื•ืช ืคืชื•ืจ ื‘ืฉื™ื˜ืช ื”ืฆื‘ื” ืื• ื”ื—ืกืจื”. ื”ืฆื‘ x ื—ื–ืจื” ืœืžืฉื•ื•ืื” ืœื•ื•ื™ื“ื•ื.""", "tokens": 60, "requires": [] }, } # ==================== PROMPT BUILDER ==================== def get_micro_prompt(topic_id: str, data: dict, grade: str = "10") -> str: """ Returns topic-focused prefix only. Safety filter: Middle school (7-9) cannot use CALCULUS topics. """ if topic_id not in MICRO_PROMPTS: raise KeyError(f"Topic '{topic_id}' not found in MICRO_PROMPTS") # Safety Filter for V4.2.11 is_middle_school = False try: grade_val = int(re.search(r'\d+', str(grade)).group()) is_middle_school = 7 <= grade_val <= 9 except: pass if is_middle_school and ("DERIVATIVE" in topic_id or "INVESTIGATION" in topic_id): print(f"๐Ÿ›ก๏ธ [Safety Filter] Micro-Prompt Filter: Topic {topic_id} blocked for Grade {grade}. Falling back to GENERAL.") return get_general_prompt(data) config = MICRO_PROMPTS[topic_id] template = config["template"] # No required fields anymore โ€” all templates are self-contained focus_block = template.strip() # Prepend focus to the general prompt (which has the anchor + schema) return f"{focus_block}\n\n{get_general_prompt(data)}" def get_prompt_token_count(topic_id: str) -> int: """Get estimated token count for topic's micro-prompt""" return MICRO_PROMPTS.get(topic_id, {}).get("tokens", 60) def get_general_prompt(data_anchor: dict) -> str: """ Clean context provider. Provides the data anchor without conflicting schemas. """ anchor_json = json.dumps(data_anchor, ensure_ascii=False, indent=2) return f"""๐Ÿ“Š [DATA ANCHOR] Problem context (Absolute Truth): {anchor_json} Instruction: Use the specific data above to solve the problem. Do not provide any explanations outside the required JSON structure.""" # ==================== USAGE EXAMPLE ==================== if __name__ == "__main__": test_data = {"equations": ["x^2 - 4 = 0"], "function_equations": ["f(x) = ln(x)/(x^2-4)"]} for topic in ["CIRCLE_EQUATION", "DERIVATIVE_QUOTIENT", "LINEAR_EQUATION"]: prompt = get_micro_prompt(topic, test_data) print(f"=== {topic} ===\n{prompt[:200]}\n")