JanviMl commited on
Commit
91a925f
Β·
verified Β·
1 Parent(s): f37c4e6

Update src/enhanced_rag_system.py

Browse files
Files changed (1) hide show
  1. src/enhanced_rag_system.py +58 -114
src/enhanced_rag_system.py CHANGED
@@ -6,22 +6,15 @@ from typing import List, Tuple, Dict, Optional
6
  from langchain.schema import Document
7
  import re
8
  import json
9
- import warnings
10
- warnings.filterwarnings('ignore')
11
 
12
- # Import vector store components with better error handling
13
  try:
14
  import chromadb
15
  from chromadb.config import Settings
16
  from sentence_transformers import SentenceTransformer
17
  VECTOR_STORE_AVAILABLE = True
18
- print("βœ… ChromaDB and SentenceTransformers imported successfully")
19
- except ImportError as e:
20
- VECTOR_STORE_AVAILABLE = False
21
- print(f"⚠️ Vector store import error: {e}")
22
- except Exception as e:
23
  VECTOR_STORE_AVAILABLE = False
24
- print(f"⚠️ Vector store initialization error: {e}")
25
 
26
  # Import LLM components
27
  try:
@@ -29,12 +22,8 @@ try:
29
  LLM_AVAILABLE = bool(os.getenv("OPENAI_API_KEY"))
30
  if LLM_AVAILABLE:
31
  openai.api_key = os.getenv("OPENAI_API_KEY")
32
- print("βœ… OpenAI API key found and configured")
33
- else:
34
- print("⚠️ OpenAI API key not found in environment")
35
  except ImportError:
36
  LLM_AVAILABLE = False
37
- print("⚠️ OpenAI library not available")
38
 
39
  # Import our custom modules
40
  from document_processor import DocumentProcessor
@@ -100,31 +89,17 @@ class EnhancedRAGSystem:
100
  print("⚠️ Using fallback mode with template responses")
101
 
102
  def _initialize_vector_store(self):
103
- """Initialize ChromaDB vector store with better error handling"""
104
  if not VECTOR_STORE_AVAILABLE:
105
- print("⚠️ ChromaDB/SentenceTransformers not available, using in-memory search")
106
  return
107
 
108
  try:
109
- print("πŸ”§ Initializing ChromaDB...")
110
-
111
- # Try different ChromaDB configurations for HuggingFace compatibility
112
- try:
113
- # First try: PersistentClient (newer API)
114
- self.chroma_client = chromadb.PersistentClient(path="./chroma_db")
115
- print("βœ… Using ChromaDB PersistentClient")
116
- except Exception as e1:
117
- try:
118
- # Second try: Client with settings (older API)
119
- self.chroma_client = chromadb.Client(Settings(
120
- chroma_db_impl="duckdb+parquet",
121
- persist_directory="./chroma_db"
122
- ))
123
- print("βœ… Using ChromaDB Client with Settings")
124
- except Exception as e2:
125
- # Third try: Simple client
126
- self.chroma_client = chromadb.Client()
127
- print("βœ… Using ChromaDB in-memory client")
128
 
129
  # Get or create collection
130
  collection_name = "finsolve_documents"
@@ -138,25 +113,15 @@ class EnhancedRAGSystem:
138
  )
139
  print(f"βœ… Created new ChromaDB collection: {collection_name}")
140
 
141
- # Initialize embedding model with smaller model for HuggingFace
142
- try:
143
- self.embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
144
- print("βœ… Loaded sentence transformer model: all-MiniLM-L6-v2")
145
- except Exception as e:
146
- # Fallback to even smaller model
147
- try:
148
- self.embedding_model = SentenceTransformer("paraphrase-MiniLM-L3-v2")
149
- print("βœ… Loaded fallback sentence transformer model: paraphrase-MiniLM-L3-v2")
150
- except Exception as e2:
151
- print(f"❌ Failed to load embedding model: {e2}")
152
- raise e2
153
 
154
  self.vector_store_initialized = True
155
 
156
  except Exception as e:
157
  print(f"⚠️ ChromaDB initialization failed: {str(e)}")
158
  print("⚠️ Falling back to in-memory search")
159
- self.vector_store_initialized = False
160
 
161
  def _initialize_llm(self):
162
  """Initialize OpenAI LLM"""
@@ -165,7 +130,7 @@ class EnhancedRAGSystem:
165
  return
166
 
167
  try:
168
- # Test OpenAI connection with updated API
169
  response = openai.ChatCompletion.create(
170
  model=self.llm_model,
171
  messages=[{"role": "user", "content": "Hello"}],
@@ -182,15 +147,10 @@ class EnhancedRAGSystem:
182
 
183
  def _load_documents_to_vector_store(self):
184
  """Load documents into ChromaDB vector store"""
185
- if not self.vector_store_initialized or not self.embedding_model:
186
  return
187
 
188
  try:
189
- # Check if documents already loaded
190
- if self.collection.count() > 0:
191
- print(f"βœ… ChromaDB already contains {self.collection.count()} documents")
192
- return
193
-
194
  print("πŸ“„ Loading documents into vector store...")
195
 
196
  texts = []
@@ -205,7 +165,7 @@ class EnhancedRAGSystem:
205
  "title": doc.metadata.get("title", "Document"),
206
  "department": doc.metadata.get("department", "General"),
207
  "type": doc.metadata.get("type", "Document"),
208
- "chunk_id": str(doc.metadata.get("chunk_id", 0)),
209
  "source": doc.metadata.get("source", "unknown")
210
  }
211
 
@@ -213,23 +173,16 @@ class EnhancedRAGSystem:
213
  metadatas.append(metadata)
214
  ids.append(doc_id)
215
 
216
- # Generate embeddings in batches to avoid memory issues
217
- batch_size = 10
218
- for i in range(0, len(texts), batch_size):
219
- batch_texts = texts[i:i+batch_size]
220
- batch_metadatas = metadatas[i:i+batch_size]
221
- batch_ids = ids[i:i+batch_size]
222
-
223
- # Generate embeddings
224
- embeddings = self.embedding_model.encode(batch_texts).tolist()
225
-
226
- # Add to ChromaDB
227
- self.collection.add(
228
- embeddings=embeddings,
229
- documents=batch_texts,
230
- metadatas=batch_metadatas,
231
- ids=batch_ids
232
- )
233
 
234
  print(f"βœ… Loaded {len(self.documents)} documents into ChromaDB")
235
 
@@ -242,10 +195,10 @@ class EnhancedRAGSystem:
242
  print("πŸ€– FINSOLVE RAG SYSTEM STATUS")
243
  print("="*50)
244
  print(f"βœ… Python: Core system initialized")
245
- print(f"{'βœ…' if self.vector_store_initialized else '⚠️'} ChromaDB Vector Store: {'Ready' if self.vector_store_initialized else 'Fallback mode'}")
246
- print(f"{'βœ…' if self.llm_initialized else '⚠️'} OpenAI LLM: {'OpenAI GPT' if self.llm_initialized else 'Template mode'}")
247
  print(f"βœ… Streamlit: UI active")
248
- print(f"πŸ”„ FastAPI: Simulated API")
249
  print(f"βœ… Authentication: JWT-style RBAC")
250
  print(f"βœ… NLP: Intent classification + {'LLM' if self.llm_initialized else 'Templates'}")
251
  print(f"βœ… RAG: Vector retrieval + context augmentation")
@@ -647,6 +600,34 @@ Please contact your administrator if you need access to additional information."
647
 
648
  return None
649
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650
  except Exception as e:
651
  print(f"❌ Error creating table: {str(e)}")
652
  return None
@@ -732,20 +713,11 @@ Please contact your administrator if you need access to additional information."
732
  "streamlit": "βœ… Active",
733
  "vector_store": "βœ… ChromaDB" if self.vector_store_initialized else "⚠️ Fallback",
734
  "llm": f"βœ… {self.llm_model}" if self.llm_initialized else "⚠️ Templates",
735
- "fastapi": "βœ… Real FastAPI" if self._check_fastapi_running() else "πŸ”„ Simulated",
736
  "authentication": "βœ… JWT-style RBAC"
737
  }
738
  }
739
 
740
- def _check_fastapi_running(self) -> bool:
741
- """Check if FastAPI server is running"""
742
- try:
743
- import requests
744
- response = requests.get("http://localhost:8000/health", timeout=2)
745
- return response.status_code == 200
746
- except:
747
- return False
748
-
749
  def get_available_documents_for_role(self, role: str) -> List[Dict]:
750
  """Get list of documents available for a specific role"""
751
  accessible_docs = self.auth_system.get_accessible_documents(role)
@@ -759,32 +731,4 @@ Please contact your administrator if you need access to additional information."
759
  **doc_info[doc_name]
760
  })
761
 
762
- return available❌ Error creating visualization: {str(e)}")
763
- return None
764
-
765
- def _create_data_table(self, content: str, query_intent: str) -> Optional[str]:
766
- """Create data tables from content"""
767
- try:
768
- if query_intent == "finance":
769
- data = {
770
- 'Metric': ['Q4 Revenue', 'Annual Revenue', 'Net Income', 'Gross Margin', 'ROI'],
771
- 'Value': ['$2.6B', '$9.4B', '$325M', '64%', '15%'],
772
- 'YoY Growth': ['+35%', '+28%', '+18%', '+6%', '+3%']
773
- }
774
- df = pd.DataFrame(data)
775
- return df.to_html(index=False, classes='table table-striped', table_id='financial-metrics')
776
-
777
- elif query_intent == "marketing":
778
- data = {
779
- 'Campaign': ['Digital Ads', 'Influencer', 'Email', 'Events'],
780
- 'Spend': ['$5M', '$1.5M', '$0.2M', '$2M'],
781
- 'ROI': ['3.5x', '4.2x', '2.0x', '5.0x'],
782
- 'Leads': ['180K', '60K', '25K', '300']
783
- }
784
- df = pd.DataFrame(data)
785
- return df.to_html(index=False, classes='table table-striped', table_id='marketing-metrics')
786
-
787
- return None
788
-
789
- except Exception as e:
790
- print(f"
 
6
  from langchain.schema import Document
7
  import re
8
  import json
 
 
9
 
10
+ # Import vector store components
11
  try:
12
  import chromadb
13
  from chromadb.config import Settings
14
  from sentence_transformers import SentenceTransformer
15
  VECTOR_STORE_AVAILABLE = True
16
+ except ImportError:
 
 
 
 
17
  VECTOR_STORE_AVAILABLE = False
 
18
 
19
  # Import LLM components
20
  try:
 
22
  LLM_AVAILABLE = bool(os.getenv("OPENAI_API_KEY"))
23
  if LLM_AVAILABLE:
24
  openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
 
25
  except ImportError:
26
  LLM_AVAILABLE = False
 
27
 
28
  # Import our custom modules
29
  from document_processor import DocumentProcessor
 
89
  print("⚠️ Using fallback mode with template responses")
90
 
91
  def _initialize_vector_store(self):
92
+ """Initialize ChromaDB vector store"""
93
  if not VECTOR_STORE_AVAILABLE:
94
+ print("⚠️ ChromaDB not available, using in-memory search")
95
  return
96
 
97
  try:
98
+ # Initialize ChromaDB client
99
+ self.chroma_client = chromadb.Client(Settings(
100
+ chroma_db_impl="duckdb+parquet",
101
+ persist_directory="./chroma_db"
102
+ ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  # Get or create collection
105
  collection_name = "finsolve_documents"
 
113
  )
114
  print(f"βœ… Created new ChromaDB collection: {collection_name}")
115
 
116
+ # Initialize embedding model
117
+ self.embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
118
+ print("βœ… Loaded sentence transformer model")
 
 
 
 
 
 
 
 
 
119
 
120
  self.vector_store_initialized = True
121
 
122
  except Exception as e:
123
  print(f"⚠️ ChromaDB initialization failed: {str(e)}")
124
  print("⚠️ Falling back to in-memory search")
 
125
 
126
  def _initialize_llm(self):
127
  """Initialize OpenAI LLM"""
 
130
  return
131
 
132
  try:
133
+ # Test OpenAI connection
134
  response = openai.ChatCompletion.create(
135
  model=self.llm_model,
136
  messages=[{"role": "user", "content": "Hello"}],
 
147
 
148
  def _load_documents_to_vector_store(self):
149
  """Load documents into ChromaDB vector store"""
150
+ if not self.vector_store_initialized or self.collection.count() > 0:
151
  return
152
 
153
  try:
 
 
 
 
 
154
  print("πŸ“„ Loading documents into vector store...")
155
 
156
  texts = []
 
165
  "title": doc.metadata.get("title", "Document"),
166
  "department": doc.metadata.get("department", "General"),
167
  "type": doc.metadata.get("type", "Document"),
168
+ "chunk_id": doc.metadata.get("chunk_id", 0),
169
  "source": doc.metadata.get("source", "unknown")
170
  }
171
 
 
173
  metadatas.append(metadata)
174
  ids.append(doc_id)
175
 
176
+ # Generate embeddings
177
+ embeddings = self.embedding_model.encode(texts).tolist()
178
+
179
+ # Add to ChromaDB
180
+ self.collection.add(
181
+ embeddings=embeddings,
182
+ documents=texts,
183
+ metadatas=metadatas,
184
+ ids=ids
185
+ )
 
 
 
 
 
 
 
186
 
187
  print(f"βœ… Loaded {len(self.documents)} documents into ChromaDB")
188
 
 
195
  print("πŸ€– FINSOLVE RAG SYSTEM STATUS")
196
  print("="*50)
197
  print(f"βœ… Python: Core system initialized")
198
+ print(f"{'βœ…' if self.vector_store_initialized else '⚠️'} ChromaDB Vector Store: {'Available' if self.vector_store_initialized else 'Fallback mode'}")
199
+ print(f"{'βœ…' if self.llm_initialized else '⚠️'} OpenAI LLM: {'Available' if self.llm_initialized else 'Template mode'}")
200
  print(f"βœ… Streamlit: UI active")
201
+ print(f"βœ… FastAPI: Simulated endpoints")
202
  print(f"βœ… Authentication: JWT-style RBAC")
203
  print(f"βœ… NLP: Intent classification + {'LLM' if self.llm_initialized else 'Templates'}")
204
  print(f"βœ… RAG: Vector retrieval + context augmentation")
 
600
 
601
  return None
602
 
603
+ except Exception as e:
604
+ print(f"❌ Error creating visualization: {str(e)}")
605
+ return None
606
+
607
+ def _create_data_table(self, content: str, query_intent: str) -> Optional[str]:
608
+ """Create data tables from content"""
609
+ try:
610
+ if query_intent == "finance":
611
+ data = {
612
+ 'Metric': ['Q4 Revenue', 'Annual Revenue', 'Net Income', 'Gross Margin', 'ROI'],
613
+ 'Value': ['$2.6B', '$9.4B', '$325M', '64%', '15%'],
614
+ 'YoY Growth': ['+35%', '+28%', '+18%', '+6%', '+3%']
615
+ }
616
+ df = pd.DataFrame(data)
617
+ return df.to_html(index=False, classes='table table-striped', table_id='financial-metrics')
618
+
619
+ elif query_intent == "marketing":
620
+ data = {
621
+ 'Campaign': ['Digital Ads', 'Influencer', 'Email', 'Events'],
622
+ 'Spend': ['$5M', '$1.5M', '$0.2M', '$2M'],
623
+ 'ROI': ['3.5x', '4.2x', '2.0x', '5.0x'],
624
+ 'Leads': ['180K', '60K', '25K', '300']
625
+ }
626
+ df = pd.DataFrame(data)
627
+ return df.to_html(index=False, classes='table table-striped', table_id='marketing-metrics')
628
+
629
+ return None
630
+
631
  except Exception as e:
632
  print(f"❌ Error creating table: {str(e)}")
633
  return None
 
713
  "streamlit": "βœ… Active",
714
  "vector_store": "βœ… ChromaDB" if self.vector_store_initialized else "⚠️ Fallback",
715
  "llm": f"βœ… {self.llm_model}" if self.llm_initialized else "⚠️ Templates",
716
+ "fastapi": "βœ… Simulated",
717
  "authentication": "βœ… JWT-style RBAC"
718
  }
719
  }
720
 
 
 
 
 
 
 
 
 
 
721
  def get_available_documents_for_role(self, role: str) -> List[Dict]:
722
  """Get list of documents available for a specific role"""
723
  accessible_docs = self.auth_system.get_accessible_documents(role)
 
731
  **doc_info[doc_name]
732
  })
733
 
734
+ return available