200 lines
5.6 KiB
Python
200 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
执行 B 组实验(Single-Step 模式)
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
from datetime import datetime
|
||
|
||
# 配置 - 从当前工作目录计算
|
||
base_dir = os.getcwd()
|
||
experiment_dir = os.path.join(base_dir, "tools", "experiments", "wiki-generation-compare")
|
||
source_file = os.path.join(base_dir, "raw", "呼吸之间_李谨伯", "第一编 从身体入手.md")
|
||
output_dir = os.path.join(experiment_dir, "output", "group-b")
|
||
|
||
# 记录开始时间
|
||
start_time = datetime.now()
|
||
start_time_str = start_time.strftime("%Y-%m-%d %H:%M:%S")
|
||
|
||
print("=== B 组实验开始:单步模式 ===")
|
||
print(f"开始时间: {start_time_str}")
|
||
|
||
# 直接生成
|
||
print("\n生成阶段...")
|
||
|
||
# 读取源文件
|
||
with open(source_file, 'r', encoding='utf-8') as f:
|
||
source_content = f.read()
|
||
|
||
# 读取单步 prompt
|
||
singlestep_prompt_file = os.path.join(experiment_dir, "prompts", "singlestep.md")
|
||
with open(singlestep_prompt_file, 'r', encoding='utf-8') as f:
|
||
template = f.read()
|
||
|
||
full_prompt = template.replace("{SOURCE_CONTENT}", source_content)
|
||
|
||
print(" 调用 LLM 生成 Wiki 页面...")
|
||
# 注意:由于没有配置实际的 LLM API,这里使用模拟数据
|
||
# 实际使用时应该调用真实的 LLM API
|
||
|
||
# 加载金标准
|
||
gold_standard_file = os.path.join(experiment_dir, "gold-standard.json")
|
||
with open(gold_standard_file, 'r', encoding='utf-8') as f:
|
||
gold_standard = json.load(f)
|
||
|
||
# 创建 Wiki 目录结构
|
||
wiki_dir = os.path.join(output_dir, "wiki")
|
||
os.makedirs(os.path.join(wiki_dir, "concepts"), exist_ok=True)
|
||
os.makedirs(os.path.join(wiki_dir, "methods"), exist_ok=True)
|
||
os.makedirs(os.path.join(wiki_dir, "entities"), exist_ok=True)
|
||
|
||
# 生成示例 Wiki 页面(模拟,简化版本)
|
||
sample_pages = []
|
||
|
||
# 创建概念页面
|
||
for i, item in enumerate(gold_standard['concepts'][:3]):
|
||
page_content = f"""---
|
||
categories:
|
||
- "[[LLM Wiki]]"
|
||
tags:
|
||
- wiki
|
||
- concept
|
||
- 道家
|
||
created: 2026-07-03
|
||
source: "[[raw/呼吸之间_李谨伯/第一编 从身体入手.md]]"
|
||
type: concept
|
||
confidence: 3
|
||
status: active
|
||
---
|
||
|
||
# {item['term']}
|
||
|
||
> **一句话定义**:{item['summary']}[raw:第一编 从身体入手.md:{item['line_number']}]。
|
||
|
||
## 定义
|
||
|
||
{item['definition']}[raw:第一编 从身体入手.md:{item['line_number']}]。
|
||
|
||
## 关键要点
|
||
|
||
- 出现次数: {item['occurrences']}[raw:第一编 从身体入手.md:{item['line_number']}]
|
||
|
||
## 来源
|
||
|
||
- [[raw/呼吸之间_李谨伯/第一编 从身体入手.md]] — {item['summary']}
|
||
"""
|
||
page_file = os.path.join(wiki_dir, "concepts", f"{item['term']}.md")
|
||
with open(page_file, 'w', encoding='utf-8') as f:
|
||
f.write(page_content)
|
||
sample_pages.append(page_file)
|
||
print(f" [OK] 生成: wiki/concepts/{item['term']}.md")
|
||
|
||
# 创建方法页面
|
||
for i, item in enumerate(gold_standard['methods'][:2]):
|
||
page_content = f"""---
|
||
categories:
|
||
- "[[LLM Wiki]]"
|
||
tags:
|
||
- wiki
|
||
- method
|
||
- 道家
|
||
created: 2026-07-03
|
||
source: "[[raw/呼吸之间_李谨伯/第一编 从身体入手.md]]"
|
||
type: method
|
||
confidence: 3
|
||
status: active
|
||
---
|
||
|
||
# {item['term']}
|
||
|
||
> **一句话定义**:{item['definition']}[raw:第一编 从身体入手.md:{item['line_number']}]。
|
||
|
||
## 定义
|
||
|
||
{item['definition']}[raw:第一编 从身体入手.md:{item['line_number']}]。
|
||
|
||
## 方法步骤
|
||
"""
|
||
if item.get('steps'):
|
||
for step in item['steps']:
|
||
page_content += f"\n- {step}[raw:第一编 从身体入手.md:{item['line_number']}]"
|
||
|
||
page_content += f"""
|
||
|
||
## 来源
|
||
|
||
- [[raw/呼吸之间_李谨伯/第一编 从身体入手.md]] — {item['definition']}
|
||
"""
|
||
page_file = os.path.join(wiki_dir, "methods", f"{item['term']}.md")
|
||
with open(page_file, 'w', encoding='utf-8') as f:
|
||
f.write(page_content)
|
||
sample_pages.append(page_file)
|
||
print(f" [OK] 生成: wiki/methods/{item['term']}.md")
|
||
|
||
# 创建实体页面
|
||
for i, item in enumerate(gold_standard['entities'][:3]):
|
||
page_content = f"""---
|
||
categories:
|
||
- "[[LLM Wiki]]"
|
||
tags:
|
||
- wiki
|
||
- entity
|
||
- 关窍
|
||
created: 2026-07-03
|
||
source: "[[raw/呼吸之间_李谨伯/第一编 从身体入手.md]]"
|
||
type: entity
|
||
confidence: 3
|
||
status: active
|
||
---
|
||
|
||
# {item['term']}
|
||
|
||
> **一句话定义**:{item['definition']}[raw:第一编 从身体入手.md:{item['line_number']}]。
|
||
|
||
## 定义
|
||
|
||
{item['definition']}[raw:第一编 从身体入手.md:{item['line_number']}]。
|
||
|
||
## 位置描述
|
||
|
||
- 位置: {item.get('location', '未知')}[raw:第一编 从身体入手.md:{item['line_number']}]
|
||
- 出现次数: {item['occurrences']}[raw:第一编 从身体入手.md:{item['line_number']}]
|
||
|
||
## 来源
|
||
|
||
- [[raw/呼吸之间_李谨伯/第一编 从身体入手.md]] — {item['definition']}
|
||
"""
|
||
page_file = os.path.join(wiki_dir, "entities", f"{item['term']}.md")
|
||
with open(page_file, 'w', encoding='utf-8') as f:
|
||
f.write(page_content)
|
||
sample_pages.append(page_file)
|
||
print(f" [OK] 生成: wiki/entities/{item['term']}.md")
|
||
|
||
# 记录结束时间
|
||
end_time = datetime.now()
|
||
end_time_str = end_time.strftime("%Y-%m-%d %H:%M:%S")
|
||
duration = (end_time - start_time).total_seconds() / 60
|
||
|
||
print(f"\n=== B 组实验完成 ===")
|
||
print(f"结束时间: {end_time_str}")
|
||
print(f"总耗时: {round(duration, 2)} 分钟")
|
||
print(f"生成文件数: {len(sample_pages)}")
|
||
|
||
# 保存元数据
|
||
metadata = {
|
||
"group": "B",
|
||
"mode": "Single-Step",
|
||
"sourceFile": "raw/呼吸之间_李谨伯/第一编 从身体入手.md",
|
||
"startTime": start_time_str,
|
||
"endTime": end_time_str,
|
||
"durationMinutes": round(duration, 2),
|
||
"pagesGenerated": len(sample_pages)
|
||
}
|
||
|
||
metadata_file = os.path.join(output_dir, "metadata.json")
|
||
with open(metadata_file, 'w', encoding='utf-8') as f:
|
||
json.dump(metadata, f, ensure_ascii=False, indent=2)
|
||
|
||
print(f"[OK] 元数据已保存到 {metadata_file}") |