Spaces:
Runtime error
Runtime error
File size: 929 Bytes
0b89aa2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import json
from pathlib import Path
class KnowledgeBase:
def __init__(self, storage_path="kb.json"):
self.storage_path = Path(storage_path)
if not self.storage_path.exists():
self._save({})
self.data = self._load()
def _load(self):
with self.storage_path.open("r") as f:
return json.load(f)
def _save(self, data):
with self.storage_path.open("w") as f:
json.dump(data, f, indent=2)
def get(self, topic):
return self.data.get(topic.lower(), "❓ No entry found.")
def set(self, topic, content):
self.data[topic.lower()] = content
self._save(self.data)
return f"✅ Knowledge added for topic: '{topic}'"
def search(self, query):
return {k: v for k, v in self.data.items() if query.lower() in k or query.lower() in v}
def list_topics(self):
return list(self.data.keys())
|