114 lines
3.7 KiB
Python
Raw Normal View History

2025-11-04 05:57:23 +01:00
import os
import time
import uuid
2025-11-04 08:09:12 +01:00
from typing import Any, Dict
2025-11-04 05:57:23 +01:00
2025-11-04 08:09:12 +01:00
from pr.memory.knowledge_store import KnowledgeEntry, KnowledgeStore
def add_knowledge_entry(
category: str, content: str, metadata: Dict[str, Any] = None, entry_id: str = None
) -> Dict[str, Any]:
2025-11-04 05:57:23 +01:00
"""Add a new entry to the knowledge base."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
if entry_id is None:
entry_id = str(uuid.uuid4())[:16]
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
entry = KnowledgeEntry(
entry_id=entry_id,
category=category,
content=content,
metadata=metadata or {},
created_at=time.time(),
2025-11-04 08:09:12 +01:00
updated_at=time.time(),
2025-11-04 05:57:23 +01:00
)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
store.add_entry(entry)
return {"status": "success", "entry_id": entry_id}
except Exception as e:
return {"status": "error", "error": str(e)}
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
def get_knowledge_entry(entry_id: str) -> Dict[str, Any]:
"""Retrieve a knowledge entry by ID."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
entry = store.get_entry(entry_id)
if entry:
return {"status": "success", "entry": entry.to_dict()}
else:
return {"status": "not_found", "entry_id": entry_id}
except Exception as e:
return {"status": "error", "error": str(e)}
2025-11-04 08:09:12 +01:00
2025-11-04 08:10:37 +01:00
def search_knowledge(query: str, category: str = None, top_k: int = 5) -> Dict[str, Any]:
2025-11-04 05:57:23 +01:00
"""Search the knowledge base semantically."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
entries = store.search_entries(query, category, top_k)
results = [entry.to_dict() for entry in entries]
return {"status": "success", "results": results}
except Exception as e:
return {"status": "error", "error": str(e)}
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
def get_knowledge_by_category(category: str, limit: int = 20) -> Dict[str, Any]:
"""Get knowledge entries by category."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
entries = store.get_by_category(category, limit)
results = [entry.to_dict() for entry in entries]
return {"status": "success", "entries": results}
except Exception as e:
return {"status": "error", "error": str(e)}
2025-11-04 08:09:12 +01:00
2025-11-04 08:10:37 +01:00
def update_knowledge_importance(entry_id: str, importance_score: float) -> Dict[str, Any]:
2025-11-04 05:57:23 +01:00
"""Update the importance score of a knowledge entry."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
store.update_importance(entry_id, importance_score)
2025-11-04 08:09:12 +01:00
return {
"status": "success",
"entry_id": entry_id,
"importance_score": importance_score,
}
2025-11-04 05:57:23 +01:00
except Exception as e:
return {"status": "error", "error": str(e)}
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
def delete_knowledge_entry(entry_id: str) -> Dict[str, Any]:
"""Delete a knowledge entry."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
success = store.delete_entry(entry_id)
return {"status": "success" if success else "not_found", "entry_id": entry_id}
except Exception as e:
return {"status": "error", "error": str(e)}
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
def get_knowledge_statistics() -> Dict[str, Any]:
"""Get statistics about the knowledge base."""
try:
2025-11-04 08:09:12 +01:00
db_path = os.path.expanduser("~/.assistant_db.sqlite")
2025-11-04 05:57:23 +01:00
store = KnowledgeStore(db_path)
2025-11-04 08:09:12 +01:00
2025-11-04 05:57:23 +01:00
stats = store.get_statistics()
return {"status": "success", "statistics": stats}
except Exception as e:
return {"status": "error", "error": str(e)}