File size: 5,596 Bytes
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import json
import time
from groq import Groq
from dotenv import load_dotenv
from pydantic import BaseModel, Field, ValidationError
from typing import List, Optional, Dict

load_dotenv('.env.local')

# Configure Groq API
API_KEY = os.getenv("GROQ_API_KEY")
client = None
if API_KEY:
    client = Groq(api_key=API_KEY)

# Pydantic models for robust validation
class Question(BaseModel):
    question_text: str
    marks: Optional[int] = 0
    bloom_level: Optional[str] = "Understand"
    type: Optional[str] = "Descriptive"
    options: Optional[List[str]] = None
    correct_answer: Optional[str] = None
    marking_scheme: Optional[str] = None
    estimated_time_mins: Optional[int] = None
    competency: Optional[str] = None
    chapter: Optional[str] = None
    internal_choice_question_text: Optional[str] = None

class Section(BaseModel):
    section_name: str
    instructions: str
    questions: List[Question]

class ExamPaper(BaseModel):
    title: str
    sections: List[Section]

def generate_questions(content, configure_data, file_path=None, retry_count=2):
    """
    Generates questions using Groq AI (Llama 3.3) with pydantic validation and retry logic.
    """
    if not API_KEY or not client:
        return {"error": "Groq API Key not configured"}

    model_name = "llama-3.3-70b-versatile"
    system_prompt = "You are an expert academic examiner. Output ONLY valid JSON."

    # Format prompts (reused from original)
    blooms_dist = configure_data.get('bloomsDistribution', {})
    question_types = configure_data.get('questionTypes', [])
    difficulty_score = configure_data.get('difficulty', 50)
    advanced = configure_data.get('advancedSettings', {})

    blooms_str = ", ".join([f"{k.capitalize()} ({v}%)" for k, v in blooms_dist.items() if v > 0])
    
    pattern_str = ""
    total_requested_questions = 0
    num_sections = 0
    for qt in question_types:
        if qt.get('selected'):
            count = int(qt.get('count', 0))
            if count > 0:
                label = qt.get('label', 'Question')
                topic = qt.get('topic', '').strip()
                choices = int(qt.get('internalChoices', 0))
                req = f"- Section for '{label}': EXACTLY {count} questions, {qt.get('marks', 0)} marks each."
                if topic: req += f" Focus on: '{topic}'."
                if choices > 0: req += f" {choices} questions with 'internal_choice_question_text'."
                if qt.get('id') == 'mcq' or 'choice' in label.lower(): req += " Include 4 options."
                pattern_str += req + "\n"
                total_requested_questions += count
                num_sections += 1
            
    diff_label = "Easy" if difficulty_score < 30 else "Hard" if difficulty_score > 70 else "Medium"
    
    adv_str = ""
    if advanced.get('answerKey'): adv_str += "- Provide correct_answer.\n"
    if advanced.get('markingScheme'): adv_str += "- Provide marking_scheme.\n"
    if advanced.get('timeEstimate'): adv_str += "- Provide estimated_time_mins.\n"
    
    user_prompt = f"""
    Analyze content and generate exam JSON.
    CONTENT: {content[:15000]}
    BLOOMS: {blooms_str}
    DIFFICULTY: {diff_label}
    SECTIONS: {pattern_str}
    {adv_str}
    JSON STRUCTURE: {{ title: str, sections: [{{ section_name: str, instructions: str, questions: [{{ question_text: str, ... }}] }}] }}
    """

    for attempt in range(retry_count + 1):
        try:
            completion = client.chat.completions.create(
                model=model_name,
                messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}],
                temperature=0.7,
                response_format={"type": "json_object"}
            )
            raw_result = json.loads(completion.choices[0].message.content)
            
            # Use Pydantic to validate and clean the data
            validated_paper = ExamPaper(**raw_result)
            return validated_paper.model_dump()

        except ValidationError as ve:
            print(f"Pydantic Validation Error (Attempt {attempt+1}): {ve}")
            if attempt == retry_count:
                return {"error": "AI generated invalid data structure after multiple attempts."}
        except Exception as e:
            if "rate_limit" in str(e).lower():
                time.sleep(2)  # Short wait before retry
                continue
            return {"error": str(e)}
    
    return {"error": "Failed to generate valid exam paper."}

def regenerate_single_question(content, question_data, context_paper_title=""):
    """
    Regenerates a single specific question with pydantic validation.
    """
    if not API_KEY or not client:
        return {"error": "Groq API Key not configured"}

    model_name = "llama-3.3-70b-versatile"
    system_prompt = "You are an expert academic examiner. Output ONLY raw JSON question object."

    user_prompt = f"""
    Regenerate this question but keep structure.
    SOURCE: {content[:10000]}
    EXISTING: {json.dumps(question_data)}
    CONTEXT: {context_paper_title}
    """

    try:
        completion = client.chat.completions.create(
            model=model_name,
            messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}],
            temperature=0.8,
            response_format={"type": "json_object"}
        )
        raw_q = json.loads(completion.choices[0].message.content)
        validated_q = Question(**raw_q)
        return validated_q.model_dump()
    except Exception as e:
        return {"error": str(e)}