LiamKhoaLe commited on
Commit
130168a
·
1 Parent(s): 5d74993

rm MongoDB services by AgentDiagram #3

Browse files
api/README.md CHANGED
@@ -10,16 +10,15 @@
10
  - CORS configuration
11
  - Azure AI Foundry provider settings
12
 
13
- ### **database.py** - Database Management
14
- - MongoDB connection management
15
- - FAISS index lazy loading
16
  - SentenceTransformer model initialization
17
- - Symptom vectors management
18
- - GridFS integration
 
19
 
20
- ### **retrieval.py** - RAG Retrieval Engine
21
- - Medical information retrieval from FAISS
22
- - Symptom-based diagnosis retrieval
23
  - Smart deduplication and similarity matching
24
  - Vector similarity computations
25
  - Optional reranking support for guideline-heavy content
 
10
  - CORS configuration
11
  - Azure AI Foundry provider settings
12
 
13
+ ### **database.py** - Runtime Model Management
 
 
14
  - SentenceTransformer model initialization
15
+ - Embedding cache/runtime hooks
16
+ - Legacy MongoDB/FAISS startup disabled
17
+ - Compatibility stubs for removed retrieval storage
18
 
19
+ ### **retrieval.py** - Retrieval/Guidance Engine
20
+ - Medical information retrieval interface
21
+ - Symptom-based diagnosis retrieval interface
22
  - Smart deduplication and similarity matching
23
  - Vector similarity computations
24
  - Optional reranking support for guideline-heavy content
api/__pycache__/app.cpython-313.pyc CHANGED
Binary files a/api/__pycache__/app.cpython-313.pyc and b/api/__pycache__/app.cpython-313.pyc differ
 
api/__pycache__/database.cpython-313.pyc CHANGED
Binary files a/api/__pycache__/database.cpython-313.pyc and b/api/__pycache__/database.cpython-313.pyc differ
 
api/__pycache__/legacy.cpython-313.pyc CHANGED
Binary files a/api/__pycache__/legacy.cpython-313.pyc and b/api/__pycache__/legacy.cpython-313.pyc differ
 
api/app.py CHANGED
@@ -35,13 +35,14 @@ app.add_middleware(
35
  allow_headers=["*"],
36
  )
37
 
38
- # ✅ Initialize database connections
39
  try:
40
  db_manager.initialize_embedding_model()
41
- db_manager.initialize_mongodb()
42
- logger.info("✅ Database connections initialized successfully")
 
43
  except Exception as e:
44
- logger.error(f"❌ Database initialization failed: {e}")
45
  raise
46
 
47
  # ✅ Include routes
 
35
  allow_headers=["*"],
36
  )
37
 
38
+ # ✅ Initialize model/runtime dependencies only
39
  try:
40
  db_manager.initialize_embedding_model()
41
+ # Root Cause vs Logic: MongoDB-backed RAG initialization caused startup DNS failures,
42
+ # so the app now skips that legacy data plane and starts with model/runtime components only.
43
+ logger.info("✅ Runtime dependencies initialized successfully")
44
  except Exception as e:
45
+ logger.error(f"❌ Runtime initialization failed: {e}")
46
  raise
47
 
48
  # ✅ Include routes
api/database.py CHANGED
@@ -1,34 +1,20 @@
1
  # api/database.py
2
- import faiss
3
  import numpy as np
4
- import gridfs
5
- from pymongo import MongoClient
6
  from sentence_transformers import SentenceTransformer
7
- from .config import mongo_uri, index_uri, MODEL_CACHE_DIR, EMBEDDING_MODEL_DEVICE
8
  import logging
9
 
10
  logger = logging.getLogger("database-bot")
11
 
12
- # Motivation vs Logic: MongoDB access is retained only as a storage/index transport layer
13
- # for retrieval artifacts; the chatbot runtime should not expose a separate MongoDB RAG service.
14
  class DatabaseManager:
15
  def __init__(self):
16
  self.embedding_model = None
17
  self.index = None
18
  self.symptom_vectors = None
19
  self.symptom_docs = None
