Repository navigation
Expand file tree
/
Copy pathindexer.py
More file actions
216 lines (183 loc) · 7.9 KB
/
Copy pathindexer.py
File metadata and controls
216 lines (183 loc) · 7.9 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
import os
import uuid
import torch
import logging
import time
from dataclasses import dataclass
from typing import List, Dict
from pathlib import Path
from qdrant_client import QdrantClient
from langchain_qdrant import QdrantVectorStore
from langchain_huggingface import HuggingFaceEmbeddings
from qdrant_client.http.models import Distance, VectorParams, Filter, FieldCondition, MatchValue
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import (
TextLoader,
CSVLoader,
Docx2txtLoader,
UnstructuredExcelLoader,
PyMuPDFLoader,
UnstructuredPowerPointLoader,
)
from storage import MinimaStore, IndexingStatus
logger = logging.getLogger(__name__)
@dataclass
class Config:
EXTENSIONS_TO_LOADERS = {
".pdf": PyMuPDFLoader,
".pptx": UnstructuredPowerPointLoader,
".ppt": UnstructuredPowerPointLoader,
".xls": UnstructuredExcelLoader,
".xlsx": UnstructuredExcelLoader,
".docx": Docx2txtLoader,
".doc": Docx2txtLoader,
".txt": TextLoader,
".md": TextLoader,
".csv": CSVLoader,
}
DEVICE = torch.device(
"mps" if torch.backends.mps.is_available() else
"cuda" if torch.cuda.is_available() else
"cpu"
)
START_INDEXING = os.environ.get("START_INDEXING")
LOCAL_FILES_PATH = os.environ.get("LOCAL_FILES_PATH")
CONTAINER_PATH = os.environ.get("CONTAINER_PATH")
QDRANT_COLLECTION = "mnm_storage"
QDRANT_BOOTSTRAP = "qdrant"
EMBEDDING_MODEL_ID = os.environ.get("EMBEDDING_MODEL_ID")
EMBEDDING_SIZE = os.environ.get("EMBEDDING_SIZE")
CHUNK_SIZE = 500
CHUNK_OVERLAP = 200
class Indexer:
def __init__(self):
self.config = Config()
self.qdrant = self._initialize_qdrant()
self.embed_model = self._initialize_embeddings()
self.document_store = self._setup_collection()
self.text_splitter = self._initialize_text_splitter()
def _initialize_qdrant(self) -> QdrantClient:
return QdrantClient(host=self.config.QDRANT_BOOTSTRAP)
def _initialize_embeddings(self) -> HuggingFaceEmbeddings:
return HuggingFaceEmbeddings(
model_name=self.config.EMBEDDING_MODEL_ID,
model_kwargs={'device': self.config.DEVICE},
encode_kwargs={'normalize_embeddings': False}
)
def _initialize_text_splitter(self) -> RecursiveCharacterTextSplitter:
return RecursiveCharacterTextSplitter(
chunk_size=self.config.CHUNK_SIZE,
chunk_overlap=self.config.CHUNK_OVERLAP
)
def _setup_collection(self) -> QdrantVectorStore:
if not self.qdrant.collection_exists(self.config.QDRANT_COLLECTION):
self.qdrant.create_collection(
collection_name=self.config.QDRANT_COLLECTION,
vectors_config=VectorParams(
size=self.config.EMBEDDING_SIZE,
distance=Distance.COSINE
),
)
self.qdrant.create_payload_index(
collection_name=self.config.QDRANT_COLLECTION,
field_name="fpath",
field_schema="keyword"
)
return QdrantVectorStore(
client=self.qdrant,
collection_name=self.config.QDRANT_COLLECTION,
embedding=self.embed_model,
)
def _create_loader(self, file_path: str):
file_extension = Path(file_path).suffix.lower()
loader_class = self.config.EXTENSIONS_TO_LOADERS.get(file_extension)
if not loader_class:
raise ValueError(f"Unsupported file type: {file_extension}")
return loader_class(file_path=file_path)
def _process_file(self, loader) -> List[str]:
try:
documents = loader.load_and_split(self.text_splitter)
if not documents:
logger.warning(f"No documents loaded from {loader.file_path}")
return []
for doc in documents:
doc.metadata['file_path'] = loader.file_path
uuids = [str(uuid.uuid4()) for _ in range(len(documents))]
ids = self.document_store.add_documents(documents=documents, ids=uuids)
logger.info(f"Successfully processed {len(ids)} documents from {loader.file_path}")
return ids
except Exception as e:
logger.error(f"Error processing file {loader.file_path}: {str(e)}")
return []
def index(self, message: Dict[str, any]) -> None:
start = time.time()
path, file_id, last_updated_seconds = message["path"], message["file_id"], message["last_updated_seconds"]
logger.info(f"Processing file: {path} (ID: {file_id})")
indexing_status: IndexingStatus = MinimaStore.check_needs_indexing(fpath=path, last_updated_seconds=last_updated_seconds)
if indexing_status != IndexingStatus.no_need_reindexing:
logger.info(f"Indexing needed for {path} with status: {indexing_status}")
try:
if indexing_status == IndexingStatus.need_reindexing:
logger.info(f"Removing {path} from index storage for reindexing")
self.remove_from_storage(files_to_remove=[path])
loader = self._create_loader(path)
ids = self._process_file(loader)
if ids:
logger.info(f"Successfully indexed {path} with IDs: {ids}")
except Exception as e:
logger.error(f"Failed to index file {path}: {str(e)}")
else:
logger.info(f"Skipping {path}, no indexing required. timestamp didn't change")
end = time.time()
logger.info(f"Processing took {end - start} seconds for file {path}")
def purge(self, message: Dict[str, any]) -> None:
existing_file_paths: list[str] = message["existing_file_paths"]
files_to_remove = MinimaStore.find_removed_files(existing_file_paths=set(existing_file_paths))
if len(files_to_remove) > 0:
logger.info(f"purge processing removing old files {files_to_remove}")
self.remove_from_storage(files_to_remove)
else:
logger.info("Nothing to purge")
def remove_from_storage(self, files_to_remove: list[str]):
filter_conditions = Filter(
must=[
FieldCondition(
key="fpath",
match=MatchValue(value=fpath)
)
for fpath in files_to_remove
]
)
response = self.qdrant.delete(
collection_name=self.config.QDRANT_COLLECTION,
points_selector=filter_conditions,
wait=True
)
logger.info(f"Delete response for {len(files_to_remove)} for files: {files_to_remove} is: {response}")
def find(self, query: str) -> Dict[str, any]:
try:
logger.info(f"Searching for: {query}")
found = self.document_store.search(query, search_type="similarity")
if not found:
logger.info("No results found")
return {"links": set(), "output": ""}
links = set()
results = []
for item in found:
path = item.metadata["file_path"].replace(
self.config.CONTAINER_PATH,
self.config.LOCAL_FILES_PATH
)
links.add(f"file://{path}")
results.append(item.page_content)
output = {
"links": links,
"output": ". ".join(results)
}
logger.info(f"Found {len(found)} results")
return output
except Exception as e:
logger.error(f"Search failed: {str(e)}")
return {"error": "Unable to find anything for the given query"}
def embed(self, query: str):
return self.embed_model.embed_query(query)