chore(vault): backup 2026-07-03 21:40:41
This commit is contained in:
@@ -37,20 +37,64 @@ with open(analysis_prompt_file, 'r', encoding='utf-8') as f:
|
||||
analysis_full_prompt = analysis_template.replace("{SOURCE_CONTENT}", source_content)
|
||||
|
||||
print(" 调用 LLM 进行分析...")
|
||||
# 注意:由于没有配置实际的 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)
|
||||
# 真实调用 LLM API
|
||||
import os
|
||||
import requests
|
||||
|
||||
# 保存分析结果
|
||||
analysis_output = os.path.join(output_dir, "analysis.json")
|
||||
with open(analysis_output, 'w', encoding='utf-8') as f:
|
||||
json.dump(gold_standard, f, ensure_ascii=False, indent=2)
|
||||
api_key = os.environ.get('OPENAI_API_KEY')
|
||||
if not api_key:
|
||||
print(" [ERROR] 未找到 OPENAI_API_KEY 环境变量")
|
||||
exit(1)
|
||||
|
||||
print(f" [OK] Analysis 完成,识别到 {len(gold_standard['recommended_pages'])} 个推荐页面")
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
body = {
|
||||
"model": "gpt-4o-mini", # 使用更便宜的模型
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "你是一位知识库分析专家。请分析源文件并提取结构化信息。"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": analysis_full_prompt
|
||||
}
|
||||
],
|
||||
"temperature": 0.3
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post("https://api.openai.com/v1/chat/completions",
|
||||
headers=headers,
|
||||
json=body,
|
||||
timeout=120)
|
||||
response.raise_for_status()
|
||||
analysis_result = response.json()['choices'][0]['message']['content']
|
||||
|
||||
# 保存分析结果
|
||||
with open(analysis_output, 'w', encoding='utf-8') as f:
|
||||
f.write(analysis_result)
|
||||
|
||||
print(f" [OK] Analysis 完成,已保存到 {analysis_output}")
|
||||
|
||||
# 解析 JSON
|
||||
try:
|
||||
gold_standard = json.loads(analysis_result)
|
||||
recommended_count = len(gold_standard.get('recommended_pages', []))
|
||||
except:
|
||||
recommended_count = len(gold_standard.get('recommended_pages', []))
|
||||
|
||||
except Exception as e:
|
||||
print(f" [ERROR] LLM API 调用失败: {e}")
|
||||
print(" 使用模拟数据...")
|
||||
# 使用备用模拟数据
|
||||
with open(gold_standard_file, 'r', encoding='utf-8') as f:
|
||||
gold_standard = json.load(f)
|
||||
recommended_count = len(gold_standard.get('recommended_pages', []))
|
||||
|
||||
# Step 2: Generation
|
||||
print("\nStep 2: Generation 阶段...")
|
||||
|
||||
Reference in New Issue
Block a user