Spaces:
Runtime error
Runtime error
| # 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' | |
| } |