← 返回首页
🧠

测验生成:AI创建测试题目

📂 llm ⏱ 3 min 479 words

--- title: "测验生成:AI创建测试题目" description: "使用LLM自动生成各种类型的测验题目" tags: ["测验生成", "题目生成", "AI测试", "LLM", "教育评估"] category: "llm" icon: "📝"

测验生成:AI创建测试题目

测验生成概述

测验生成是利用LLM自动创建各种类型测试题目的技术,包括选择题、填空题、简答题等。

核心功能

1. 选择题生成

from openai import OpenAI
from typing import Dict, List

class MultipleChoiceGenerator:
    """选择题生成器"""
    
    def __init__(self, model: str = "gpt-4"):
        self.client = OpenAI()
        self.model = model
    
    def generate_questions(self, topic: str, num_questions: int = 10,
                          difficulty: str = "中等") -> str:
        """生成选择题"""
        prompt = f"""请为"{topic}"生成{num_questions}道选择题。

难度:{difficulty}

要求:
1. 每题4个选项(A、B、C、D)
2. 只有一个正确答案
3. 包含答案和解析
4. 题目清晰无歧义"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个测验设计专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.4
        )
        
        return response.choices[0].message.content
    
    def generate_distractors(self, correct_answer: str, topic: str,
                            num_distractors: int = 3) -> List[str]:
        """生成干扰项"""
        prompt = f"""为以下正确答案生成{num_distractors}个看似合理但错误的干扰项:

正确答案:{correct_answer}
主题:{topic}

要求:
1. 与正确答案相关
2. 常见错误答案
3. 有一定迷惑性"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个选择题设计专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.5
        )
        
        # 解析干扰项
        items = response.choices[0].message.content.strip().split("\n")
        distractors = [item.strip() for item in items if item.strip()]
        
        return distractors[:num_distractors]

class QuestionAnalyzer:
    """题目分析器"""
    
    def __init__(self, model: str = "gpt-4"):
        self.client = OpenAI()
        self.model = model
    
    def analyze_question_quality(self, question: str) -> Dict:
        """分析题目质量"""
        prompt = f"""请分析以下选择题的质量:

{question}

请从以下方面评估:
1. 题目清晰度
2. 选项合理性
3. 难度适当性
4. 干扰项质量
5. 改进建议"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个测验评估专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.3
        )
        
        return {"analysis": response.choices[0].message.content}

2. 填空题生成

class FillInBlankGenerator:
    """填空题生成器"""
    
    def __init__(self, model: str = "gpt-4"):
        self.client = OpenAI()
        self.model = model
    
    def generate_questions(self, topic: str, num_questions: int = 10) -> str:
        """生成填空题"""
        prompt = f"""请为"{topic}"生成{num_questions}道填空题。

要求:
1. 空格位置明确
2. 答案唯一
3. 包含答案
4. 语境完整"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个填空题设计专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.4
        )
        
        return response.choices[0].message.content
    
    def generate_from_text(self, text: str, num_blanks: int = 5) -> str:
        """从文本生成填空题"""
        prompt = f"""请从以下文本中提取关键信息,生成{num_blanks}道填空题:

文本:
{text}

要求:
1. 提取重要概念
2. 空格位置合理
3. 保持句子完整
4. 提供答案"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个文本分析专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.4
        )
        
        return response.choices[0].message.content

3. 简答题生成

class ShortAnswerGenerator:
    """简答题生成器"""
    
    def __init__(self, model: str = "gpt-4"):
        self.client = OpenAI()
        self.model = model
    
    def generate_questions(self, topic: str, num_questions: int = 5,
                          depth: str = "中等") -> str:
        """生成简答题"""
        prompt = f"""请为"{topic}"生成{num_questions}道简答题。

深度:{depth}

要求:
1. 问题清晰
2. 需要理解和分析
3. 包含评分标准
4. 提供参考答案"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个简答题设计专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.5
        )
        
        return response.choices[0].message.content
    
    def generate_rubric(self, question: str) -> str:
        """生成评分标准"""
        prompt = f"""请为以下简答题生成详细的评分标准:

问题:{question}

请提供:
1. 评分维度
2. 每个维度的分数
3. 详细描述
4. 常见错误"""
        
        response = self.client.chat.completions.create(
            self.model,
            messages=[
                {"role": "system", "content": "你是一个评估标准设计专家。"},
                {"role": "user", "content": prompt}
            ],
            temperature=0.4
        )
        
        return response.choices[0].message.content

测验生成工作流

class QuizGenerationWorkflow:
    """测验生成工作流"""
    
    def __init__(self):
        self.mc_generator = MultipleChoiceGenerator()
        self.fill_generator = FillInBlankGenerator()
        self.short_generator = ShortAnswerGenerator()
        self.analyzer = QuestionAnalyzer()
    
    def create_comprehensive_quiz(self, topic: str, 
                                 difficulty: str = "中等") -> Dict:
        """创建综合测验"""
        # 生成各类题目
        multiple_choice = self.mc_generator.generate_questions(
            topic, 5, difficulty
        )
        
        fill_blank = self.fill_generator.generate_questions(topic, 5)
        
        short_answer = self.short_generator.generate_questions(topic, 3)
        
        # 分析题目质量
        analysis = self.analyzer.analyze_question_quality(multiple_choice)
        
        return {
            "topic": topic,
            "difficulty": difficulty,
            "multiple_choice": multiple_choice,
            "fill_blank": fill_blank,
            "short_answer": short_answer,
            "analysis": analysis
        }

# 使用示例
workflow = QuizGenerationWorkflow()
quiz = workflow.create_comprehensive_quiz("二次方程", "中等")

print(f"主题:{quiz['topic']}")
print(f"难度:{quiz['difficulty']}")
print("\n选择题(前200字):")
print(quiz['multiple_choice'][:200])

最佳实践

  1. 覆盖全面:覆盖知识点的不同方面
  2. 难度适当:难度符合学生水平
  3. 清晰明确:题目表述清晰无歧义
  4. 及时更新:定期更新题目库

总结

测验生成是LLM教育应用的重要功能。通过合理使用,可以快速创建高质量的测验题目。