-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_wiki.py
More file actions
139 lines (101 loc) · 3.77 KB
/
build_wiki.py
File metadata and controls
139 lines (101 loc) · 3.77 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""
build_wiki.py - 从 PDF 生成机器学习概念知识库
功能:读取 PDF 内容,让模型生成若干个 markdown 概念页,保存到 wiki/
"""
import os
from http import HTTPStatus
import dashscope
from dotenv import load_dotenv
load_dotenv()
DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
if not DASHSCOPE_API_KEY:
raise ValueError("请设置环境变量 DASHSCOPE_API_KEY")
def load_pdf_documents(raw_dir: str = "raw"):
from langchain_community.document_loaders import PyPDFLoader
docs = []
if not os.path.exists(raw_dir):
print(f"警告:目录 {raw_dir} 不存在")
return docs
for file in os.listdir(raw_dir):
if file.endswith(".pdf"):
loader = PyPDFLoader(os.path.join(raw_dir, file))
docs.extend(loader.load())
print(f"已加载 PDF: {file}")
return docs
def load_pdf_text(pdf_path: str) -> str:
from langchain_community.document_loaders import PyPDFLoader
loader = PyPDFLoader(pdf_path)
documents = loader.load()
return "\n\n".join(doc.page_content for doc in documents)
def generate_wiki_page(full_text: str, topic: str) -> str:
prompt = f"""你是一个机器学习课程知识整理助手。
请根据以下课程资料,围绕主题"{topic}"生成一份 markdown 格式的知识页。
要求:
1. 只使用资料中的内容,必要时做简洁整理
2. 输出必须是 markdown
3. 必须按以下结构输出:
- 标题
- 核心定义
- 训练/预测流程
- 关键要点
- 与其他概念的关系
- 资料中未充分覆盖的部分(如果有,必须放在最后)
4. "资料中未充分覆盖的部分"不能放在前面
5. 全部用中文输出,专业术语可保留英文
课程资料:
{full_text}
请直接输出 markdown:
"""
response = dashscope.Generation.call(
model="qwen-plus",
prompt=prompt,
api_key=DASHSCOPE_API_KEY,
temperature=0,
)
if response.status_code == HTTPStatus.OK:
return response.output.text
raise RuntimeError(f"生成失败:{response.code} - {response.message}")
def save_wiki_page(topic: str, content: str, output_dir: str = "wiki"):
os.makedirs(output_dir, exist_ok=True)
filename = topic.lower().replace(" ", "_") + ".md"
path = os.path.join(output_dir, filename)
with open(path, "w", encoding="utf-8") as file:
file.write(content)
print(f"已保存:{path}")
def main(topics_override=None):
import sys
print("正在读取 raw/ 目录下的所有 PDF...")
docs = load_pdf_documents("raw")
if not docs:
print("Error: 没有找到任何 PDF 文件")
return
full_text = "\n\n".join(doc.page_content for doc in docs)
print(f"PDF 读取完成,总长度:{len(full_text)} 字符")
key_topics = [
"regression",
"classification vs regression",
"supervised learning",
]
if topics_override:
topics = topics_override
print(f"\n>>> 使用指定的 topic 列表(共 {len(topics)} 个)")
elif len(sys.argv) > 1:
topics = sys.argv[1:]
print(f"\n>>> 使用命令行指定的 topic 列表(共 {len(topics)} 个)")
else:
topics = key_topics
print(f"\n>>> 使用关键 topic 列表(共 {len(topics)} 个)- 省 token 模式")
print(f"开始生成 {len(topics)} 个概念页面...")
print("=" * 50)
for topic in topics:
print(f"\n正在生成:{topic}")
try:
content = generate_wiki_page(full_text, topic)
save_wiki_page(topic, content)
except Exception as exc:
print(f"Error: 生成失败 [{topic}]: {exc}")
print("\n" + "=" * 50)
print("生成完成!")
print("输出目录:wiki/")
if __name__ == "__main__":
main()