finalize mcq and add test cases

parent 923f5898
......@@ -363,51 +363,69 @@ class AgentService:
self, grade: int, subject: str, unit: str, concept: str, is_arabic: bool, count: int
) -> List[Dict]:
"""
Generates a dynamic quiz of 'count' questions for a specific topic.
It ensures a portion of the questions are newly generated.
Generates a dynamic quiz of 'count' questions using a hybrid approach:
1. Always generates a "freshness batch" of new questions.
2. Retrieves all questions and checks if the total meets the 'count'.
3. If not, generates the remaining number of questions needed.
"""
if not self.pgvector:
raise HTTPException(status_code=503, detail="Vector service is not available for this feature.")
# 1. Calculate how many new questions to generate
# Logic: at least 1, up to 1/3 of the quiz size, with a max cap of 5.
num_new_questions = min(max(1, math.floor(count / 3)), 5)
logger.info(f"Request for {count} questions. Will generate {num_new_questions} new ones.")
# --- PART 1: Follow the original logic to ensure freshness ---
# 2. Generate and store the new questions
# 1. Calculate how many new questions to generate for freshness.
num_fresh_questions = min(max(1, math.floor(count / 3)), 5)
logger.info(f"Request for {count} questions. Step 1: Generating {num_fresh_questions} new 'freshness' questions.")
# 2. Generate and store these new "freshness" questions.
try:
self.generate_and_store_mcqs(
grade=grade,
subject=subject,
unit=unit,
concept=concept,
is_arabic=is_arabic,
num_questions=num_new_questions
grade=grade, subject=subject, unit=unit, concept=concept,
is_arabic=is_arabic, num_questions=num_fresh_questions
)
except Exception as e:
# If generation fails, we can still proceed with existing questions.
logger.warning(f"Could not generate new questions for the quiz due to an error: {e}")
# 3. Retrieve ALL available questions for the topic from the database
all_mcqs = self.pgvector.get_mcqs(
grade=grade,
subject=subject,
unit=unit,
concept=concept,
is_arabic=is_arabic,
limit=None # Retrieve all
logger.warning(f"Could not generate 'freshness' questions for the quiz due to an error: {e}")
# --- PART 2: Check for a shortfall and generate more if needed ---
# 3. Retrieve ALL available questions for the topic from the database.
all_mcqs_after_freshness = self.pgvector.get_mcqs(
grade=grade, subject=subject, unit=unit, concept=concept,
is_arabic=is_arabic, limit=None
)
if not all_mcqs:
raise HTTPException(status_code=404, detail="No questions could be found or generated for this topic.")
# 4. Calculate if there is still a shortfall.
questions_still_needed = count - len(all_mcqs_after_freshness)
# 4. Randomly select the desired number of questions from the full pool
# First, shuffle the entire list of questions
random.shuffle(all_mcqs)
# 5. If we still need more questions, generate the exact number missing.
if questions_still_needed > 0:
logger.info(f"After freshness batch, have {len(all_mcqs_after_freshness)} questions. Generating {questions_still_needed} more to meet count of {count}.")
# Cap this second generation step as a safeguard.
num_to_generate_gap = min(questions_still_needed, 10)
try:
self.generate_and_store_mcqs(
grade=grade, subject=subject, unit=unit, concept=concept,
is_arabic=is_arabic, num_questions=num_to_generate_gap
)
except Exception as e:
logger.warning(f"Could not generate the remaining {num_to_generate_gap} questions due to an error: {e}")
# --- PART 3: Final Assembly and Return ---
# 6. Retrieve the final, complete list of ALL questions.
final_pool = self.pgvector.get_mcqs(
grade=grade, subject=subject, unit=unit, concept=concept,
is_arabic=is_arabic, limit=None
)
if not final_pool:
raise HTTPException(status_code=404, detail="No questions could be found or generated for this topic.")
# Then, return a slice of the list with the requested count
# This gracefully handles cases where we have fewer questions than requested.
final_quiz = all_mcqs[:count]
# 7. Randomly select the desired number of questions from the final pool.
random.shuffle(final_pool)
final_quiz = final_pool[:count]
logger.info(f"Returning a dynamic quiz of {len(final_quiz)} questions for '{concept}'.")
return final_quiz
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment