atulkrs commited on
Commit
f90ed23
·
verified ·
1 Parent(s): 37f130f

Add full RAG pipeline: agent, rag_engine, generator, knowledge_base, full Gradio UI

Browse files
Files changed (1) hide show
  1. rag_engine.py +16 -6
rag_engine.py CHANGED
@@ -49,11 +49,13 @@ class MLOpsRAGEngine:
49
  Settings.llm = None # We handle generation separately
50
 
51
  def _setup_vector_store(self):
 
52
  self.chroma_client = chromadb.PersistentClient(path=self.chroma_path)
53
  self.chroma_collection = self.chroma_client.get_or_create_collection(
54
  name=COLLECTION_NAME,
55
  metadata={"hnsw:space": "cosine"},
56
  )
 
57
  self.vector_store = ChromaVectorStore(chroma_collection=self.chroma_collection)
58
  self.storage_context = StorageContext.from_defaults(
59
  vector_store=self.vector_store
@@ -62,23 +64,30 @@ class MLOpsRAGEngine:
62
  def build_index(self, force_rebuild: bool = False) -> VectorStoreIndex:
63
  """Build or load the vector index from the knowledge base documents."""
64
  existing_count = self.chroma_collection.count()
 
65
 
66
  if existing_count > 0 and not force_rebuild:
67
- logger.info(f"Loading existing index ({existing_count} chunks in ChromaDB)")
68
  self.index = VectorStoreIndex.from_vector_store(
69
  vector_store=self.vector_store,
70
  embed_model=self.embed_model,
71
  )
72
  return self.index
73
 
74
- logger.info(f"Building index from {self.knowledge_base_path}")
 
 
75
  if not self.knowledge_base_path.exists():
76
  raise FileNotFoundError(f"Knowledge base path not found: {self.knowledge_base_path}")
77
 
 
 
 
78
  # Load .txt files directly — avoids the llama-index-readers-file dependency
79
  documents = []
80
- for txt_file in sorted(self.knowledge_base_path.glob("*.txt")):
81
  text = txt_file.read_text(encoding="utf-8")
 
82
  documents.append(Document(
83
  text=text,
84
  metadata={"file_name": txt_file.name},
@@ -87,7 +96,7 @@ class MLOpsRAGEngine:
87
 
88
  if not documents:
89
  raise FileNotFoundError(f"No .txt files found in {self.knowledge_base_path}")
90
- logger.info(f"Loaded {len(documents)} documents")
91
 
92
  parser = SentenceSplitter(
93
  chunk_size=CHUNK_SIZE,
@@ -95,7 +104,7 @@ class MLOpsRAGEngine:
95
  paragraph_separator="\n\n",
96
  )
97
  nodes = parser.get_nodes_from_documents(documents)
98
- logger.info(f"Created {len(nodes)} chunks")
99
 
100
  self.index = VectorStoreIndex(
101
  nodes=nodes,
@@ -103,7 +112,8 @@ class MLOpsRAGEngine:
103
  embed_model=self.embed_model,
104
  show_progress=True,
105
  )
106
- logger.info("Index built successfully")
 
107
  return self.index
108
 
109
  def retrieve(
 
49
  Settings.llm = None # We handle generation separately
50
 
51
  def _setup_vector_store(self):
52
+ print(f"[DEBUG] ChromaDB path: {self.chroma_path}", flush=True)
53
  self.chroma_client = chromadb.PersistentClient(path=self.chroma_path)
54
  self.chroma_collection = self.chroma_client.get_or_create_collection(
55
  name=COLLECTION_NAME,
56
  metadata={"hnsw:space": "cosine"},
57
  )
58
+ print(f"[DEBUG] ChromaDB collection '{COLLECTION_NAME}' size on init: {self.chroma_collection.count()}", flush=True)
59
  self.vector_store = ChromaVectorStore(chroma_collection=self.chroma_collection)
60
  self.storage_context = StorageContext.from_defaults(
61
  vector_store=self.vector_store
 
64
  def build_index(self, force_rebuild: bool = False) -> VectorStoreIndex:
65
  """Build or load the vector index from the knowledge base documents."""
66
  existing_count = self.chroma_collection.count()
67
+ print(f"[DEBUG] build_index called. existing ChromaDB count={existing_count}, force_rebuild={force_rebuild}", flush=True)
68
 
69
  if existing_count > 0 and not force_rebuild:
70
+ print(f"[DEBUG] Reusing existing index ({existing_count} chunks)", flush=True)
71
  self.index = VectorStoreIndex.from_vector_store(
72
  vector_store=self.vector_store,
73
  embed_model=self.embed_model,
74
  )
75
  return self.index
76
 
77
+ print(f"[DEBUG] Knowledge base path: {self.knowledge_base_path}", flush=True)
78
+ print(f"[DEBUG] Knowledge base path exists: {self.knowledge_base_path.exists()}", flush=True)
79
+
80
  if not self.knowledge_base_path.exists():
81
  raise FileNotFoundError(f"Knowledge base path not found: {self.knowledge_base_path}")
82
 
83
+ txt_files = sorted(self.knowledge_base_path.glob("*.txt"))
84
+ print(f"[DEBUG] .txt files found: {[f.name for f in txt_files]}", flush=True)
85
+
86
  # Load .txt files directly — avoids the llama-index-readers-file dependency
87
  documents = []
88
+ for txt_file in txt_files:
89
  text = txt_file.read_text(encoding="utf-8")
90
+ print(f"[DEBUG] loaded {txt_file.name} ({len(text)} chars)", flush=True)
91
  documents.append(Document(
92
  text=text,
93
  metadata={"file_name": txt_file.name},
 
96
 
97
  if not documents:
98
  raise FileNotFoundError(f"No .txt files found in {self.knowledge_base_path}")
99
+ print(f"[DEBUG] Total documents loaded: {len(documents)}", flush=True)
100
 
101
  parser = SentenceSplitter(
102
  chunk_size=CHUNK_SIZE,
 
104
  paragraph_separator="\n\n",
105
  )
106
  nodes = parser.get_nodes_from_documents(documents)
107
+ print(f"[DEBUG] Total chunks created: {len(nodes)}", flush=True)
108
 
109
  self.index = VectorStoreIndex(
110
  nodes=nodes,
 
112
  embed_model=self.embed_model,
113
  show_progress=True,
114
  )
115
+ final_count = self.chroma_collection.count()
116
+ print(f"[DEBUG] Index built. ChromaDB collection size after indexing: {final_count}", flush=True)
117
  return self.index
118
 
119
  def retrieve(