Spaces:
Running
Running
File size: 2,837 Bytes
0cde401 6a91b07 0cde401 6a91b07 0cde401 6a91b07 0cde401 6a91b07 0cde401 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | # api/courseware/teaching_copilot.py
"""
Teaching Copilot & Student Adaptation:课堂实时辅助,按学生画像(Name, Progress, Behavior)动态调整建议。
"""
from typing import Optional, List, Dict, Any, 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 TEACHING_COPILOT_SYSTEM, TEACHING_COPILOT_USER_TEMPLATE
def _format_student_profiles(profiles: List[Dict[str, Any]]) -> str:
"""将学生画像列表格式化为可读文本。"""
if not profiles:
return "(未提供)"
lines = []
for i, p in enumerate(profiles[:20], 1):
name = p.get("name") or p.get("Name") or "—"
progress = p.get("progress") or p.get("Progress") or "—"
behavior = p.get("behavior") or p.get("Behavior") or "—"
lines.append(f"- 学生{i}: Name={name}, Progress={progress}, Behavior={behavior}")
return "\n".join(lines)
def teaching_copilot(
current_content: str,
student_profiles: Optional[List[Dict[str, Any]]] = None,
max_tokens: int = 1200,
history: Optional[list] = None,
) -> str:
"""
课堂实时辅助:根据当前授课内容与学生画像给出即时建议与个性化调整。
"""
query = current_content[:1500]
rag_context, refs = get_rag_context_with_refs(query, top_k=6, max_context_chars=3500)
ref_instruction = inject_refs_instruction(refs)
profiles_text = _format_student_profiles(student_profiles or [])
user_content = TEACHING_COPILOT_USER_TEMPLATE.format(
current_content=current_content.strip() or "(未提供当前内容)",
student_profiles=profiles_text,
rag_context=rag_context or "(无检索到知识库摘录。)",
ref_instruction=ref_instruction,
)
messages = [{"role": "system", "content": TEACHING_COPILOT_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.5,
max_tokens=max_tokens,
timeout=60,
)
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
|