Spaces:
Sleeping
Sleeping
| # api/courseware/qa_optimizer.py | |
| """ | |
| Course QA Optimizer:基于学生答题数据(Smart Quiz)分析弱点,自动优化后续教学建议。 | |
| """ | |
| from typing import Optional, List, Tuple | |
| from api.config import client, DEFAULT_MODEL | |
| from api.courseware.rag import get_rag_context_with_refs, inject_refs_instruction | |
| from api.courseware.references import append_references_to_content | |
| from api.courseware.prompts import QA_OPTIMIZER_SYSTEM, QA_OPTIMIZER_USER_TEMPLATE | |
| def optimize_from_quiz_data( | |
| quiz_summary: str, | |
| course_topic: Optional[str] = None, | |
| max_tokens: int = 1500, | |
| history: Optional[list] = None, | |
| ) -> str: | |
| """ | |
| 基于 Smart Quiz 答题数据摘要,分析薄弱点并给出后续教学优化建议。 | |
| """ | |
| query = f"{quiz_summary}\n{course_topic or ''}"[:2000] | |
| rag_context, refs = get_rag_context_with_refs(query, top_k=6, max_context_chars=4000) | |
| ref_instruction = inject_refs_instruction(refs) | |
| user_content = QA_OPTIMIZER_USER_TEMPLATE.format( | |
| quiz_summary=quiz_summary.strip() or "(未提供答题数据)", | |
| course_topic=(course_topic or "(未指定)").strip(), | |
| rag_context=rag_context or "(无检索到知识库摘录。)", | |
| ref_instruction=ref_instruction, | |
| ) | |
| messages = [{"role": "system", "content": QA_OPTIMIZER_SYSTEM}] | |
| if history: | |
| for user_msg, assistant_msg in history[-10:]: | |
| if user_msg: | |
| messages.append({"role": "user", "content": user_msg}) | |
| if assistant_msg: | |
| messages.append({"role": "assistant", "content": assistant_msg}) | |
| messages.append({"role": "user", "content": user_content}) | |
| try: | |
| resp = client.chat.completions.create( | |
| model=DEFAULT_MODEL, | |
| messages=messages, | |
| temperature=0.4, | |
| max_tokens=max_tokens, | |
| timeout=90, | |
| ) | |
| out = (resp.choices[0].message.content or "").strip() | |
| except Exception as e: | |
| out = f"生成失败:{e}。请稍后重试。" | |
| if refs and "## References" not in out and "[Source:" not in out: | |
| out = append_references_to_content(out, refs) | |
| return out | |