File size: 2,331 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
# api/courseware/content_generator.py
"""
Content Generator:生成 Markdown 详细教案,并导出可用于生成 PPT 的结构化数据。
"""
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 CONTENT_GENERATOR_SYSTEM, CONTENT_GENERATOR_LESSON_PLAN_TEMPLATE


def generate_lesson_plan_and_ppt_data(
    topic: str,
    duration: Optional[str] = None,
    outline_points: Optional[str] = None,
    max_tokens: int = 2800,
    history: Optional[list] = None,
) -> str:
    """
    生成 Markdown 详细教案 + 可用于 PPT 的结构化数据(每页 title、bullets、speaker_notes)。
    """
    query = f"{topic}\n{outline_points or ''}"[: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 = CONTENT_GENERATOR_LESSON_PLAN_TEMPLATE.format(
        topic=topic.strip() or "(未提供主题)",
        duration=(duration or "1 课时").strip(),
        outline_points=(outline_points or "(未提供,请根据主题生成)").strip(),
        rag_context=rag_context or "(无检索到知识库摘录。)",
        ref_instruction=ref_instruction,
    )
    messages = [{"role": "system", "content": CONTENT_GENERATOR_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=120,
        )
        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