2.0 KiB
2.0 KiB
H2: LLM 驱动的 Wiki 策展
修改文件
~/.hermes/scripts/wiki_curator.py
在现有启发式提取基础上,新增 --llm 模式调用 NewAPI。
1. 配置
# LLM 配置
LLM_API = "http://127.0.0.1:3000/v1/chat/completions"
LLM_MODEL = "minimaxai/minimax-m3"
LLM_KEY = "sk-0Ex...MWBP" # 从 ~/.hermes/config.yaml 读取
从 ~/.hermes/config.yaml 读取 key(避免硬编码):
import yaml
with open(os.path.expanduser("~/.hermes/config.yaml")) as f:
cfg = yaml.safe_load(f)
llm_key = cfg.get("providers", {}).get("newapi-local", {}).get("api_key", "")
2. 新增参数
parser.add_argument("--llm", action="store_true", help="Use LLM for extraction (default: heuristic)")
3. LLM 提取函数
def extract_with_llm(content: str, filepath: str) -> dict:
"""调用 NewAPI LLM 提取结构化知识"""
prompt = f"""Analyze the following technical document and extract knowledge.
Return JSON only with this exact structure:
{{
"concepts": [{{"name": "...", "summary": "...", "details": "..."}}],
"entities": [{{"name": "...", "attributes": {{...}}}}],
"relations": [{{"source": "...", "relation": "uses|contains|depends_on|implements|part_of", "target": "..."}}]
}}
Document: {content[:3000]}
"""
resp = requests.post(LLM_API,
headers={"Authorization": f"Bearer {LLM_KEY}", "Content-Type": "application/json"},
json={"model": LLM_MODEL, "messages": [{"role": "user", "content": prompt}], "temperature": 0.1},
timeout=30)
# 解析 JSON 响应
...
4. 提取逻辑
- 用
--llm→ 优先 LLM 提取,LLM 失败/超时 → 回退到启发式 - 不用
--llm→ 当前启发式行为
验证
# LLM 模式
python3 ~/.hermes/scripts/wiki_curator.py --dir /tmp/test-wiki --llm --force
# LLM 模式 dry-run
python3 ~/.hermes/scripts/wiki_curator.py --dir /tmp/test-wiki --llm --dry-run
# 检查提取质量(LLM 应产出比启发式更精准的概念)