Upload 2 files
Browse files- core/knowledge_engine.py +38 -26
core/knowledge_engine.py
CHANGED
|
@@ -39,40 +39,51 @@ class KnowledgeEngine:
|
|
| 39 |
def __init__(self, persist_dir: str = "./chroma_db"):
|
| 40 |
self.persist_dir = Path(persist_dir)
|
| 41 |
self.persist_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if not LLAMAINDEX_AVAILABLE:
|
| 44 |
-
self.index = None
|
| 45 |
return
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
)
|
| 52 |
-
Settings.llm = Anthropic(
|
| 53 |
-
api_key=os.getenv("ANTHROPIC_API_KEY"),
|
| 54 |
-
model="claude-sonnet-4-20250514"
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
# Initialize ChromaDB
|
| 58 |
-
self.chroma_client = chromadb.PersistentClient(path=str(self.persist_dir))
|
| 59 |
-
self.chroma_collection = self.chroma_client.get_or_create_collection("omnimind_knowledge")
|
| 60 |
-
|
| 61 |
-
# Vector store
|
| 62 |
-
self.vector_store = ChromaVectorStore(chroma_collection=self.chroma_collection)
|
| 63 |
-
self.storage_context = StorageContext.from_defaults(vector_store=self.vector_store)
|
| 64 |
|
| 65 |
-
# Try to load existing index
|
| 66 |
try:
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
async def add_documents(self, documents_path: str) -> Dict[str, Any]:
|
| 74 |
"""Add documents to the knowledge base"""
|
| 75 |
-
|
|
|
|
| 76 |
return {"status": "unavailable", "message": "LlamaIndex not installed"}
|
| 77 |
|
| 78 |
reader = SimpleDirectoryReader(documents_path)
|
|
@@ -97,7 +108,8 @@ class KnowledgeEngine:
|
|
| 97 |
|
| 98 |
async def query(self, question: str, top_k: int = 3) -> Dict[str, Any]:
|
| 99 |
"""Query the knowledge base"""
|
| 100 |
-
|
|
|
|
| 101 |
return {
|
| 102 |
"status": "unavailable",
|
| 103 |
"answer": "Knowledge base not configured",
|
|
|
|
| 39 |
def __init__(self, persist_dir: str = "./chroma_db"):
|
| 40 |
self.persist_dir = Path(persist_dir)
|
| 41 |
self.persist_dir.mkdir(parents=True, exist_ok=True)
|
| 42 |
+
self.index = None
|
| 43 |
+
self._initialized = False
|
| 44 |
|
| 45 |
if not LLAMAINDEX_AVAILABLE:
|
|
|
|
| 46 |
return
|
| 47 |
|
| 48 |
+
def _ensure_initialized(self):
|
| 49 |
+
"""Lazy initialization to avoid startup errors"""
|
| 50 |
+
if self._initialized or not LLAMAINDEX_AVAILABLE:
|
| 51 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
|
|
|
| 53 |
try:
|
| 54 |
+
# Configure LlamaIndex settings
|
| 55 |
+
Settings.embed_model = OpenAIEmbedding(
|
| 56 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
| 57 |
+
model="text-embedding-3-small"
|
| 58 |
+
)
|
| 59 |
+
# Skip Anthropic LLM initialization to avoid version conflict
|
| 60 |
+
# Settings.llm = Anthropic(...)
|
| 61 |
+
|
| 62 |
+
# Initialize ChromaDB
|
| 63 |
+
self.chroma_client = chromadb.PersistentClient(path=str(self.persist_dir))
|
| 64 |
+
self.chroma_collection = self.chroma_client.get_or_create_collection("omnimind_knowledge")
|
| 65 |
+
|
| 66 |
+
# Vector store
|
| 67 |
+
self.vector_store = ChromaVectorStore(chroma_collection=self.chroma_collection)
|
| 68 |
+
self.storage_context = StorageContext.from_defaults(vector_store=self.vector_store)
|
| 69 |
+
|
| 70 |
+
# Try to load existing index
|
| 71 |
+
try:
|
| 72 |
+
self.index = load_index_from_storage(self.storage_context)
|
| 73 |
+
print("[OK] Loaded existing knowledge base")
|
| 74 |
+
except:
|
| 75 |
+
self.index = None
|
| 76 |
+
print("[INFO] No existing knowledge base - will create on first document add")
|
| 77 |
+
|
| 78 |
+
self._initialized = True
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"[WARNING] LlamaIndex initialization failed: {e}")
|
| 81 |
+
self._initialized = False
|
| 82 |
|
| 83 |
async def add_documents(self, documents_path: str) -> Dict[str, Any]:
|
| 84 |
"""Add documents to the knowledge base"""
|
| 85 |
+
self._ensure_initialized()
|
| 86 |
+
if not LLAMAINDEX_AVAILABLE or not self._initialized:
|
| 87 |
return {"status": "unavailable", "message": "LlamaIndex not installed"}
|
| 88 |
|
| 89 |
reader = SimpleDirectoryReader(documents_path)
|
|
|
|
| 108 |
|
| 109 |
async def query(self, question: str, top_k: int = 3) -> Dict[str, Any]:
|
| 110 |
"""Query the knowledge base"""
|
| 111 |
+
self._ensure_initialized()
|
| 112 |
+
if not LLAMAINDEX_AVAILABLE or not self._initialized or self.index is None:
|
| 113 |
return {
|
| 114 |
"status": "unavailable",
|
| 115 |
"answer": "Knowledge base not configured",
|