Peter Mutwiri commited on
Commit Β·
35919f2
1
Parent(s): a5d7fab
refactored ai service
Browse files- app/service/ai_service.py +23 -5
app/service/ai_service.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
-
# app/service/ai_service.py
|
| 2 |
import json
|
| 3 |
import logging
|
| 4 |
from app.deps import get_vector_db
|
| 5 |
-
from
|
| 6 |
-
from app.service.embedding_service import embedder
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
class AIService:
|
|
@@ -19,10 +20,27 @@ class AIService:
|
|
| 19 |
self.vector_db = None
|
| 20 |
self.vss_available = False
|
| 21 |
|
| 22 |
-
self.
|
| 23 |
-
self.
|
| 24 |
logger.info(f"β
AI Service initialized (VSS: {'ENABLED' if self.vss_available else 'DISABLED'})")
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def detect_entity_type(self, org_id: str, columns: list[str], filename: str) -> dict:
|
| 27 |
"""Detect entity type with JSON validation and timeout"""
|
| 28 |
columns_str = ",".join(columns)
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import logging
|
| 3 |
from app.deps import get_vector_db
|
| 4 |
+
from typing import TYPE_CHECKING # β
For forward reference
|
|
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from app.service.llm_service import LocalLLMService # β
Type hint only
|
| 9 |
+
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
class AIService:
|
|
|
|
| 20 |
self.vector_db = None
|
| 21 |
self.vss_available = False
|
| 22 |
|
| 23 |
+
self._llm = None # β
Lazy initialization
|
| 24 |
+
self._embedder = None # β
FIXED: Use _embedder (not embedder)
|
| 25 |
logger.info(f"β
AI Service initialized (VSS: {'ENABLED' if self.vss_available else 'DISABLED'})")
|
| 26 |
|
| 27 |
+
@property
|
| 28 |
+
def llm(self) -> "LocalLLMService":
|
| 29 |
+
"""Lazy property to get LLM service (avoids circular import)"""
|
| 30 |
+
if self._llm is None:
|
| 31 |
+
from app.service.llm_service import get_llm_service # β
Import INSIDE property
|
| 32 |
+
self._llm = get_llm_service()
|
| 33 |
+
return self._llm
|
| 34 |
+
|
| 35 |
+
@property
|
| 36 |
+
def embedder(self):
|
| 37 |
+
"""Lazy property to get embedder"""
|
| 38 |
+
if self._embedder is None:
|
| 39 |
+
from app.service.embedding_service import embedder # β
Import INSIDE property
|
| 40 |
+
self._embedder = embedder
|
| 41 |
+
return self._embedder
|
| 42 |
+
|
| 43 |
+
|
| 44 |
def detect_entity_type(self, org_id: str, columns: list[str], filename: str) -> dict:
|
| 45 |
"""Detect entity type with JSON validation and timeout"""
|
| 46 |
columns_str = ",".join(columns)
|