File size: 4,315 Bytes
c1bc991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8c4827
c1bc991
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8c4827
c1bc991
 
 
 
 
 
 
 
 
 
 
 
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
# agents/quiz_agent.py
"""
Quiz Generation Agent - Creates quizzes and flashcards using Generative AI.
"""
import re

class QuizAgent:
    def __init__(self, gemini_model=None):
        """
        Initializes the agent with the Gemini model.

        Args:
            gemini_model: An instance of the Gemini model client.
        """
        self.model = gemini_model

    def _extract_topic(self, query: str) -> str:
        """A simple helper to extract the core topic from the user's query."""
        # Remove common phrases used to request a quiz
        patterns = [
            r"make a quiz on",
            r"create a quiz on",
            r"give me a quiz on",
            r"quiz on",
            r"quiz about",
            r"test me on"
        ]
        topic = query.lower()
        for p in patterns:
            topic = re.sub(p, "", topic)
        
        # Clean up any extra whitespace
        return topic.strip()

    def process_query(self, query: str, file_context: str = "",chat_history: list = None):
        """
        Processes a query to generate a quiz. The agent prioritizes file_context if provided.

        Args:
            query (str): The user's full query (e.g., "Make a quiz on analgesics").
            file_context (str): Optional text content from an uploaded file.
        
        Returns:
            dict: A dictionary containing the quiz and agent metadata.
        """
        if not self.model:
            return {
                'message': "❓ **Quiz Master**\n\nThe question bank is locked! The Gemini API key is missing, so I can't generate quizzes. Please configure the API key to enable this feature.",
                'agent_type': 'quiz_generation',
                'status': 'error_no_api_key'
            }

        topic = self._extract_topic(query)
        task_description = f"Generate a short quiz (3-5 questions) for a B.Pharmacy student on the topic: **{topic.title()}**."
        
        if file_context:
            task_description += f"\n\nIf relevant, use the following text from the student's uploaded notes for additional context:\n---\n{file_context}\n---"
        
        else:
            return {
                'message': "Please tell me what to quiz you on! Either upload a file or ask for a quiz on a specific topic, like 'quiz on antibiotics'.",
                'agent_type': 'quiz_generation',
                'status': 'error_no_topic'
            }

        # Construct a specialized prompt for the Gemini model
        prompt = f"""
You are "Quiz Master," an AI that creates engaging and effective study quizzes for B.Pharmacy students in India.

**Your Task:**
{task_description}

**Instructions:**
1.  **Question Variety:** Create a mix of question types:
    * Multiple Choice Questions (MCQs) with 4 options.
    * True/False questions.
    * Fill-in-the-Blank questions.
2.  **Clarity:** Ensure questions are clear, concise, and relevant.
3.  **Answer Key:** THIS IS ESSENTIAL. After all the questions, provide a clearly separated "πŸ”‘ Answer Key" section with the correct answers. For MCQs, also provide a brief (one-sentence) explanation for why the answer is correct.
4.  **Formatting:** Use markdown for headings, bolding, and lists. Use emojis to make it fun and engaging.
Good luck! 🌟
**Example Output Structure:**

πŸ“ **Quiz Time: [Topic Name]**

**Q1. [MCQ Question]**
    A) Option 1
    B) Option 2
    ...

**Q2. [True/False Question]**

**Q3. [Fill-in-the-Blank Question]**

---
πŸ”‘ **Answer Key**
1.  **Answer:** B) Correct Option. *Explanation: [Brief reason why B is correct].*
2.  **Answer:** True.
3.  **Answer:** [Correct word(s)].

Let's test your knowledge! Good luck! 🌟
"""

        try:
            # Generate content using the AI model
            ai_response = self.model.generate_content(prompt, chat_history)
            return {
                'message': ai_response.text,
                'agent_used': 'quiz_generation',
                'status': 'success'
            }
        except Exception as e:
            print(f"Quiz Agent Error: {e}")
            return {
                'message': f"I'm sorry, my question book seems to be stuck. I ran into an error: {str(e)}",
                'agent_type': 'quiz_generation',
                'status': 'error_api_call'
            }