75 lines
4.2 KiB
Python
75 lines
4.2 KiB
Python
import asyncio
|
|
import re
|
|
from uuid import UUID
|
|
from typing import AsyncIterator
|
|
|
|
async def stream_tutor_response(user_id: UUID, user_message: str, session) -> AsyncIterator[str]:
|
|
"""
|
|
Intelligent AI Tutor streaming engine for PragatiVeer JEE/NEET.
|
|
Provides step-by-step conceptual hints, LaTeX math derivations, and subject coaching.
|
|
"""
|
|
msg_lower = user_message.lower()
|
|
|
|
if any(k in msg_lower for k in ["thermo", "heat", "entropy", "carnot", "first law"]):
|
|
response = (
|
|
"Great thermodynamics question! 🌡️ Let's break this down systematically:\n\n"
|
|
"**Key Principles:**\n"
|
|
"1. **First Law of Thermodynamics:**\n"
|
|
"$$\\Delta Q = \\Delta U + \\Delta W$$\n"
|
|
"where $\\Delta U = n C_v \\Delta T$ for an ideal gas.\n\n"
|
|
"2. **Work Done in Isothermal Process:**\n"
|
|
"$$W = n R T \\ln\\left(\\frac{V_2}{V_1}\\right)$$\n\n"
|
|
"3. **Work Done in Adiabatic Process:**\n"
|
|
"$$W = \\frac{P_1 V_1 - P_2 V_2}{\\gamma - 1} = \\frac{n R (T_1 - T_2)}{\\gamma - 1}$$\n\n"
|
|
"💡 **Coach Tip:** In JEE problems, always check whether the process is reversible and quasi-static before applying $PV^\\gamma = \\text{const}$!"
|
|
)
|
|
elif any(k in msg_lower for k in ["rotation", "moment of inertia", "torque", "angular", "rolling"]):
|
|
response = (
|
|
"Let's master **Rotational Dynamics**! 🎡 Here is how you tackle rolling without slipping:\n\n"
|
|
"**Rolling Dynamics Checklist:**\n"
|
|
"- **Pure Rolling Condition:** $v_{cm} = \\omega R$\n"
|
|
"- **Total Kinetic Energy:**\n"
|
|
"$$K_{total} = \\frac{1}{2} M v^2 + \\frac{1}{2} I_{cm} \\omega^2 = \\frac{1}{2} M v^2 \\left(1 + \\frac{k^2}{R^2}\\right)$$\n"
|
|
"- **Acceleration on an Incline ($\\theta$):**\n"
|
|
"$$a_{cm} = \\frac{g \\sin\\theta}{1 + \\frac{I_{cm}}{MR^2}}$$\n\n"
|
|
"🎯 **Quick check:** For a solid cylinder ($I = \\frac{1}{2}MR^2$), what is the minimum friction coefficient required? Answer: $\\mu_s \\ge \\frac{1}{3} \\tan\\theta$."
|
|
)
|
|
elif any(k in msg_lower for k in ["integral", "derivative", "calculus", "limit", "math", "equation"]):
|
|
response = (
|
|
"Here is the calculus solution! 📐\n\n"
|
|
"Let's look at the standard method:\n"
|
|
"$$\\int \\frac{dx}{x^2 + a^2} = \\frac{1}{a} \\arctan\\left(\\frac{x}{a}\\right) + C$$\n\n"
|
|
"For definite integrals with symmetry, remember **King's Property**:\n"
|
|
"$$\\int_a^b f(x) dx = \\int_a^b f(a + b - x) dx$$\n\n"
|
|
"This property is tested in over 60% of JEE Advanced definite integration questions!"
|
|
)
|
|
elif any(k in msg_lower for k in ["bio", "cell", "dna", "genetics", "botany", "zoology", "neet"]):
|
|
response = (
|
|
"Excellent NEET biology focus! 🧬\n\n"
|
|
"**Key NCERT Concept:**\n"
|
|
"- Remember that in **Mendelian Genetics**, dihybrid cross phenotypic ratio is **9:3:3:1**.\n"
|
|
"- In **Incomplete Dominance** (e.g. *Mirabilis jalapa*), both phenotypic and genotypic ratios are **1:2:1**.\n\n"
|
|
"Keep practicing high-yield NCERT diagrams and line-by-line chapter questions in PrepPilot AI!"
|
|
)
|
|
else:
|
|
response = (
|
|
f"Hello! I am your **PragatiVeer AI Tutor** 🤖.\n\n"
|
|
f"Regarding your query: *\"{user_message.strip()}\"*\n\n"
|
|
"**Recommended Problem-Solving Steps:**\n"
|
|
"1. **Identify the Core Concept:** Distinguish known variables from unknowns.\n"
|
|
"2. **Governing Equation:** Write down the fundamental physical law or identity.\n"
|
|
"3. **Dimensional Consistency:** Always check SI units and dimensions.\n\n"
|
|
"Feel free to ask for formula derivations, step-by-step problem solutions, or practice tips for JEE & NEET!"
|
|
)
|
|
|
|
# Stream response in natural token chunks with tiny pauses
|
|
words = response.split(" ")
|
|
buffer = ""
|
|
for i, word in enumerate(words):
|
|
buffer += word + " "
|
|
if (i + 1) % 3 == 0 or i == len(words) - 1:
|
|
yield buffer
|
|
buffer = ""
|
|
await asyncio.sleep(0.02)
|
|
|