Files

130 lines
4.5 KiB
PowerShell

# B 组:单步模式
$ErrorActionPreference = "Stop"
# 配置
$sourceFile = "raw/呼吸之间_李谨伯/第一编 从身体入手.md"
$outputDir = "tools/experiments/wiki-generation-compare/output/group-b"
$singleStepPrompt = "tools/experiments/wiki-generation-compare/prompts/singlestep.md"
# 记录开始时间
$startTime = Get-Date
$startTimeStr = $startTime.ToString("yyyy-MM-dd HH:mm:ss")
Write-Host "=== B 组实验开始:单步模式 ===" -ForegroundColor Cyan
Write-Host "开始时间: $startTimeStr" -ForegroundColor Yellow
# 直接生成
Write-Host "`n生成阶段..." -ForegroundColor Green
$sourceContent = Get-Content $sourceFile -Raw -Encoding UTF8
$template = Get-Content $singleStepPrompt -Raw -Encoding UTF8
$fullPrompt = $template -replace "{SOURCE_CONTENT}", $sourceContent
Write-Host " 调用 LLM 生成 Wiki 页面..." -ForegroundColor Gray
# 使用 opencode-mem 调用 LLM
$generationResult = opencode-mem query "$fullPrompt" --format text
# 如果 opencode-mem 不可用,使用备用方法
if (-not $generationResult) {
Write-Host " opencode-mem 不可用,使用备用方法..." -ForegroundColor Yellow
# 直接调用 LLM API
$apiKey = $env:OPENAI_API_KEY
if ($apiKey) {
$body = @{
model = "gpt-4"
messages = @(
@{
role = "system"
content = "你是一位知识库构建专家。"
},
@{
role = "user"
content = $fullPrompt
}
)
temperature = 0.5
max_tokens = 8000
} | ConvertTo-Json -Depth 10
try {
$response = Invoke-RestMethod -Uri "https://api.openai.com/v1/chat/completions" `
-Method Post `
-Headers @{
"Authorization" = "Bearer $apiKey"
"Content-Type" = "application/json"
} `
-Body $body
$generationResult = $response.choices[0].message.content
} catch {
Write-Host " ✗ OpenAI API 调用失败: $_" -ForegroundColor Red
Write-Host " 生成模拟数据用于测试..." -ForegroundColor Yellow
$generationResult = $null
}
}
}
# 解析生成结果
$wikiDir = "$outputDir/wiki"
if (!(Test-Path $wikiDir)) {
New-Item -ItemType Directory -Path $wikiDir | Out-Null
}
if ($generationResult) {
# 解析 ---FILE: ... ---END FILE--- 块
$fileBlocks = [regex]::Matches($generationResult, "---FILE: (.*?)---(.*?)---END FILE---", [regexoptions]::Singleline)
if ($fileBlocks.Count -eq 0) {
Write-Host " ⚠ 未找到文件块,尝试其他解析方式..." -ForegroundColor Yellow
# 尝试宽松匹配
$fileBlocks = [regex]::Matches($generationResult, "FILE: (.*?)\n(.*?)(?=(FILE:|$))", [regexoptions]::Singleline)
}
foreach ($block in $fileBlocks) {
$filePath = $block.Groups[1].Value.Trim()
$fileContent = $block.Groups[2].Value.Trim()
# 创建目录(如果需要)
$fullPath = "$outputDir/$filePath"
$fileDir = Split-Path $fullPath -Parent
if (!(Test-Path $fileDir)) {
New-Item -ItemType Directory -Path $fileDir -Force | Out-Null
}
# 写入文件
$fileContent | Out-File $fullPath -Encoding UTF8
Write-Host " ✓ 生成: $filePath" -ForegroundColor Gray
}
} else {
Write-Host " ⚠ LLM 调用失败,跳过生成步骤" -ForegroundColor Yellow
Write-Host " 这是正常的,因为需要配置 LLM API" -ForegroundColor Yellow
}
# 记录结束时间
$endTime = Get-Date
$endTimeStr = $endTime.ToString("yyyy-MM-dd HH:mm:ss")
$duration = ($endTime - $startTime).TotalMinutes
# 统计生成的文件
$generatedFiles = @(Get-ChildItem -Path $wikiDir -Recurse -Filter "*.md")
Write-Host "`n=== B 组实验完成 ===" -ForegroundColor Cyan
Write-Host "结束时间: $endTimeStr" -ForegroundColor Yellow
Write-Host "总耗时: $([math]::Round($duration, 2)) 分钟" -ForegroundColor Yellow
Write-Host "生成文件数: $($generatedFiles.Count)" -ForegroundColor Yellow
# 保存元数据
$metadata = @{
group = "B"
mode = "Single-Step"
sourceFile = $sourceFile
startTime = $startTimeStr
endTime = $endTimeStr
durationMinutes = [math]::Round($duration, 2)
pagesGenerated = $generatedFiles.Count
} | ConvertTo-Json -Depth 10
$metadata | Out-File "$outputDir/metadata.json" -Encoding UTF8
Write-Host "✓ 元数据已保存到 $outputDir/metadata.json" -ForegroundColor Green