Files
memoryweave/skills/zhiyi/scripts/oc_migrate.py
T
xiaowei 2a350cf781 fix(distill): max_tokens 800→1200 — Agnes推理token预算不足
Agnes 2.0-flash是推理模型,推理token占大量预算。
800仍不够→JSON截断(parse error: unexpected end of JSON input)。
1200确保推理+正文都够用。
2026-08-20 01:13:06 +08:00

74 lines
2.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""迁移 openclaw LanceDB 全部数据到织忆 API(保留原始 namespace 隔离)"""
import lancedb, json, sys, time, requests
DB_PATH = "/var/lib/openclaw-memory/lancedb"
HEADERS = {"X-API-Key": "zhiyi-dev-key-2026", "Content-Type": "application/json"}
if "--dry-run" in sys.argv:
db = lancedb.connect(DB_PATH)
arrow = db.open_table("memories").to_arrow()
from collections import Counter
ns = Counter(arrow.column("namespace").to_pylist())
print(f"总条数: {arrow.num_rows}")
for n, c in sorted(ns.items(), key=lambda x: -x[1]):
print(f" {n}: {c}")
sys.exit(0)
db = lancedb.connect(DB_PATH)
arrow = db.open_table("memories").to_arrow()
total = arrow.num_rows
contents = arrow.column("content").to_pylist()
namespaces = arrow.column("namespace").to_pylist()
agent_ids = arrow.column("agent_id").to_pylist()
categories = arrow.column("category").to_pylist() if "category" in arrow.schema.names else [None] * total
print(f"读取 {total} 条, 开始迁移...", flush=True)
ok, fail, empty = 0, 0, 0
t0 = time.time()
for i in range(total):
content = str(contents[i]).strip() if contents[i] else ""
if not content or len(content) < 5:
empty += 1
continue
payload = {
"content": content,
"namespace": str(namespaces[i]) if namespaces[i] else "openclaw-main",
"agent_id": str(agent_ids[i]) if agent_ids[i] else "openclaw",
"category": str(categories[i]) if categories[i] else "general",
}
for attempt in range(5):
try:
resp = requests.post("http://localhost:7821/api/v1/commit", headers=HEADERS, json=payload, timeout=10)
if resp.status_code in (200, 201):
ok += 1
break
elif resp.status_code == 429:
time.sleep(resp.json().get("retry_after", 2))
continue
else:
fail += 1
if fail <= 3:
print(f" ✗ [{i}] HTTP {resp.status_code}: {resp.text[:60]}", flush=True)
break
except Exception as e:
fail += 1
if fail <= 3:
print(f" ✗ [{i}] {e}", flush=True)
break
if (i + 1) % 100 == 0:
elapsed = time.time() - t0
rate = (i + 1) / elapsed
eta = (total - i - 1) / rate / 60
print(f" [{i+1}/{total}] ✓{ok} ✗{fail} ({rate:.0f}/s, ETA {eta:.0f}m)", flush=True)
time.sleep(0.12)
elapsed = time.time() - t0
print(f"\n=== 完成 {elapsed:.0f}s ===", flush=True)
print(f" 提交: {ok}", flush=True)
print(f" 失败: {fail}", flush=True)
print(f" 跳过(空/短): {empty}", flush=True)
print(f" 总计: {ok+fail+empty}/{total}", flush=True)