RAG 在智能题库中的应用:相似题检索和难度评估方案

发布时间:2026/7/23 11:51:38
RAG 在智能题库中的应用:相似题检索和难度评估方案 RAG 在智能题库中的应用相似题检索和难度评估方案一、搜一元二次方程结果返回了 3000 道题学生挑了最难的开始做在线教育平台最大的资源浪费之一题库里 20 万道题学生却找不到适合自己的那一道。关键词搜索二次函数能返回 3000 条结果但这些题目按照录入时间排序而不是按难度排序导致学生要么做了太简单的题浪费时间要么做了太难的题打击信心直接退出。理想的智能题库需要解决两个问题一是找相似题给定一道题找到与其考察知识点和解题思路相似的题目二是评估难度这道题对当前学生来说有多难。前者用向量检索后者需要结合题目属性、学生历史表现和 IRT项目反应理论来建模。二、相似题检索与难度评估的系统架构核心设计是向量检索 难度预测的双路协同核心公式推荐得分 相似度_score × 0.6 难度适配_score × 0.4。相似度保证找对题难度适配保证适合学生。两者权重可以通过 A/B 测试调优。三、Python 实现相似题检索 IRT 难度模型import numpy as np from typing import List, Dict, Tuple, Optional from dataclasses import dataclass from scipy.special import expit # sigmoid 函数 dataclass class Question: 题库中的题目 question_id: str text: str subject: str knowledge_ids: List[str] question_type: str # choice, fill, proof difficulty_param: float # IRT b 参数难度 discrimination: float # IRT a 参数区分度 guess_param: float 0.25 # IRT c 参数猜测概率 embedding: Optional[np.ndarray] None dataclass class StudentAbility: 学生能力估计 student_id: str theta: float # IRT 能力值 θ theta_std: float # 能力值标准差不确定性 n_questions: int # 已作答题目数 class IRTDifficultyModel: 基于 IRT 的难度评估模型 staticmethod def probability_correct( theta: float, question: Question ) - float: 3PL IRT 模型P(correct|θ) a, b, c ( question.discrimination, question.difficulty_param, question.guess_param, ) return c (1 - c) * expit(1.702 * a * (theta - b)) staticmethod def information( theta: float, question: Question ) - float: 题目信息量用于选择最优下一题 q_val 1.702 * question.discrimination p IRTDifficultyModel.probability_correct( theta, question ) return (q_val ** 2 * (p - question.guess_param) ** 2 * (1 - p) / (p * (1 - question.guess_param))) staticmethod def update_ability( student: StudentAbility, question: Question, correct: bool, ) - StudentAbility: 贝叶斯更新学生能力值简化 EAP 估计 prob IRTDifficultyModel.probability_correct( student.theta, question ) # 似然函数 × 先验简化版 if correct: likelihood prob else: likelihood 1 - prob # 梯度方向更新 learning_rate 0.1 / (1 0.01 * student.n_questions) gradient correct - prob new_theta student.theta learning_rate * gradient return StudentAbility( student_idstudent.student_id, thetanew_theta, theta_stdstudent.theta_std * 0.95, # 随数据增加降低不确定性 n_questionsstudent.n_questions 1, ) class SmartQuestionBank: 智能题库 def __init__(self): self.questions: Dict[str, Question] {} # 倒排索引知识点 - 题目 ID 列表 self.knowledge_index: Dict[str, List[str]] {} # 学生能力模型 self.students: Dict[str, StudentAbility] {} def add_question(self, q: Question): 添加题目到题库 self.questions[q.question_id] q for kid in q.knowledge_ids: self.knowledge_index.setdefault(kid, []).append( q.question_id ) def search_similar( self, query_embedding: np.ndarray, top_k: int 10, min_similarity: float 0.85, ) - List[Tuple[Question, float]]: 向量检索相似题 results [] for q in self.questions.values(): if q.embedding is None: continue sim np.dot(query_embedding, q.embedding) / ( np.linalg.norm(query_embedding) * np.linalg.norm(q.embedding) 1e-8 ) if sim min_similarity: results.append((q, float(sim))) results.sort(keylambda x: x[1], reverseTrue) return results[:top_k] def recommend_by_ability( self, student_id: str, target_knowledge_id: str, top_k: int 5, strategy: str i_plus_1, ) - List[Tuple[Question, float]]: 基于学生能力推荐题目 student self.students.get(student_id) if student is None: # 新学生默认中等能力 student StudentAbility( student_idstudent_id, theta0.0, theta_std1.0, n_questions0, ) self.students[student_id] student # 获取该知识点的所有题目 candidate_ids self.knowledge_index.get( target_knowledge_id, [] ) if not candidate_ids: return [] scored [] for qid in candidate_ids: q self.questions.get(qid) if q is None: continue # 难度适配分 if strategy i_plus_1: # i1 策略推荐略高于当前能力的题 difficulty_match 1.0 - abs( q.difficulty_param - (student.theta 0.5) ) / 3.0 else: # 最近发展区策略 difficulty_match 1.0 - abs( q.difficulty_param - student.theta ) / 3.0 # 信息量加权 info IRTDifficultyModel.information( student.theta, q ) final_score ( 0.4 * difficulty_match 0.3 * info 0.3 * q.discrimination ) scored.append((q, final_score)) scored.sort(keylambda x: x[1], reverseTrue) return scored[:top_k] def hybrid_recommend( self, student_id: str, query_text: str, query_embedding: np.ndarray, top_k: int 10, ) - List[Tuple[Question, float, str]]: 混合推荐相似度 能力匹配 # 1. 向量检索相似题 similar self.search_similar( query_embedding, top_ktop_k * 3 ) # 2. 能力匹配过滤 student self.students.get(student_id) if student is None: # 新学生只按相似度排序 return [(q, sim, similarity) for q, sim in similar[:top_k]] # 3. 综合评分 scored [] for q, sim in similar: ability_score 1.0 - abs( q.difficulty_param - student.theta ) / 3.0 final 0.6 * sim 0.4 * ability_score scored.append((q, final, hybrid)) scored.sort(keylambda x: x[1], reverseTrue) return scored[:top_k]四、边界分析与 Trade-offs向量检索的精度 vs 效率全库做余弦相似度计算在 20 万量级还能撑约 200ms到 100 万题量就需要向量数据库。Milvus 和 Qdrant 都能满足但部署维护成本不低。小规模题库用 FAISS 的 IVF 索引内存中是性价比最高的方案。IRT 模型的数据需求3PL IRT 模型要求每个题目至少有 100-200 次作答数据才能稳定估计 a、b、c 参数。新题没有足够数据时可以用题目属性题型、字数、知识点难度做回归估计初始参数然后用在线学习逐步修正。i1 策略的风险持续推荐略高于能力的题目如果学生连续做错挫败感累积。实践中需要设置错误容忍窗口——连续 3 次错误后策略自动切换为复习模式推荐学生已经掌握的知识点题目来恢复信心。Embedding 模型的领域适配通用 Sentence-BERT 在数学题 Embedding 上效果一般相似度区分度不够。如果能用题库本身的数据做对比学习微调SimCSE 方法Top-10 召回率可以从 72% 提升到 91%。微调需要 5 万对正负样本成本可控。五、总结智能题库的两个核心技术是向量检索找相似题和IRT 模型评估难度。前者保证检索相关性后者保证难度适配性。两者融合的策略权重0.6 : 0.4需要通过实验确定没有通用最优值。工程上需要注意向量检索的索引构建和增量更新、IRT 参数的冷启动估计、以及难度推荐策略的容错机制。最终目标是让学生感觉每道题都刚刚好——既不会太简单觉得无聊也不会太难想要放弃。