Agnes 2.0-flash是推理模型,推理token占大量预算。 800仍不够→JSON截断(parse error: unexpected end of JSON input)。 1200确保推理+正文都够用。
67 lines
2.0 KiB
Markdown
67 lines
2.0 KiB
Markdown
# H2: LLM 驱动的 Wiki 策展
|
|
|
|
## 修改文件
|
|
|
|
### `~/.hermes/scripts/wiki_curator.py`
|
|
|
|
在现有启发式提取基础上,新增 `--llm` 模式调用 NewAPI。
|
|
|
|
#### 1. 配置
|
|
|
|
```python
|
|
# 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(避免硬编码):
|
|
```python
|
|
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. 新增参数
|
|
```python
|
|
parser.add_argument("--llm", action="store_true", help="Use LLM for extraction (default: heuristic)")
|
|
```
|
|
|
|
#### 3. LLM 提取函数
|
|
```python
|
|
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` → 当前启发式行为
|
|
|
|
## 验证
|
|
```bash
|
|
# 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 应产出比启发式更精准的概念)
|
|
```
|