forked from SciPhi-AI/R2R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingestion.py
68 lines (56 loc) · 1.93 KB
/
ingestion.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
import uuid
from abc import abstractmethod
from typing import Any, Optional
from ..abstractions.document import BasicDocument
from ..providers.logging import LoggingDatabaseConnection
from .pipeline import Pipeline
class IngestionPipeline(Pipeline):
def __init__(
self,
logging_database: Optional[LoggingDatabaseConnection] = None,
**kwargs,
):
super().__init__(logging_database=logging_database, **kwargs)
def initialize_pipeline(self) -> None:
self.pipeline_run_info = {"run_id": uuid.uuid4(), "type": "ingestion"}
@property
@abstractmethod
def supported_types(self) -> list[str]:
"""
Returns a list of supported data types.
"""
pass
@abstractmethod
def process_data(self, entry_type: str, entry_data: Any) -> str:
"""
Process data into plaintext based on the data type.
"""
pass
@abstractmethod
def parse_entry(self, entry_type: str, entry_data: Any) -> str:
"""
Parse entry data into plaintext based on the entry type.
"""
pass
def run(
self,
document_id: str,
blobs: dict[str, Any],
metadata: Optional[dict] = None,
**kwargs,
) -> BasicDocument:
"""
Run the appropriate parsing method based on the data type and whether the data is a file or an entry.
Returns the processed data and metadata.
"""
self.initialize_pipeline()
if len(blobs) == 0:
raise ValueError("No blobs provided to process.")
processed_text = ""
for entry_type, blob in blobs.items():
if entry_type not in self.supported_types:
raise ValueError(f"EntryType {entry_type} not supported.")
processed_text += self.parse_entry(entry_type, blob)
return BasicDocument(
id=document_id, text=processed_text, metadata=metadata
)