sanatan_ai / modules /quiz /answer_validator.py
vikramvasudevan's picture
Upload folder using huggingface_hub
d198bbf verified
from openai import OpenAI
from modules.quiz.models import AnswerValidation
from modules.quiz.quiz_helper import Question
client = OpenAI()
def validate_answer(
question: Question, user_answer: str, preferred_language: str = "English"
) -> AnswerValidation:
"""
Validate a user's answer against a Question object.
Uses LLM reasoning to account for synonyms, variations, and explanation.
"""
print("validating answer ...")
prompt = f"""
You are an answer validator for a scripture-based quiz.
Question:
{question.question}
Choices (if any):
{question.choices}
Expected Answer:
{question.expected_answer}
User's Answer:
{user_answer}
Rules:
- Check strictly against the expected answer and choices.
- Accept semantically equivalent answers (e.g., synonyms, transliterations).
- Respond in {question.preferred_language}.
- Address the user directly, e.g., "You nailed it!" or "Not quite, here's why…".
- Do NOT repeat the user's answer verbatim.
- Give reasoning concisely, and optionally include the expected answer if user was wrong.
- Only validate correctness; do not invent new answers.
"""
response = client.chat.completions.parse(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
response_format=AnswerValidation,
)
return response.choices[0].message.parsed