20
-
21
- # MongoDB connections
22
- self.client = None
23
- self.iclient = None
24
- self.symptom_client = None
25
-
26
- # Collections
27
- self.qa_collection = None
28
- self.index_collection = None
29
- self.symptom_col = None
30
- self.fs = None
31
-
32
  def initialize_embedding_model(self):
33
  """Initialize the SentenceTransformer model"""
34
  logger.info("[Embedder] 📥 Loading SentenceTransformer Model...")
@@ -41,47 +27,20 @@ class DatabaseManager:
41
  raise
42
 
43
  def initialize_mongodb(self):
44
- """Initialize MongoDB connections and collections"""
45
- # Root Cause vs Logic: the retrieval layer depends on MongoDB only for persisted
46
- # artifacts; we keep one initializer here so all collections come from a single path.
47
- # QA data
48
- self.client = MongoClient(mongo_uri)
49
- db = self.client["MedicalChatbotDB"]
50
- self.qa_collection = db["qa_data"]
51
-
52
- # FAISS Index data
53
- self.iclient = MongoClient(index_uri)
54
- idb = self.iclient["MedicalChatbotDB"]
55
- self.index_collection = idb["faiss_index_files"]
56
-
57
- # Symptom Diagnosis data
58
- self.symptom_client = MongoClient(mongo_uri)
59
- self.symptom_col = self.symptom_client["MedicalChatbotDB"]["symptom_diagnosis"]
60
 
61
- # GridFS for FAISS index
62
- self.fs = gridfs.GridFS(idb, collection="faiss_index_files")
63
-
64
  def load_faiss_index(self):
65
- """Lazy load FAISS index from GridFS"""
66
- if self.index is None:
67
- logger.info("[KB] ⏳ Loading FAISS index from GridFS...")
68
- existing_file = self.fs.find_one({"filename": "faiss_index.bin"})
69
- if existing_file:
70
- stored_index_bytes = existing_file.read()
71
- index_bytes_np = np.frombuffer(stored_index_bytes, dtype='uint8')
72
- self.index = faiss.deserialize_index(index_bytes_np)
73
- logger.info("[KB] ✅ FAISS Index Loaded")
74
- else:
75
- logger.error("[KB] ❌ FAISS index not found in GridFS.")
76
- return self.index
77
-
78
  def load_symptom_vectors(self):
79
- """Lazy load symptom vectors for diagnosis"""
80
- if self.symptom_vectors is None:
81
- all_docs = list(self.symptom_col.find({}, {"embedding": 1, "answer": 1, "question": 1, "prognosis": 1}))
82
- self.symptom_docs = all_docs
83
- self.symptom_vectors = np.array([doc["embedding"] for doc in all_docs], dtype=np.float32)
84
-
85
  def get_embedding_model(self):
86
  """Get the embedding model"""
87
  if self.embedding_model is None:
@@ -89,16 +48,12 @@ class DatabaseManager:
89
  return self.embedding_model
90
 
91
  def get_qa_collection(self):
92
- """Get QA collection"""
93
- if self.qa_collection is None:
94
- self.initialize_mongodb()
95
- return self.qa_collection
96
-
97
  def get_symptom_collection(self):
98
- """Get symptom collection"""
99
- if self.symptom_col is None:
100
- self.initialize_mongodb()
101
- return self.symptom_col
102
 
103
  # Global database manager instance
104
  db_manager = DatabaseManager()
 
1
  # api/database.py
 
2
  import numpy as np
 
 
3
  from sentence_transformers import SentenceTransformer
4
+ from .config import MODEL_CACHE_DIR, EMBEDDING_MODEL_DEVICE
5
  import logging
6
 
7
  logger = logging.getLogger("database-bot")
8
 
9
+ # Root Cause vs Logic: the runtime used to initialize a MongoDB/FAISS RAG data plane at import time,
10
+ # which caused DNS failures when the index cluster was unavailable; the app now keeps only the embedder hook.
11
  class DatabaseManager:
12
  def __init__(self):
13
  self.embedding_model = None
14
  self.index = None
15
  self.symptom_vectors = None
16
  self.symptom_docs = None
17
+
 
 
 
 
 
 
 
 
 
 
 
