-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
177 lines (146 loc) · 5.87 KB
/
__init__.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
from embeddings import EmbedProviders
from rags import RAGProviders, BaseRAG
from datachecks import RAGConfig, RAGTask, RAGTaskStatus
import asyncio
from threading import Thread
from utils import configure_logger
from typing import Dict
from uuid import uuid4
import tempfile
import os
logger = configure_logger(__name__)
class RAG:
"""
Retrieval-Augmented Generation (RAG) implementation.
This class handles the ingestion and storage of documents for RAG.
"""
def __init__(self, VectorDB: BaseRAG, Workers: int = 2) -> None:
"""
Initialize the RAG instance.
Args:
VectorDB (BaseRAG): The vector database to use.
Workers (int, optional): Number of worker threads. Defaults to 2.
"""
self.file_process_task_queue: asyncio.Queue = asyncio.Queue()
self.file_store_task_queue: asyncio.Queue = asyncio.Queue()
self.VectorDB: BaseRAG = VectorDB
self.Workers: int = Workers
self.RAG_THREAD = Thread(target=self.start)
self.shutdown = False
async def __shutdown_loop(self):
"""Monitor the shutdown flag."""
while not self.shutdown:
await asyncio.sleep(0.5)
async def __ingestion_task(self):
"""Process ingestion tasks from the queue."""
while not self.shutdown:
task: RAGTask = await self.file_process_task_queue.get()
task._status = RAGTaskStatus.PROCESSING
try:
nodes = await self.VectorDB.generate_nodes_sentence_splitter(task.file_loc)
except Exception as e:
logger.error(f"ERROR in {e}")
task._status = RAGTaskStatus.ERROR
continue
task._nodes = nodes
await self.file_store_task_queue.put(task)
async def __nodes_storage(self):
"""Store processed nodes in the vector database."""
while not self.shutdown:
task: RAGTask = await self.file_store_task_queue.get()
try:
index = await self.VectorDB.add_index(task._nodes)
except Exception as e:
logger.error(f"ERROR in {e}")
task._status = RAGTaskStatus.ERROR
continue
task._index = index
task._status = RAGTaskStatus.SUCESSFUL
def start(self):
"""Start the RAG processing loop."""
loop = asyncio.new_event_loop()
ingestion_task_pool = [loop.create_task(self.__ingestion_task()) for _ in range(self.Workers)]
file_storage = loop.create_task(self.__nodes_storage())
loop.run_until_complete(self.__shutdown_loop())
file_storage.cancel()
for t in ingestion_task_pool:
t.cancel()
loop.close()
class RAGFactory:
"""
Factory class for creating and managing RAG instances.
"""
def __init__(self) -> None:
"""Initialize the RAGFactory."""
self.RAGS: Dict[str, RAG] = dict()
def make_rag(self, config: RAGConfig):
"""
Create a new RAG instance.
Args:
config (RAGConfig): Configuration for the RAG instance.
Returns:
str: Unique identifier for the created RAG instance.
"""
rag_name = f"RAG-{uuid4()}"
embedding_name = EmbedProviders[config.provider_config.embedding_name.provider](config.provider_config.embedding_name.embedding_model_name)
vector_db = RAGProviders[config.provider](embedding_name, config.provider_config)
rag = RAG(vector_db, config.provider_config.worker)
rag.RAG_THREAD.start()
self.RAGS[rag_name] = rag
return rag_name
def stop_all(self):
"""Stop all RAG instances."""
for rag in self.RAGS.values():
rag.shutdown = True
def stop(self, rag_name: str):
"""
Stop a specific RAG instance.
Args:
rag_name (str): Identifier of the RAG instance to stop.
Raises:
ValueError: If the specified RAG instance doesn't exist.
"""
if rag_name in self.RAGS.keys():
self.RAGS[rag_name].shutdown = True
self.RAGS.pop(rag_name)
else:
raise ValueError("No RAG with that ID exists")
async def file_ingest(self, rag_name, file) -> RAGTask:
"""
Ingest a file into a RAG instance.
Args:
rag_name (str): Identifier of the RAG instance.
file: File object to ingest.
Returns:
RAGTask: Task object representing the ingestion process.
Raises:
ValueError: If the specified RAG instance doesn't exist or if the file type is unsupported.
"""
if rag_name not in self.RAGS.keys():
raise ValueError(f"RAG: {rag_name} does not exist")
if file.content_type not in ["application/pdf", "application/x-pdf"]:
raise ValueError("Only PDF files are supported for now")
task_id = str(uuid4())
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(await file.read())
prev = temp_file.name
file_name = f"/tmp/{task_id}.pdf"
os.rename(prev, file_name)
task = RAGTask(file_loc=file_name)
await self.RAGS[rag_name].file_process_task_queue.put(task)
while task._status in [RAGTaskStatus.WAIT, RAGTaskStatus.PROCESSING]:
await asyncio.sleep(0.4)
os.rename(file_name, prev)
return task
async def retrieve_query(self, rag_name: str, index: str, query: str):
"""
Retrieve documents based on a query.
Args:
rag_name (str): Identifier of the RAG instance.
index (str): Index to search in.
query (str): Query string.
Returns:
List of relevant documents.
"""
rag = self.RAGS[rag_name]
return await rag.VectorDB.get_docs_index(query=query, index=index)