question_generator.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. AI 自动出题器
  3. 基于药典原文 + 考试大纲知识点,使用 LLM 自动生成 A/B/X 型试题
  4. """
  5. import json
  6. import logging
  7. from typing import Optional
  8. from dataclasses import dataclass
  9. logger = logging.getLogger(__name__)
  10. @dataclass
  11. class GeneratedQuestion:
  12. question_type: str # A / B / X
  13. subject: str
  14. chapter_id: str
  15. difficulty: int
  16. content: str
  17. options: list[str]
  18. answer: str
  19. explanation: str
  20. source: str
  21. knowledge_point_ids: list[str]
  22. class QuestionGenerator:
  23. def __init__(self, llm_client=None):
  24. self.llm = llm_client
  25. def generate(
  26. self,
  27. knowledge_point: dict,
  28. drug_reference: Optional[dict] = None,
  29. question_type: str = "A",
  30. count: int = 3,
  31. ) -> list[GeneratedQuestion]:
  32. """
  33. Phase 2 实现。
  34. 基于知识点 + 药典原文,生成指定类型和数量的题目。
  35. """
  36. return []
  37. def generate_batch(
  38. self,
  39. knowledge_points: list[dict],
  40. question_counts: dict = None,
  41. ) -> list[GeneratedQuestion]:
  42. """批量出题,按题型比例分配"""
  43. if question_counts is None:
  44. question_counts = {"A": 4, "B": 3, "X": 3}
  45. return []
  46. def validate_answer(self, question: GeneratedQuestion) -> bool:
  47. """
  48. 验证答案是否能在原文中找到明确依据。
  49. 若验证失败,标记需要人工审核。
  50. """
  51. return True
  52. def export_jsonl(self, questions: list[GeneratedQuestion], output_path: str):
  53. """导出为 JSONL 格式,用于人工审核"""
  54. with open(output_path, "w", encoding="utf-8") as f:
  55. for q in questions:
  56. f.write(json.dumps({
  57. "question_type": q.question_type,
  58. "subject": q.subject,
  59. "chapter_id": q.chapter_id,
  60. "difficulty": q.difficulty,
  61. "content": q.content,
  62. "options": q.options,
  63. "answer": q.answer,
  64. "explanation": q.explanation,
  65. "source": q.source,
  66. "knowledge_point_ids": q.knowledge_point_ids,
  67. }, ensure_ascii=False) + "\n")
  68. logger.info(f"Exported {len(questions)} questions to {output_path}")