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
|
# type: ignore
import logging
import time
from typing import Any, AsyncGenerator, Optional
from core import parsers
from core.base import (
AsyncParser,
ChunkingStrategy,
Document,
DocumentChunk,
DocumentType,
IngestionConfig,
IngestionProvider,
R2RDocumentProcessingError,
RecursiveCharacterTextSplitter,
TextSplitter,
)
from core.utils import generate_extraction_id
from ...database import PostgresDatabaseProvider
from ...llm import (
LiteLLMCompletionProvider,
OpenAICompletionProvider,
R2RCompletionProvider,
)
logger = logging.getLogger()
class R2RIngestionConfig(IngestionConfig):
chunk_size: int = 1024
chunk_overlap: int = 512
chunking_strategy: ChunkingStrategy = ChunkingStrategy.RECURSIVE
extra_fields: dict[str, Any] = {}
separator: Optional[str] = None
class R2RIngestionProvider(IngestionProvider):
DEFAULT_PARSERS = {
DocumentType.BMP: parsers.BMPParser,
DocumentType.CSV: parsers.CSVParser,
DocumentType.DOC: parsers.DOCParser,
DocumentType.DOCX: parsers.DOCXParser,
DocumentType.EML: parsers.EMLParser,
DocumentType.EPUB: parsers.EPUBParser,
DocumentType.HTML: parsers.HTMLParser,
DocumentType.HTM: parsers.HTMLParser,
DocumentType.ODT: parsers.ODTParser,
DocumentType.JSON: parsers.JSONParser,
DocumentType.MSG: parsers.MSGParser,
DocumentType.ORG: parsers.ORGParser,
DocumentType.MD: parsers.MDParser,
DocumentType.PDF: parsers.BasicPDFParser,
DocumentType.PPT: parsers.PPTParser,
DocumentType.PPTX: parsers.PPTXParser,
DocumentType.TXT: parsers.TextParser,
DocumentType.XLSX: parsers.XLSXParser,
DocumentType.GIF: parsers.ImageParser,
DocumentType.JPEG: parsers.ImageParser,
DocumentType.JPG: parsers.ImageParser,
DocumentType.TSV: parsers.TSVParser,
DocumentType.PNG: parsers.ImageParser,
DocumentType.HEIC: parsers.ImageParser,
DocumentType.SVG: parsers.ImageParser,
DocumentType.MP3: parsers.AudioParser,
DocumentType.P7S: parsers.P7SParser,
DocumentType.RST: parsers.RSTParser,
DocumentType.RTF: parsers.RTFParser,
DocumentType.TIFF: parsers.ImageParser,
DocumentType.XLS: parsers.XLSParser,
}
EXTRA_PARSERS = {
DocumentType.CSV: {"advanced": parsers.CSVParserAdvanced},
DocumentType.PDF: {
"unstructured": parsers.PDFParserUnstructured,
"zerox": parsers.VLMPDFParser,
},
DocumentType.XLSX: {"advanced": parsers.XLSXParserAdvanced},
}
IMAGE_TYPES = {
DocumentType.GIF,
DocumentType.HEIC,
DocumentType.JPG,
DocumentType.JPEG,
DocumentType.PNG,
DocumentType.SVG,
}
def __init__(
self,
config: R2RIngestionConfig,
database_provider: PostgresDatabaseProvider,
llm_provider: (
LiteLLMCompletionProvider
| OpenAICompletionProvider
| R2RCompletionProvider
),
):
super().__init__(config, database_provider, llm_provider)
self.config: R2RIngestionConfig = config
self.database_provider: PostgresDatabaseProvider = database_provider
self.llm_provider: (
LiteLLMCompletionProvider
| OpenAICompletionProvider
| R2RCompletionProvider
) = llm_provider
self.parsers: dict[DocumentType, AsyncParser] = {}
self.text_splitter = self._build_text_splitter()
self._initialize_parsers()
logger.info(
f"R2RIngestionProvider initialized with config: {self.config}"
)
def _initialize_parsers(self):
for doc_type, parser in self.DEFAULT_PARSERS.items():
# will choose the first parser in the list
if doc_type not in self.config.excluded_parsers:
self.parsers[doc_type] = parser(
config=self.config,
database_provider=self.database_provider,
llm_provider=self.llm_provider,
)
for doc_type, doc_parser_name in self.config.extra_parsers.items():
self.parsers[f"{doc_parser_name}_{str(doc_type)}"] = (
R2RIngestionProvider.EXTRA_PARSERS[doc_type][doc_parser_name](
config=self.config,
database_provider=self.database_provider,
llm_provider=self.llm_provider,
)
)
def _build_text_splitter(
self, ingestion_config_override: Optional[dict] = None
) -> TextSplitter:
logger.info(
f"Initializing text splitter with method: {self.config.chunking_strategy}"
)
if not ingestion_config_override:
ingestion_config_override = {}
chunking_strategy = (
ingestion_config_override.get("chunking_strategy")
or self.config.chunking_strategy
)
chunk_size = (
ingestion_config_override.get("chunk_size")
or self.config.chunk_size
)
chunk_overlap = (
ingestion_config_override.get("chunk_overlap")
or self.config.chunk_overlap
)
if chunking_strategy == ChunkingStrategy.RECURSIVE:
return RecursiveCharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
)
elif chunking_strategy == ChunkingStrategy.CHARACTER:
from core.base.utils.splitter.text import CharacterTextSplitter
separator = (
ingestion_config_override.get("separator")
or self.config.separator
or CharacterTextSplitter.DEFAULT_SEPARATOR
)
return CharacterTextSplitter(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
separator=separator,
keep_separator=False,
strip_whitespace=True,
)
elif chunking_strategy == ChunkingStrategy.BASIC:
raise NotImplementedError(
"Basic chunking method not implemented. Please use Recursive."
)
elif chunking_strategy == ChunkingStrategy.BY_TITLE:
raise NotImplementedError("By title method not implemented")
else:
raise ValueError(f"Unsupported method type: {chunking_strategy}")
def validate_config(self) -> bool:
return self.config.chunk_size > 0 and self.config.chunk_overlap >= 0
def chunk(
self,
parsed_document: str | DocumentChunk,
ingestion_config_override: dict,
) -> AsyncGenerator[Any, None]:
text_spliiter = self.text_splitter
if ingestion_config_override:
text_spliiter = self._build_text_splitter(
ingestion_config_override
)
if isinstance(parsed_document, DocumentChunk):
parsed_document = parsed_document.data
if isinstance(parsed_document, str):
chunks = text_spliiter.create_documents([parsed_document])
else:
# Assuming parsed_document is already a list of text chunks
chunks = parsed_document
for chunk in chunks:
yield (
chunk.page_content if hasattr(chunk, "page_content") else chunk
)
async def parse(
self,
file_content: bytes,
document: Document,
ingestion_config_override: dict,
) -> AsyncGenerator[DocumentChunk, None]:
if document.document_type not in self.parsers:
raise R2RDocumentProcessingError(
document_id=document.id,
error_message=f"Parser for {document.document_type} not found in `R2RIngestionProvider`.",
)
else:
t0 = time.time()
contents = []
parser_overrides = ingestion_config_override.get(
"parser_overrides", {}
)
if document.document_type.value in parser_overrides:
logger.info(
f"Using parser_override for {document.document_type} with input value {parser_overrides[document.document_type.value]}"
)
# TODO - Cleanup this approach to be less hardcoded
if (
document.document_type != DocumentType.PDF
or parser_overrides[DocumentType.PDF.value] != "zerox"
):
raise ValueError(
"Only Zerox PDF parser override is available."
)
# Collect content from VLMPDFParser
async for chunk in self.parsers[
f"zerox_{DocumentType.PDF.value}"
].ingest(file_content, **ingestion_config_override):
if isinstance(chunk, dict) and chunk.get("content"):
contents.append(chunk)
elif (
chunk
): # Handle string output for backward compatibility
contents.append({"content": chunk})
if (
contents
and document.document_type == DocumentType.PDF
and parser_overrides.get(DocumentType.PDF.value) == "zerox"
):
text_splitter = self._build_text_splitter(
ingestion_config_override
)
iteration = 0
sorted_contents = [
item
for item in sorted(
contents, key=lambda x: x.get("page_number", 0)
)
if isinstance(item.get("content"), str)
]
for content_item in sorted_contents:
page_num = content_item.get("page_number", 0)
page_content = content_item["content"]
page_chunks = text_splitter.create_documents(
[page_content]
)
# Create document chunks for each split piece
for chunk in page_chunks:
metadata = {
**document.metadata,
"chunk_order": iteration,
"page_number": page_num,
}
extraction = DocumentChunk(
id=generate_extraction_id(
document.id, iteration
),
document_id=document.id,
owner_id=document.owner_id,
collection_ids=document.collection_ids,
data=chunk.page_content,
metadata=metadata,
)
iteration += 1
yield extraction
logger.debug(
f"Parsed document with id={document.id}, title={document.metadata.get('title', None)}, "
f"user_id={document.metadata.get('user_id', None)}, metadata={document.metadata} "
f"into {iteration} extractions in t={time.time() - t0:.2f} seconds using page-by-page splitting."
)
return
else:
# Standard parsing for non-override cases
async for text in self.parsers[document.document_type].ingest(
file_content, **ingestion_config_override
):
if text is not None:
contents.append({"content": text})
if not contents:
logging.warning(
"No valid text content was extracted during parsing"
)
return
iteration = 0
for content_item in contents:
chunk_text = content_item["content"]
chunks = self.chunk(chunk_text, ingestion_config_override)
for chunk in chunks:
metadata = {**document.metadata, "chunk_order": iteration}
if "page_number" in content_item:
metadata["page_number"] = content_item["page_number"]
extraction = DocumentChunk(
id=generate_extraction_id(document.id, iteration),
document_id=document.id,
owner_id=document.owner_id,
collection_ids=document.collection_ids,
data=chunk,
metadata=metadata,
)
iteration += 1
yield extraction
logger.debug(
f"Parsed document with id={document.id}, title={document.metadata.get('title', None)}, "
f"user_id={document.metadata.get('user_id', None)}, metadata={document.metadata} "
f"into {iteration} extractions in t={time.time() - t0:.2f} seconds."
)
def get_parser_for_document_type(self, doc_type: DocumentType) -> Any:
return self.parsers.get(doc_type)
|