18
  def initialize_embedding_model(self):
19
  """Initialize the SentenceTransformer model"""
20
  logger.info("[Embedder] 📥 Loading SentenceTransformer Model...")
 
27
  raise
28
 
29
  def initialize_mongodb(self):
30
+ """Compatibility stub for legacy MongoDB-backed retrieval startup."""
31
+ # Root Cause vs Logic: all MongoDB-backed RAG wiring has been removed from the runtime;
32
+ # keeping this as a no-op preserves import compatibility without triggering DNS lookups.
33
+ logger.info("[DB] MongoDB initialization skipped (legacy RAG disabled).")
 
 
 
 
 
 
 
 
 
 
 
 
34
 
 
 
 
35
  def load_faiss_index(self):
36
+ """Legacy FAISS loading disabled."""
37
+ return None
38
+
 
 
 
 
 
 
 
 
 
 
39
  def load_symptom_vectors(self):
40
+ """Legacy symptom vector loading disabled."""
41
+ self.symptom_docs = []
42
+ self.symptom_vectors = np.empty((0, 0), dtype=np.float32)
43
+
 
 
44
  def get_embedding_model(self):
45
  """Get the embedding model"""
46
  if self.embedding_model is None:
 
48
  return self.embedding_model
49
 
50
  def get_qa_collection(self):
51
+ """Legacy QA collection access disabled."""
52
+ return None
53
+
 
 
54
  def get_symptom_collection(self):
55
+ """Legacy symptom collection access disabled."""
56
+ return None
 
 
57
 
58
  # Global database manager instance
59
  db_manager = DatabaseManager()
utils/clear_mongo.py CHANGED
@@ -1,48 +1,7 @@
1
- from pymongo import MongoClient
2
- from dotenv import load_dotenv
3
- import os
4
 
5
- # # Load environment variables from .env
6
- # load_dotenv()
7
 
8
- ##-------------##
9
- # FOR QA CLUSTER
10
- ##-------------##
11
-
12
- # mongo_uri = os.getenv("MONGO_URI")
13
- # if not mongo_uri:
14
- # raise ValueError("❌ MongoDB URI (MongoURI) is missing!")
15
-
16
- # client = MongoClient(mongo_uri)
17
- # db = client["MedicalChatbotDB"] # Use the same database name as in your main script
18
-
19
- # # To drop just the collection storing the FAISS index:
20
- # db.drop_collection("qa_data")
21
- # print("Dropped collection 'qa_data' from MedicalChatbotDB.")
22
-
23
- # # Alternatively, to drop the entire database:
24
- # client.drop_database("MedicalChatbotDB")
25
- # print("Dropped database 'MedicalChatbotDB'.")
26
-
27
-
28
- ##-------------##
29
- # FOR INDEX CLUSTER
30
- ##-------------##
31
-
32
- # Load environment variables from .env
33
- # load_dotenv()
34
- # index_uri = os.getenv("INDEX_URI")
35
- # if not index_uri:
36
- # raise ValueError("❌ MongoDB URI (IndexURI) is missing!")
37
-
38
- # iclient = MongoClient(index_uri)
39
- # idb = iclient["MedicalChatbotDB"] # Use the same database name as in your main script
40
-
41
- # # To drop just the collection storing the FAISS index:
42
- # idb.drop_collection("faiss_index_files.files")
43
- # idb.drop_collection("faiss_index_files.chunks")
44
- # print("Dropped collection 'faiss_index_files' and chunks from MedicalChatbotDB.")
45
-
46
- # # Alternatively, to drop the entire database:
47
- # iclient.drop_database("MedicalChatbotDB")
48
- # print("Dropped database 'MedicalChatbotDB'.")
 
1
+ """Legacy MongoDB cleanup helpers removed with the retired RAG data plane."""
 
 
2
 
3
+ # Root Cause vs Logic: this utility existed only to delete MongoDB-hosted QA/FAISS artifacts,
4
+ # so the live code path is intentionally replaced with a harmless stub to avoid accidental use.
5
 
6
+ def __getattr__(name):
7
+ raise AttributeError(f"legacy cleanup utility disabled: {name}")