File size: 1,358 Bytes
70f23d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75c8d26
70f23d5
75c8d26
 
70f23d5
75c8d26
 
70f23d5
75c8d26
 
70f23d5
75c8d26
 
 
 
 
 
 
 
 
 
 
 
70f23d5
 
 
d198bbf
70f23d5
 
 
 
 
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
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