Peter Mutwiri commited on
Commit Β·
fbaed84
1
Parent(s): fdaa96f
ai service refactor
Browse files- app/service/ai_service.py +86 -86
app/service/ai_service.py
CHANGED
|
@@ -1,108 +1,108 @@
|
|
| 1 |
-
# app/service/ai_service.py
|
| 2 |
-
import json
|
| 3 |
-
import logging
|
| 4 |
-
from app.deps import get_vector_db
|
| 5 |
-
from app.service.llm_service import LocalLLMService
|
| 6 |
-
from app.service.embedding_service import embedder
|
| 7 |
-
import time
|
| 8 |
|
| 9 |
-
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
-
class AIService:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
You MUST respond with ONLY valid JSON in this exact format:
|
| 46 |
-
{{"entity_type":"sales|inventory|customer|product","confidence":0.95}}
|
| 47 |
|
| 48 |
-
Dataset info:
|
| 49 |
-
- Filename: {filename}
|
| 50 |
-
- Columns: {columns_str}
|
| 51 |
|
| 52 |
-
Analyze and respond with ONLY JSON:"""
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
-
ai_service = AIService()
|
|
|
|
| 1 |
+
# # app/service/ai_service.py
|
| 2 |
+
# import json
|
| 3 |
+
# import logging
|
| 4 |
+
# from app.deps import get_vector_db
|
| 5 |
+
# from app.service.llm_service import LocalLLMService
|
| 6 |
+
# from app.service.embedding_service import embedder
|
| 7 |
+
# import time
|
| 8 |
|
| 9 |
+
# logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
+
# class AIService:
|
| 12 |
+
# def __init__(self):
|
| 13 |
+
# try:
|
| 14 |
+
# self.vector_db = get_vector_db()
|
| 15 |
+
# self.vss_available = True
|
| 16 |
+
# logger.info("β
Vector DB initialized")
|
| 17 |
+
# except Exception as e:
|
| 18 |
+
# logger.warning(f"β οΈ Vector DB unavailable: {e}")
|
| 19 |
+
# self.vector_db = None
|
| 20 |
+
# self.vss_available = False
|
| 21 |
|
| 22 |
+
# self.llm = llm_service
|
| 23 |
+
# self.embedder = embedder
|
| 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)
|
| 29 |
|
| 30 |
+
# # Check cache
|
| 31 |
+
# if self.vss_available:
|
| 32 |
+
# cached = self.vector_db.execute("""
|
| 33 |
+
# SELECT entity_type FROM vector_store.embeddings
|
| 34 |
+
# WHERE org_id = ? AND content = ?
|
| 35 |
+
# ORDER BY created_at DESC LIMIT 1
|
| 36 |
+
# """, [org_id, columns_str]).fetchone()
|
| 37 |
|
| 38 |
+
# if cached:
|
| 39 |
+
# logger.info(f"[ai_service] Cache hit: {cached[0]}")
|
| 40 |
+
# return {"entity_type": cached[0], "confidence": 0.99, "cached": True}
|
| 41 |
|
| 42 |
+
# # β
SIMPLE, CLEAR prompt for Phi-3
|
| 43 |
+
# prompt = f"""You are a data classification assistant.
|
| 44 |
|
| 45 |
+
# You MUST respond with ONLY valid JSON in this exact format:
|
| 46 |
+
# {{"entity_type":"sales|inventory|customer|product","confidence":0.95}}
|
| 47 |
|
| 48 |
+
# Dataset info:
|
| 49 |
+
# - Filename: {filename}
|
| 50 |
+
# - Columns: {columns_str}
|
| 51 |
|
| 52 |
+
# Analyze and respond with ONLY JSON:"""
|
| 53 |
|
| 54 |
+
# logger.info(f"[ai_service] Calling LLM for {org_id}...")
|
| 55 |
|
| 56 |
+
# try:
|
| 57 |
+
# # β
TIMEOUT WRAPPER (5 seconds max)
|
| 58 |
+
# start_time = time.time()
|
| 59 |
+
# response_text = self.llm.generate(prompt, max_tokens=50, temperature=0.1)
|
| 60 |
+
# elapsed = time.time() - start_time
|
| 61 |
|
| 62 |
+
# logger.info(f"[ai_service] LLM responded in {elapsed:.2f}s: {response_text}")
|
| 63 |
|
| 64 |
+
# # β
AGGRESSIVE JSON cleaning
|
| 65 |
+
# response_text = response_text.strip()
|
| 66 |
+
# if "```json" in response_text:
|
| 67 |
+
# response_text = response_text.split("```json")[1].split("```")[0].strip()
|
| 68 |
+
# elif "```" in response_text:
|
| 69 |
+
# response_text = response_text.split("```")[1].split("```")[0].strip()
|
| 70 |
|
| 71 |
+
# # β
Remove any stray text before/after JSON
|
| 72 |
+
# if "{" in response_text and "}" in response_text:
|
| 73 |
+
# response_text = response_text[response_text.find("{"):response_text.rfind("}")+1]
|
| 74 |
|
| 75 |
+
# logger.info(f"[ai_service] Cleaned response: {response_text}")
|
| 76 |
|
| 77 |
+
# # β
PARSE with error handling
|
| 78 |
+
# result = json.loads(response_text)
|
| 79 |
|
| 80 |
+
# # β
Normalize
|
| 81 |
+
# result["entity_type"] = result["entity_type"].upper()
|
| 82 |
+
# result["confidence"] = float(result["confidence"])
|
| 83 |
|
| 84 |
+
# # β
Cache it
|
| 85 |
+
# if self.vss_available:
|
| 86 |
+
# try:
|
| 87 |
+
# embedding = self.embedder.generate(columns_str)
|
| 88 |
+
# self.vector_db.execute("""
|
| 89 |
+
# INSERT INTO vector_store.embeddings (org_id, content, embedding, entity_type)
|
| 90 |
+
# VALUES (?, ?, ?, ?)
|
| 91 |
+
# """, [org_id, columns_str, embedding, result["entity_type"]])
|
| 92 |
+
# logger.info(f"[ai_service] Cached for {org_id}")
|
| 93 |
+
# except Exception as e:
|
| 94 |
+
# logger.warning(f"[ai_service] Cache insert failed: {e}")
|
| 95 |
|
| 96 |
+
# return result
|
| 97 |
|
| 98 |
+
# except Exception as e:
|
| 99 |
+
# logger.error(f"[ai_service] β Detection failed: {e}", exc_info=True)
|
| 100 |
+
# # β
SAFE FALLBACK (never crash)
|
| 101 |
+
# return {
|
| 102 |
+
# "entity_type": "SALES",
|
| 103 |
+
# "confidence": 0.50,
|
| 104 |
+
# "error": str(e),
|
| 105 |
+
# "fallback": True
|
| 106 |
+
# }
|
| 107 |
|
| 108 |
+
# ai_service = AIService()
|