-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
279 lines (226 loc) · 9.47 KB
/
vector.py
File metadata and controls
279 lines (226 loc) · 9.47 KB
1
2
3
4
5
6
7
8
9
10
11
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
46
47
48
49
50
51
52
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from langchain_ollama import OllamaEmbeddings
from langchain_chroma import Chroma
from langchain_core.documents import Document
from langchain_text_splitters import RecursiveCharacterTextSplitter
import os
import pandas as pd
import json
from pathlib import Path
from typing import List, Dict, Any
from config import Config
EMBED_MAX_CHARS = 512
class UniversalVectorStore:
"""
Universal vector store that handles any document type:
CSV, TXT, MD, JSON, PDF, etc.
"""
def __init__(self, config: Config = None):
self.config = config or Config()
self.embeddings = OllamaEmbeddings(model=self.config.EMBEDDING_MODEL,
num_ctx = 2048)
self.vector_store = Chroma(
collection_name=self.config.COLLECTION_NAME,
persist_directory=self.config.DB_LOCATION,
embedding_function=self.embeddings
)
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=self.config.CHUNK_SIZE,
chunk_overlap=self.config.CHUNK_OVERLAP,
length_function=len,
)
def process_csv(self, file_path: str) -> List[Document]:
"""Process CSV files - auto-detects columns"""
df = pd.read_csv(file_path)
documents = []
if self.config.CSV_CONTENT_COLUMNS:
content_cols = self.config.CSV_CONTENT_COLUMNS
else:
content_cols = df.select_dtypes(include=['object']).columns.tolist()
if self.config.CSV_METADATA_COLUMNS:
metadata_cols = self.config.CSV_METADATA_COLUMNS
else:
metadata_cols = [col for col in df.columns if col not in content_cols]
for idx, row in df.iterrows():
content_parts = []
for col in content_cols:
if pd.notna(row[col]):
content_parts.append(f"{col}: {row[col]}")
page_content = "\n".join(content_parts)
metadata = {"source": file_path, "row_id": idx}
for col in metadata_cols:
if pd.notna(row[col]):
metadata[col] = row[col]
documents.append(Document(
page_content=page_content,
metadata=metadata
))
return documents
def process_text_file(self, file_path: str) -> List[Document]:
"""Process TXT/MD files with chunking"""
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
chunks = self.text_splitter.split_text(text)
safe_chunks = [
chunk[:EMBED_MAX_CHARS]
for chunk in chunks
if chunk and chunk.strip()
]
documents = []
for idx, chunk in enumerate(chunks):
documents.append(Document(
page_content=chunk,
metadata={
"source": file_path,
"chunk_id": idx,
"file_type": Path(file_path).suffix
}
))
return documents
def process_json(self, file_path: str) -> List[Document]:
"""Process JSON files - handles arrays and objects"""
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
documents = []
if isinstance(data, list):
for idx, item in enumerate(data):
if isinstance(item, dict):
content = json.dumps(item, indent=2)
documents.append(Document(
page_content=content,
metadata={
"source": file_path,
"item_id": idx,
**{k: v for k, v in item.items() if isinstance(v, (str, int, float, bool))}
}
))
else:
documents.append(Document(
page_content=str(item),
metadata={"source": file_path, "item_id": idx}
))
elif isinstance(data, dict):
content = json.dumps(data, indent=2)
documents.append(Document(
page_content=content,
metadata={"source": file_path, "type": "json_object"}
))
return documents
def process_pdf(self, file_path: str) -> List[Document]:
"""Process PDF files"""
try:
from pypdf import PdfReader
reader = PdfReader(file_path)
documents = []
for page_num, page in enumerate(reader.pages):
text = page.extract_text()
if not text:
continue
text = text[:100_000]
chunks = self.text_splitter.split_text(text)
safe_chunks = [
chunk[:EMBED_MAX_CHARS]
for chunk in chunks
if chunk and chunk.strip()
]
for chunk_idx, chunk in enumerate(chunks):
documents.append(Document(
page_content=chunk,
metadata={
"source": file_path,
"page": page_num + 1,
"chunk_id": chunk_idx,
"file_type": "pdf"
}
))
return documents
except ImportError:
raise ImportError("pypdf is required for PDF processing. Install with: pip install pypdf")
def add_documents_from_file(self, file_path: str) -> int:
"""
Universal document processor - detects file type and processes accordingly
Returns: number of documents added
"""
file_ext = Path(file_path).suffix.lower()
if file_ext == '.csv':
documents = self.process_csv(file_path)
elif file_ext in ['.txt', '.md']:
documents = self.process_text_file(file_path)
elif file_ext == '.json':
documents = self.process_json(file_path)
elif file_ext == '.pdf':
documents = self.process_pdf(file_path)
else:
raise ValueError(f"Unsupported file type: {file_ext}")
existing_count = len(self.vector_store.get()["ids"]) if self.vector_store.get()["ids"] else 0
ids = [f"doc_{existing_count + i}" for i in range(len(documents))]
for doc, doc_id in zip(documents, ids):
self.vector_store.add_documents(
documents=[doc],
ids=[doc_id]
)
return len(documents)
def add_documents_from_text(self, text: str, metadata: Dict[str, Any] = None) -> int:
"""Add documents directly from text (useful for API)"""
chunks = self.text_splitter.split_text(text)
safe_chunks = [
chunk[:EMBED_MAX_CHARS]
for chunk in chunks
if chunk and chunk.strip()
]
documents = []
for idx, chunk in enumerate(chunks):
doc_metadata = metadata or {}
doc_metadata["chunk_id"] = idx
documents.append(Document(
page_content=chunk,
metadata=doc_metadata
))
existing_count = len(self.vector_store.get()["ids"]) if self.vector_store.get()["ids"] else 0
ids = [f"doc_{existing_count + i}" for i in range(len(documents))]
for doc, doc_id in zip(documents, ids):
self.vector_store.add_documents(
documents=[doc],
ids=[doc_id]
)
return len(documents)
def get_retriever(self, k: int = None):
"""Get retriever for querying"""
k = k or self.config.RETRIEVAL_K
return self.vector_store.as_retriever(search_kwargs={"k": k})
def search(self, query: str, k: int = None) -> List[Document]:
"""Search for relevant documents"""
k = k or self.config.RETRIEVAL_K
retriever = self.get_retriever(k)
return retriever.invoke(query)
def get_stats(self) -> Dict[str, Any]:
"""Get statistics about the vector store"""
data = self.vector_store.get()
total_docs = len(data["ids"]) if data["ids"] else 0
sources = {}
if data["metadatas"]:
for metadata in data["metadatas"]:
source = metadata.get("source", "unknown")
sources[source] = sources.get(source, 0) + 1
return {
"total_documents": total_docs,
"sources": sources,
"collection_name": self.config.COLLECTION_NAME
}
def clear(self):
"""Clear all documents from the vector store"""
try:
self.vector_store.delete_collection()
except:
pass
self.vector_store = Chroma(
collection_name=self.config.COLLECTION_NAME,
persist_directory=self.config.DB_LOCATION,
embedding_function=self.embeddings
)
vector_store = None
def get_vector_store(config: Config = None) -> UniversalVectorStore:
"""Get or create global vector store instance"""
global vector_store
if vector_store is None or config is not None:
vector_store = UniversalVectorStore(config)
return vector_store