-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathloader.py
More file actions
171 lines (147 loc) · 6.11 KB
/
Copy pathloader.py
File metadata and controls
171 lines (147 loc) · 6.11 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
#
# Copyright IBM Corp. 2025 - 2025
# SPDX-License-Identifier: MIT
#
"""Docling LangChain loader module."""
from abc import ABC, abstractmethod
from enum import Enum
from typing import Any, Dict, Iterable, Iterator, Optional, Union
from docling.chunking import BaseChunk, BaseChunker, HybridChunker
from docling.datamodel.document import DoclingDocument
from docling.document_converter import DocumentConverter
from langchain_core.document_loaders import BaseBlobParser, BaseLoader, Blob
from langchain_core.documents import Document
class ExportType(str, Enum):
"""Enumeration of available export types."""
MARKDOWN = "markdown"
DOC_CHUNKS = "doc_chunks"
class BaseMetaExtractor(ABC):
"""BaseMetaExtractor."""
@abstractmethod
def extract_chunk_meta(self, file_path: str, chunk: BaseChunk) -> dict[str, Any]:
"""Extract chunk meta."""
raise NotImplementedError()
@abstractmethod
def extract_dl_doc_meta(
self, file_path: str, dl_doc: DoclingDocument
) -> dict[str, Any]:
"""Extract Docling document meta."""
raise NotImplementedError()
class MetaExtractor(BaseMetaExtractor):
"""MetaExtractor."""
def extract_chunk_meta(self, file_path: str, chunk: BaseChunk) -> dict[str, Any]:
"""Extract chunk meta."""
return {
"source": file_path,
"dl_meta": chunk.meta.export_json_dict(),
}
def extract_dl_doc_meta(
self, file_path: str, dl_doc: DoclingDocument
) -> dict[str, Any]:
"""Extract Docling document meta."""
return {"source": file_path}
class DoclingParser(BaseBlobParser):
"""Docling Parser."""
def __init__(
self,
*,
converter: Optional[DocumentConverter] = None,
convert_kwargs: Optional[Dict[str, Any]] = None,
export_type: ExportType = ExportType.DOC_CHUNKS,
md_export_kwargs: Optional[dict[str, Any]] = None,
chunker: Optional[BaseChunker] = None,
meta_extractor: Optional[BaseMetaExtractor] = None,
):
"""Initialize DoclingParser."""
self._converter: DocumentConverter = converter or DocumentConverter()
self._convert_kwargs = convert_kwargs if convert_kwargs is not None else {}
self._export_type = export_type
self._md_export_kwargs = (
md_export_kwargs
if md_export_kwargs is not None
else {"image_placeholder": ""}
)
if self._export_type == ExportType.DOC_CHUNKS:
self._chunker: BaseChunker = chunker or HybridChunker()
self._meta_extractor = meta_extractor or MetaExtractor()
def lazy_parse(self, blob: Blob) -> Iterator[Document]:
"""Lazy parse blob into documents."""
file_path = str(blob.source) if blob.source else str(blob.path)
conv_res = self._converter.convert(
source=file_path,
**self._convert_kwargs,
)
dl_doc = conv_res.document
if self._export_type == ExportType.MARKDOWN:
yield Document(
page_content=dl_doc.export_to_markdown(**self._md_export_kwargs),
metadata=self._meta_extractor.extract_dl_doc_meta(
file_path=file_path,
dl_doc=dl_doc,
),
)
elif self._export_type == ExportType.DOC_CHUNKS:
chunk_iter = self._chunker.chunk(dl_doc)
for chunk in chunk_iter:
yield Document(
page_content=self._chunker.contextualize(chunk=chunk),
metadata=self._meta_extractor.extract_chunk_meta(
file_path=file_path,
chunk=chunk,
),
)
else:
raise ValueError(f"Unexpected export type: {self._export_type}")
class DoclingLoader(BaseLoader):
"""Docling Loader."""
def __init__(
self,
file_path: Union[str, Iterable[str]],
*,
converter: Optional[DocumentConverter] = None,
convert_kwargs: Optional[Dict[str, Any]] = None,
export_type: ExportType = ExportType.DOC_CHUNKS,
md_export_kwargs: Optional[dict[str, Any]] = None,
chunker: Optional[BaseChunker] = None,
meta_extractor: Optional[BaseMetaExtractor] = None,
):
"""Initialize with a file path.
Args:
file_path: File source as single str (URL or local file) or Iterable
thereof.
converter: Any specific `DocumentConverter` to use. Defaults to `None` (i.e.
converter defined internally).
convert_kwargs: Any specific kwargs to pass to conversion invocation.
Defaults to `None` (i.e. behavior defined internally).
export_type: The type to export to: either `ExportType.MARKDOWN` (outputs
Markdown of whole input file) or `ExportType.DOC_CHUNKS` (outputs chunks
based on chunker).
md_export_kwargs: Any specific kwargs to pass to Markdown export (in case of
`ExportType.MARKDOWN`). Defaults to `None` (i.e. behavior defined
internally).
chunker: Any specific `BaseChunker` to use (in case of
`ExportType.DOC_CHUNKS`). Defaults to `None` (i.e. chunker defined
internally).
meta_extractor: The extractor instance to use for populating the output
document metadata; if not set, a system default is used.
"""
self._file_paths = (
file_path
if isinstance(file_path, Iterable) and not isinstance(file_path, str)
else [file_path]
)
self._parser = DoclingParser(
converter=converter,
convert_kwargs=convert_kwargs,
export_type=export_type,
md_export_kwargs=md_export_kwargs,
chunker=chunker,
meta_extractor=meta_extractor,
)
def lazy_load(
self,
) -> Iterator[Document]:
"""Lazy load documents."""
for file_path in self._file_paths:
blob = Blob(path=file_path)
yield from self._parser.lazy_parse(blob)