File size: 2,200 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
# api/courseware/vision_builder.py
"""
Course Vision & Structure Builder:课程定位、学习目标、层级化知识树。
"""
from typing import Optional, List, Tuple

from api.config import client, DEFAULT_MODEL, USE_WEAVIATE
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 COURSE_VISION_SYSTEM, COURSE_VISION_USER_TEMPLATE


def build_course_vision(
    course_info: str,
    syllabus: str,
    max_tokens: int = 2000,
    history: Optional[list] = None,
) -> str:
    """
    输入:课程基本信息 + 教学大纲。
    输出:课程定位、学习目标、层级化知识树;含 References。
    """
    query = f"{course_info}\n{syllabus}"[:2000]
    rag_context, refs = get_rag_context_with_refs(query, top_k=8, max_context_chars=5000)
    ref_instruction = inject_refs_instruction(refs)
    user_content = COURSE_VISION_USER_TEMPLATE.format(
        course_info=course_info.strip() or "(未提供)",
        syllabus=syllabus.strip() or "(未提供)",
        rag_context=rag_context or "(无检索到知识库摘录,将基于通用课程设计经验回答。)",
        ref_instruction=ref_instruction,
    )
    messages = [{"role": "system", "content": COURSE_VISION_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