-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.py
368 lines (311 loc) · 14 KB
/
index.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""Indexing service for RAG files."""
import logging
import tempfile
import time
from fastapi import HTTPException, UploadFile, status
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from openai.types.beta.vector_store import FileCounts, VectorStore
from openai.types.beta.vector_stores import VectorStoreFile
from openai.types.beta.vector_stores.vector_store_file import LastError
from supabase import AClient as AsyncClient
from leapfrogai_api.backend.rag.document_loader import load_file, split
from leapfrogai_api.backend.rag.leapfrogai_embeddings import LeapfrogAIEmbeddings
from leapfrogai_api.data.crud_file_bucket import CRUDFileBucket
from leapfrogai_api.data.crud_file_object import CRUDFileObject, FilterFileObject
from leapfrogai_api.data.crud_vector_store import CRUDVectorStore, FilterVectorStore
from leapfrogai_api.backend.types import (
VectorStoreStatus,
VectorStoreFileStatus,
CreateVectorStoreRequest,
ModifyVectorStoreRequest,
)
from leapfrogai_api.data.crud_vector_store_file import (
CRUDVectorStoreFile,
FilterVectorStoreFile,
)
from leapfrogai_api.data.crud_vector_content import CRUDVectorContent, Vector
# Allows for overwriting type of embeddings that will be instantiated
embeddings_type: type[Embeddings] | type[LeapfrogAIEmbeddings] | None = (
LeapfrogAIEmbeddings
)
class FileAlreadyIndexedError(Exception):
"""Raised when a file is already indexed."""
class IndexingService:
"""Service for indexing files into a vector store."""
def __init__(self, db: AsyncClient):
self.db = db
self.embeddings = embeddings_type()
self.query_name: str = "match_vectors"
self.table_name: str = "vector_content"
async def index_file(self, vector_store_id: str, file_id: str) -> VectorStoreFile:
"""Index a file into a vector store."""
crud_vector_store_file = CRUDVectorStoreFile(db=self.db)
crud_vector_store = CRUDVectorStore(db=self.db)
if await crud_vector_store_file.get(
filters=FilterVectorStoreFile(vector_store_id=vector_store_id, id=file_id)
):
logging.error("File already indexed: %s", file_id)
raise FileAlreadyIndexedError("File already indexed")
if not (
await crud_vector_store.get(filters=FilterVectorStore(id=vector_store_id))
):
logging.error("Vector store doesn't exist: %s", vector_store_id)
raise ValueError("Vector store not found")
crud_file_object = CRUDFileObject(db=self.db)
crud_file_bucket = CRUDFileBucket(db=self.db, model=UploadFile)
file_object = await crud_file_object.get(filters=FilterFileObject(id=file_id))
if not file_object:
raise ValueError("File not found")
file_bytes = await crud_file_bucket.download(id_=file_id)
with tempfile.NamedTemporaryFile(suffix=file_object.filename) as temp_file:
temp_file.write(file_bytes)
temp_file.seek(0)
documents = await load_file(temp_file.name)
chunks = await split(documents)
if len(chunks) == 0:
vector_store_file = VectorStoreFile(
id=file_id,
created_at=0,
last_error=LastError(
message="No text found in file", code="parsing_error"
),
object="vector_store.file",
status=VectorStoreFileStatus.FAILED.value,
usage_bytes=0,
vector_store_id=vector_store_id,
)
return await crud_vector_store_file.create(object_=vector_store_file)
vector_store_file = VectorStoreFile(
id=file_id,
created_at=0,
last_error=None,
object="vector_store.file",
status=VectorStoreFileStatus.IN_PROGRESS.value,
usage_bytes=0,
vector_store_id=vector_store_id,
)
vector_store_file = await crud_vector_store_file.create(
object_=vector_store_file
)
try:
ids = await self.aadd_documents(
documents=chunks,
vector_store_id=vector_store_id,
file_id=file_id,
)
if len(ids) == 0:
vector_store_file.status = VectorStoreFileStatus.FAILED.value
else:
vector_store_file.status = VectorStoreFileStatus.COMPLETED.value
await crud_vector_store_file.update(
id_=vector_store_file.id, object_=vector_store_file
)
except Exception as exc:
vector_store_file.status = VectorStoreFileStatus.FAILED.value
await crud_vector_store_file.update(
id_=vector_store_file.id, object_=vector_store_file
)
raise exc
return await crud_vector_store_file.get(
filters=FilterVectorStoreFile(vector_store_id=vector_store_id, id=file_id)
)
async def index_files(
self, vector_store_id: str, file_ids: list[str]
) -> list[VectorStoreFile]:
"""Index a list of files into a vector store."""
responses = []
for file_id in file_ids:
try:
response = await self.index_file(
vector_store_id=vector_store_id, file_id=file_id
)
responses.append(response)
except FileAlreadyIndexedError:
logging.info("File %s already exists and cannot be re-indexed", file_id)
continue
except Exception as exc:
raise exc
return responses
async def create_new_vector_store(
self, request: CreateVectorStoreRequest
) -> VectorStore:
"""Create a new vector store given a set of file ids"""
crud_vector_store = CRUDVectorStore(db=self.db)
last_active_at = int(time.time())
expires_after, expires_at = request.get_expiry(last_active_at)
try:
vector_store = VectorStore(
id="", # Leave blank to have Postgres generate a UUID
usage_bytes=0, # Automatically calculated by DB
created_at=0, # Leave blank to have Postgres generate a timestamp
file_counts=FileCounts(
cancelled=0, completed=0, failed=0, in_progress=0, total=0
),
last_active_at=last_active_at, # Set to current time
metadata=request.metadata,
name=request.name or "",
object="vector_store",
status=VectorStoreStatus.IN_PROGRESS.value,
expires_after=expires_after,
expires_at=expires_at,
)
new_vector_store = await crud_vector_store.create(object_=vector_store)
if request.file_ids != []:
responses = await self.index_files(
new_vector_store.id, request.file_ids
)
for response in responses:
await self._increment_vector_store_file_status(
new_vector_store, response
)
new_vector_store.status = VectorStoreStatus.COMPLETED.value
return await crud_vector_store.update(
id_=new_vector_store.id,
object_=new_vector_store,
)
except Exception as exc:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Unable to parse vector store request",
) from exc
async def modify_existing_vector_store(
self,
vector_store_id: str,
request: ModifyVectorStoreRequest,
) -> VectorStore:
"""Modify an existing vector store given its id."""
crud_vector_store = CRUDVectorStore(db=self.db)
if not (
old_vector_store := await crud_vector_store.get(
filters=FilterVectorStore(id=vector_store_id)
)
):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Vector store not found",
)
try:
new_vector_store = VectorStore(
id=vector_store_id,
usage_bytes=old_vector_store.usage_bytes, # Automatically calculated by DB
created_at=old_vector_store.created_at,
file_counts=old_vector_store.file_counts,
last_active_at=old_vector_store.last_active_at, # Update after indexing files
metadata=getattr(request, "metadata", old_vector_store.metadata),
name=getattr(request, "name", old_vector_store.name),
object="vector_store",
status=VectorStoreStatus.IN_PROGRESS.value,
expires_after=old_vector_store.expires_after,
expires_at=old_vector_store.expires_at,
)
await crud_vector_store.update(
id_=vector_store_id,
object_=new_vector_store,
) # Sets status to in_progress for the duration of this function
if request.file_ids:
responses = await self.index_files(
new_vector_store.id, request.file_ids
)
for response in responses:
await self._increment_vector_store_file_status(
new_vector_store, response
)
new_vector_store.status = VectorStoreStatus.COMPLETED.value
last_active_at = int(time.time())
new_vector_store.last_active_at = (
last_active_at # Update after indexing files
)
expires_after, expires_at = request.get_expiry(last_active_at)
if expires_at and expires_at:
new_vector_store.expires_after = expires_after
new_vector_store.expires_at = expires_at
return await crud_vector_store.update(
id_=vector_store_id,
object_=new_vector_store,
)
except Exception as exc:
logging.error(exc)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Unable to parse vector store request",
) from exc
async def file_ids_are_valid(self, file_ids: str | list[str]) -> bool:
"""Check if the provided file ids exist"""
crud_file_object = CRUDFileObject(db=self.db)
if not isinstance(file_ids, list):
file_ids = [file_ids]
try:
for file_id in file_ids:
await crud_file_object.get(filters=FilterFileObject(id=file_id))
except Exception:
return False
return True
async def aadd_documents(
self,
documents: list[Document],
vector_store_id: str,
file_id: str,
batch_size: int = 100,
) -> list[str]:
"""Adds documents to the vector store in batches.
Args:
documents (list[Document]): A list of Langchain Document objects to be added.
vector_store_id (str): The ID of the vector store where the documents will be added.
file_id (str): The ID of the file associated with the documents.
batch_size (int): The size of the batches that will
be pushed to the db. This value defaults to 100
as a balance between the memory impact of large files and performance improvements from batching.
Returns:
List[str]: A list of IDs assigned to the added documents.
Raises:
Any exceptions that may occur during the execution of the method.
"""
ids = []
embeddings = await self.embeddings.aembed_documents(
texts=[document.page_content for document in documents]
)
vectors: list[Vector] = []
for document, embedding in zip(documents, embeddings):
vector = Vector(
id="",
vector_store_id=vector_store_id,
file_id=file_id,
content=document.page_content,
metadata=document.metadata,
embedding=embedding,
)
vectors.append(vector)
crud_vector_content = CRUDVectorContent(db=self.db)
for i in range(0, len(vectors), batch_size):
batch = vectors[i : i + batch_size]
response = await crud_vector_content.add_vectors(batch)
ids.extend([item.id for item in response])
return ids
async def asimilarity_search(self, query: str, vector_store_id: str, k: int = 4):
"""Searches for similar documents.
Args:
query (str): The query string.
vector_store_id (str): The ID of the vector store to search in.
k (int, optional): The number of similar documents to retrieve. Defaults to 4.
Returns:
The response from the database after executing the similarity search.
"""
vector = await self.embeddings.aembed_query(query)
crud_vector_content = CRUDVectorContent(db=self.db)
return await crud_vector_content.similarity_search(
query=vector, vector_store_id=vector_store_id, k=k
)
async def _increment_vector_store_file_status(
self, vector_store: VectorStore, file_response: VectorStoreFile
):
"""Increment the file count of a given vector store based on the file response"""
if file_response.status == VectorStoreFileStatus.COMPLETED.value:
vector_store.file_counts.completed += 1
elif file_response.status == VectorStoreFileStatus.FAILED.value:
vector_store.file_counts.failed += 1
elif file_response.status == VectorStoreFileStatus.IN_PROGRESS.value:
vector_store.file_counts.in_progress += 1
elif file_response.status == VectorStoreFileStatus.CANCELLED.value:
vector_store.file_counts.cancelled += 1
vector_store.file_counts.total += 1