90 lines
2.9 KiB
Python
90 lines
2.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
向量记忆系统 - 命令行工具
|
|
用法:
|
|
python3 memory_cli.py add "记忆内容" --importance 4 --tags tag1,tag2
|
|
python3 memory_cli.py search "查询内容"
|
|
python3 memory_cli.py recent
|
|
python3 memory_cli.py count
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from vector_memory import VectorMemorySystem
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="OpenClaw 向量记忆系统 CLI")
|
|
subparsers = parser.add_subparsers(dest="command", help="命令")
|
|
|
|
# 添加记忆
|
|
add_parser = subparsers.add_parser("add", help="添加记忆")
|
|
add_parser.add_argument("content", help="记忆内容")
|
|
add_parser.add_argument("--importance", "-i", type=int, default=3,
|
|
help="重要性 1-5 (默认: 3)")
|
|
add_parser.add_argument("--tags", "-t", type=str, default="",
|
|
help="标签,用逗号分隔")
|
|
|
|
# 搜索
|
|
search_parser = subparsers.add_parser("search", help="语义搜索")
|
|
search_parser.add_argument("query", help="查询内容")
|
|
search_parser.add_argument("--top", "-n", type=int, default=5,
|
|
help="返回数量 (默认: 5)")
|
|
|
|
# 最近
|
|
subparsers.add_parser("recent", help="查看最近记忆")
|
|
subparsers.add_parser("count", help="统计记忆数量")
|
|
|
|
# 删除
|
|
delete_parser = subparsers.add_parser("delete", help="删除记忆")
|
|
delete_parser.add_argument("memory_id", help="记忆 ID")
|
|
|
|
args = parser.parse_args()
|
|
|
|
# 获取 API Key
|
|
api_key = os.getenv("SILICONFLOW_API_KEY")
|
|
if not api_key:
|
|
print("❌ 请设置 SILICONFLOW_API_KEY 环境变量")
|
|
sys.exit(1)
|
|
|
|
# 初始化
|
|
vm = VectorMemorySystem(api_key=api_key)
|
|
|
|
# 执行命令
|
|
if args.command == "add":
|
|
tags = args.tags.split(",") if args.tags else []
|
|
metadata = {"tags": tags} if tags else {}
|
|
vm.add_memory(args.content, metadata, args.importance)
|
|
|
|
elif args.command == "search":
|
|
results = vm.search(args.query, args.top)
|
|
print(f"\n🔍 搜索: '{args.query}' (共 {len(results)} 条)")
|
|
for i, r in enumerate(results, 1):
|
|
print(f"\n{i}. {r['content']}")
|
|
print(f" 相似度: {r['similarity']:.2%} | ID: {r['id']}")
|
|
|
|
elif args.command == "recent":
|
|
recent = vm.get_recent(10)
|
|
print(f"\n📅 最近 {len(recent)} 条记忆:")
|
|
for i, r in enumerate(recent, 1):
|
|
print(f"{i}. {r['content'][:50]}...")
|
|
print(f" 创建于: {r['created_at']} | 重要性: {r['importance']}")
|
|
|
|
elif args.command == "count":
|
|
print(f"\n📊 记忆总数: {vm.count()}")
|
|
|
|
elif args.command == "delete":
|
|
vm.delete(args.memory_id)
|
|
|
|
else:
|
|
parser.print_help()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|