Tutor (prompt tutor-v3-stepwise-plain): plain text instead of LaTeX/markdown, with rag/services/mathtext.py converting whatever still slips through; full step-by-step solutions with a check before the final answer; three tones by grade; the last 6 messages of the conversation go to the model and follow-ups are no longer served from another user's cache; book solutions the checker disputes are kept out of the context; a questionId gives the tutor the practice question and the student's wrong answer. /api/health reports when the AI credit runs out. rag_model_benchmark compares chat models on verified book problems (run on the server; credit ran out mid-run). Map: only lessons that have questions count toward finishing an island, lessonIds[0] / nextLessonId is always the next lesson to play, and a finished island stays finished. The Seyar characters (sigma/delta/pi) are seeded. Audit fixes, each reproduced before fixing: - the plan is stored on the order, so paying a monthly order after starting a yearly checkout no longer grants a year - opening checkout no longer demotes an active subscription; renewals extend from the current expiry; expired subscriptions are not reported active - one discount code per order and only before the gateway; a 100% code completes the purchase; lost Zarinpal confirmations are re-verified - the unused /api/payment/webhook "mark paid" endpoint is removed - XP counts only answers recorded on the server - 6-digit OTP, each code dies after 5 wrong guesses; POST /api/auth/logout - a guest who signs up keeps their account and progress (test phones excluded) - affiliate commission on the first real payment; guests excluded - parents with several children no longer get a 500 - SQLite runs transactions IMMEDIATE, since it ignores select_for_update - admin login is rate-limited; display names drop phone numbers and links; userQuestion is capped at 2000 characters - server-side heart enforcement exists behind HEARTS_ENFORCED (off until the frontend handles 403 no_hearts) Not done on purpose: rotating the server's secret key and moving to Postgres. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
"""
|
|
کشِ پاسخِ RAG برای سوالِ عیناً تکراری.
|
|
|
|
کلید = هشِ (collection + مقدارِ فیلترِ درس/فصل + پایه + متنِ نرمالشدهی سوال).
|
|
فقط مرحلهی گران (embedding + جستجوی Qdrant + تولیدِ GPT) رو رد میکنه؛
|
|
کوتای روزانه، idempotency و ثبتِ Message همچنان در view اجرا میشن.
|
|
"""
|
|
import hashlib
|
|
|
|
from django.conf import settings
|
|
from django.core.cache import cache
|
|
|
|
# v2: پایه هم واردِ کلید شد (پایین را ببینید). نسخهی prefix عمداً جلو رفت تا
|
|
# کلیدهای v1 که پایه در آنها نبود، خودبهخود بیاثر شوند.
|
|
_PREFIX = 'rag:ans:v2:'
|
|
|
|
|
|
def ttl() -> int:
|
|
return int(getattr(settings, 'RAG_ANSWER_CACHE_TTL', 0))
|
|
|
|
|
|
def enabled() -> bool:
|
|
return ttl() > 0
|
|
|
|
|
|
def _norm(text: str) -> str:
|
|
return ' '.join((text or '').split()).strip().lower()
|
|
|
|
|
|
def make_key(collection, filter_value, question: str, grade_title: str = '') -> str:
|
|
"""
|
|
⚠️ `grade_title` جزءِ کلید است چون تولیدِ پاسخ از این به بعد پایهآگاه است
|
|
(لحن و سطحِ توضیح از پایه میآید). بدونِ آن، وقتی subject/فصل مشخص نیست
|
|
collection و filter هر دو None میشوند و کلید فقط به متنِ سوال میماند —
|
|
یعنی پاسخی که برای دانشآموزِ نهم ساخته شده عیناً به دانشآموزِ سوم
|
|
برمیگشت.
|
|
"""
|
|
from .generation import PROMPT_VERSION
|
|
|
|
# نسخهی پرامپت هم جزءِ کلید است: بعد از عوض شدنِ پرامپت، جوابِ نسخهی
|
|
# قبلی (مثلاً با LaTeX) نباید تا پایانِ TTL دوباره سرو شود.
|
|
raw = (f'{PROMPT_VERSION}|{collection}|{filter_value}|'
|
|
f'{(grade_title or "").strip()}|{_norm(question)}')
|
|
return _PREFIX + hashlib.sha1(raw.encode('utf-8')).hexdigest()
|
|
|
|
|
|
def cacheable(history) -> bool:
|
|
"""
|
|
فقط پیامِ اولِ هر گفتگو کش میشود. «نفهمیدم» یا «یه مثال دیگه بزن» معنایش
|
|
به گفتگوی قبلی بسته است؛ پیشتر کلید فقط متنِ سوال بود و هر «نفهمیدم» در
|
|
همان کتاب، جوابِ کششدهی گفتگوی یک نفرِ دیگر را میگرفت.
|
|
"""
|
|
return not history
|
|
|
|
|
|
def get(key: str) -> dict | None:
|
|
if not enabled():
|
|
return None
|
|
return cache.get(key)
|
|
|
|
|
|
def set(key: str, payload: dict) -> None: # noqa: A003
|
|
if not enabled():
|
|
return
|
|
cache.set(key, payload, ttl())
|