Skip to content

Docugami

DocugamiReader #

Bases: BaseReader

Docugami阅读器。

从Docugami中将文档读取为文档XML知识图中的节点。

Source code in llama_index/readers/docugami/base.py
 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
class DocugamiReader(BaseReader):
    """Docugami阅读器。

从Docugami中将文档读取为文档XML知识图中的节点。"""

    api: str = DEFAULT_API_ENDPOINT
    """Docugami API端点的使用。"""

    access_token: Optional[str] = os.environ.get("DOCUGAMI_API_KEY")
    """Docugami API访问令牌。"""

    max_text_length = 4096
    """返回的文本块的最大长度。"""

    min_text_length: int = 32
    """阈值,低于该值时,将块追加到下一个以避免过度分块。"""

    max_metadata_length = 512
    """返回的元数据文本的最大长度。"""

    include_xml_tags: bool = False
    """设置为True以在块输出文本中使用XML标记。"""

    parent_hierarchy_levels: int = 0
    """根据块层次结构适当设置以获取父块。"""

    parent_id_key: str = "doc_id"
    """父文档ID的元数据键。"""

    sub_chunk_tables: bool = False
    """设置为True以返回表格内的子块。"""

    whitespace_normalize_text: bool = True
    """如果您希望在原始XML文档中进行完整的空格格式化,包括缩进,请将其设置为False。"""

    docset_id: Optional[str]
    """要使用的Docugami API文档集ID。"""

    document_ids: Optional[Sequence[str]]
    """Docugami API文档中要使用的文档ID。"""

    file_paths: Optional[Sequence[Union[Path, str]]]
    """要使用的本地文件路径。"""

    include_project_metadata_in_doc_metadata: bool = True
    """如果您希望在文档元数据中包含项目元数据,则设置为True。"""

    def __init__(
        self,
        api: str = DEFAULT_API_ENDPOINT,
        access_token: Optional[str] = os.environ.get("DOCUGAMI_API_KEY"),
        max_text_length=4096,
        min_text_length: int = 32,
        max_metadata_length=512,
        include_xml_tags: bool = False,
        parent_hierarchy_levels: int = 0,
        parent_id_key: str = "doc_id",
        sub_chunk_tables: bool = False,
        whitespace_normalize_text: bool = True,
        docset_id: Optional[str] = None,
        document_ids: Optional[Sequence[str]] = None,
        file_paths: Optional[Sequence[Union[Path, str]]] = None,
        include_project_metadata_in_doc_metadata: bool = True,
    ):
        self.api = api
        self.access_token = access_token
        self.max_text_length = max_text_length
        self.min_text_length = min_text_length
        self.max_metadata_length = max_metadata_length
        self.include_xml_tags = include_xml_tags
        self.parent_hierarchy_levels = parent_hierarchy_levels
        self.parent_id_key = parent_id_key
        self.sub_chunk_tables = sub_chunk_tables
        self.whitespace_normalize_text = whitespace_normalize_text
        self.docset_id = docset_id
        self.document_ids = document_ids
        self.file_paths = file_paths
        self.include_project_metadata_in_doc_metadata = (
            include_project_metadata_in_doc_metadata
        )

    def _parse_dgml(
        self,
        content: bytes,
        document_name: Optional[str] = None,
        additional_doc_metadata: Optional[Mapping] = None,
    ) -> List[Document]:
        """将单个DGML文档解析为文档列表。"""
        try:
            from lxml import etree
        except ImportError:
            raise ImportError(
                "Could not import lxml python package. "
                "Please install it with `pip install lxml`."
            )

        # helpers
        def _xpath_qname_for_chunk(chunk: Any) -> str:
            """获取块的xpath qname。"""
            qname = f"{chunk.prefix}:{chunk.tag.split('}')[-1]}"

            parent = chunk.getparent()
            if parent is not None:
                doppelgangers = [x for x in parent if x.tag == chunk.tag]
                if len(doppelgangers) > 1:
                    idx_of_self = doppelgangers.index(chunk)
                    qname = f"{qname}[{idx_of_self + 1}]"

            return qname

        def _xpath_for_chunk(chunk: Any) -> str:
            """获取一个块的xpath。"""
            ancestor_chain = chunk.xpath("ancestor-or-self::*")
            return "/" + "/".join(_xpath_qname_for_chunk(x) for x in ancestor_chain)

        def _structure_value(node: Any) -> Optional[str]:
            """获取节点的结构值。"""
            return (
                "table"
                if node.tag == TABLE_NAME
                else node.attrib["structure"]
                if "structure" in node.attrib
                else None
            )

        def _build_framework_chunk(dg_chunk: Chunk) -> Document:
            # Stable IDs for chunks with the same text.
            _hashed_id = hashlib.md5(dg_chunk.text.encode()).hexdigest()
            metadata = {
                XPATH_KEY: dg_chunk.xpath,
                ID_KEY: _hashed_id,
                DOCUMENT_NAME_KEY: document_name,
                STRUCTURE_KEY: dg_chunk.structure,
                TAG_KEY: dg_chunk.tag,
            }

            text = dg_chunk.text
            if additional_doc_metadata:
                if self.include_project_metadata_in_doc_metadata:
                    metadata.update(additional_doc_metadata)

            return Document(
                text=text[: self.max_text_length],
                metadata=metadata,
                excluded_llm_metadata_keys=[XPATH_KEY, ID_KEY, STRUCTURE_KEY],
            )

        # Parse the tree and return chunks
        tree = etree.parse(io.BytesIO(content))
        root = tree.getroot()

        dg_chunks = get_chunks(
            root,
            min_text_length=self.min_text_length,
            max_text_length=self.max_text_length,
            whitespace_normalize_text=self.whitespace_normalize_text,
            sub_chunk_tables=self.sub_chunk_tables,
            include_xml_tags=self.include_xml_tags,
            parent_hierarchy_levels=self.parent_hierarchy_levels,
        )

        framework_chunks: Dict[str, Document] = {}
        for dg_chunk in dg_chunks:
            framework_chunk = _build_framework_chunk(dg_chunk)
            chunk_id = framework_chunk.metadata.get(ID_KEY)
            if chunk_id:
                framework_chunks[chunk_id] = framework_chunk
                if dg_chunk.parent:
                    framework_parent_chunk = _build_framework_chunk(dg_chunk.parent)
                    parent_id = framework_parent_chunk.metadata.get(ID_KEY)
                    if parent_id and framework_parent_chunk.text:
                        framework_chunk.metadata[self.parent_id_key] = parent_id
                        framework_chunks[parent_id] = framework_parent_chunk

        return list(framework_chunks.values())

    def _document_details_for_docset_id(self, docset_id: str) -> List[Dict]:
        """获取给定文档集ID的所有文档详情。"""
        url = f"{self.api}/docsets/{docset_id}/documents"
        all_documents = []

        while url:
            response = requests.get(
                url,
                headers={"Authorization": f"Bearer {self.access_token}"},
            )
            if response.ok:
                data = response.json()
                all_documents.extend(data["documents"])
                url = data.get("next", None)
            else:
                raise Exception(
                    f"Failed to download {url} (status: {response.status_code})"
                )

        return all_documents

    def _project_details_for_docset_id(self, docset_id: str) -> List[Dict]:
        """获取给定文档集ID的所有项目详情。"""
        url = f"{self.api}/projects?docset.id={docset_id}"
        all_projects = []

        while url:
            response = requests.request(
                "GET",
                url,
                headers={"Authorization": f"Bearer {self.access_token}"},
                data={},
            )
            if response.ok:
                data = response.json()
                all_projects.extend(data["projects"])
                url = data.get("next", None)
            else:
                raise Exception(
                    f"Failed to download {url} (status: {response.status_code})"
                )

        return all_projects

    def _metadata_for_project(self, project: Dict) -> Dict:
        """获取所有文件的项目元数据。"""
        project_id = project.get(ID_KEY)

        url = f"{self.api}/projects/{project_id}/artifacts/latest"
        all_artifacts = []

        per_file_metadata: Dict = {}
        while url:
            response = requests.request(
                "GET",
                url,
                headers={"Authorization": f"Bearer {self.access_token}"},
                data={},
            )
            if response.ok:
                data = response.json()
                all_artifacts.extend(data["artifacts"])
                url = data.get("next", None)
            elif response.status_code == 404:
                # Not found is ok, just means no published projects
                return per_file_metadata
            else:
                raise Exception(
                    f"Failed to download {url} (status: {response.status_code})"
                )

        for artifact in all_artifacts:
            artifact_name = artifact.get("name")
            artifact_url = artifact.get("url")
            artifact_doc = artifact.get("document")

            if artifact_name == "report-values.xml" and artifact_url and artifact_doc:
                doc_id = artifact_doc[ID_KEY]
                metadata: Dict = {}

                # The evaluated XML for each document is named after the project
                response = requests.request(
                    "GET",
                    f"{artifact_url}/content",
                    headers={"Authorization": f"Bearer {self.access_token}"},
                    data={},
                )

                if response.ok:
                    try:
                        from lxml import etree
                    except ImportError:
                        raise ImportError(
                            "Could not import lxml python package. "
                            "Please install it with `pip install lxml`."
                        )
                    artifact_tree = etree.parse(io.BytesIO(response.content))
                    artifact_root = artifact_tree.getroot()
                    ns = artifact_root.nsmap
                    entries = artifact_root.xpath("//pr:Entry", namespaces=ns)
                    for entry in entries:
                        heading = entry.xpath("./pr:Heading", namespaces=ns)[0].text
                        value = " ".join(
                            entry.xpath("./pr:Value", namespaces=ns)[0].itertext()
                        ).strip()
                        metadata[heading] = value[: self.max_metadata_length]
                    per_file_metadata[doc_id] = metadata
                else:
                    raise Exception(
                        f"Failed to download {artifact_url}/content "
                        + "(status: {response.status_code})"
                    )

        return per_file_metadata

    def _load_chunks_for_document(
        self,
        document_id: str,
        docset_id: str,
        document_name: Optional[str] = None,
        additional_metadata: Optional[Mapping] = None,
    ) -> List[Document]:
        """为文档加载块。"""
        url = f"{self.api}/docsets/{docset_id}/documents/{document_id}/dgml"

        response = requests.request(
            "GET",
            url,
            headers={"Authorization": f"Bearer {self.access_token}"},
            data={},
        )

        if response.ok:
            return self._parse_dgml(
                content=response.content,
                document_name=document_name,
                additional_doc_metadata=additional_metadata,
            )
        else:
            raise Exception(
                f"Failed to download {url} (status: {response.status_code})"
            )

    def load_data(
        self,
        docset_id: str,
        document_ids: Optional[List[str]] = None,
        access_token: Optional[str] = None,
    ) -> List[Document]:
        """加载Docugami中给定docset_id的数据。

Args:
    docset_id (str): 要加载数据的文档集ID。
    document_ids (Optional[List[str]]): 要加载数据的可选文档ID列表。
                            如果未指定,将加载docset_id中的所有文档。
"""
        chunks: List[Document] = []

        if access_token:
            self.access_token = access_token

        if not self.access_token:
            raise Exception(
                "Please specify access token as argument or set the DOCUGAMI_API_KEY"
                " env var."
            )

        _document_details = self._document_details_for_docset_id(docset_id)
        if document_ids:
            _document_details = [
                d for d in _document_details if d[ID_KEY] in document_ids
            ]

        _project_details = self._project_details_for_docset_id(docset_id)
        combined_project_metadata: Dict[str, Dict] = {}
        if _project_details and self.include_project_metadata_in_doc_metadata:
            # If there are any projects for this docset and the caller requested
            # project metadata, load it.
            for project in _project_details:
                metadata = self._metadata_for_project(project)
                for file_id in metadata:
                    if file_id not in combined_project_metadata:
                        combined_project_metadata[file_id] = metadata[file_id]
                    else:
                        combined_project_metadata[file_id].update(metadata[file_id])

        for doc in _document_details:
            doc_id = doc[ID_KEY]
            doc_name = doc.get(DOCUMENT_NAME_KEY)
            doc_metadata = combined_project_metadata.get(doc_id)
            chunks += self._load_chunks_for_document(
                document_id=doc_id,
                docset_id=docset_id,
                document_name=doc_name,
                additional_metadata=doc_metadata,
            )

        return chunks

api class-attribute instance-attribute #

api: str = api

Docugami API端点的使用。

access_token class-attribute instance-attribute #

access_token: Optional[str] = access_token

Docugami API访问令牌。

max_text_length class-attribute instance-attribute #

max_text_length = max_text_length

返回的文本块的最大长度。

min_text_length class-attribute instance-attribute #

min_text_length: int = min_text_length

阈值,低于该值时,将块追加到下一个以避免过度分块。

max_metadata_length class-attribute instance-attribute #

max_metadata_length = max_metadata_length

返回的元数据文本的最大长度。

include_xml_tags class-attribute instance-attribute #

include_xml_tags: bool = include_xml_tags

设置为True以在块输出文本中使用XML标记。

parent_hierarchy_levels class-attribute instance-attribute #

parent_hierarchy_levels: int = parent_hierarchy_levels

根据块层次结构适当设置以获取父块。

parent_id_key class-attribute instance-attribute #

parent_id_key: str = parent_id_key

父文档ID的元数据键。

sub_chunk_tables class-attribute instance-attribute #

sub_chunk_tables: bool = sub_chunk_tables

设置为True以返回表格内的子块。

whitespace_normalize_text class-attribute instance-attribute #

whitespace_normalize_text: bool = whitespace_normalize_text

如果您希望在原始XML文档中进行完整的空格格式化,包括缩进,请将其设置为False。

docset_id instance-attribute #

docset_id: Optional[str] = docset_id

要使用的Docugami API文档集ID。

document_ids instance-attribute #

document_ids: Optional[Sequence[str]] = document_ids

Docugami API文档中要使用的文档ID。

file_paths instance-attribute #

file_paths: Optional[Sequence[Union[Path, str]]] = (
    file_paths
)

要使用的本地文件路径。

include_project_metadata_in_doc_metadata class-attribute instance-attribute #

include_project_metadata_in_doc_metadata: bool = (
    include_project_metadata_in_doc_metadata
)

如果您希望在文档元数据中包含项目元数据,则设置为True。

load_data #

load_data(
    docset_id: str,
    document_ids: Optional[List[str]] = None,
    access_token: Optional[str] = None,
) -> List[Document]

加载Docugami中给定docset_id的数据。

Parameters:

Name Type Description Default
docset_id str

要加载数据的文档集ID。

required
document_ids Optional[List[str]]

要加载数据的可选文档ID列表。 如果未指定,将加载docset_id中的所有文档。

None
Source code in llama_index/readers/docugami/base.py
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
    def load_data(
        self,
        docset_id: str,
        document_ids: Optional[List[str]] = None,
        access_token: Optional[str] = None,
    ) -> List[Document]:
        """加载Docugami中给定docset_id的数据。

Args:
    docset_id (str): 要加载数据的文档集ID。
    document_ids (Optional[List[str]]): 要加载数据的可选文档ID列表。
                            如果未指定,将加载docset_id中的所有文档。
"""
        chunks: List[Document] = []

        if access_token:
            self.access_token = access_token

        if not self.access_token:
            raise Exception(
                "Please specify access token as argument or set the DOCUGAMI_API_KEY"
                " env var."
            )

        _document_details = self._document_details_for_docset_id(docset_id)
        if document_ids:
            _document_details = [
                d for d in _document_details if d[ID_KEY] in document_ids
            ]

        _project_details = self._project_details_for_docset_id(docset_id)
        combined_project_metadata: Dict[str, Dict] = {}
        if _project_details and self.include_project_metadata_in_doc_metadata:
            # If there are any projects for this docset and the caller requested
            # project metadata, load it.
            for project in _project_details:
                metadata = self._metadata_for_project(project)
                for file_id in metadata:
                    if file_id not in combined_project_metadata:
                        combined_project_metadata[file_id] = metadata[file_id]
                    else:
                        combined_project_metadata[file_id].update(metadata[file_id])

        for doc in _document_details:
            doc_id = doc[ID_KEY]
            doc_name = doc.get(DOCUMENT_NAME_KEY)
            doc_metadata = combined_project_metadata.get(doc_id)
            chunks += self._load_chunks_for_document(
                document_id=doc_id,
                docset_id=docset_id,
                document_name=doc_name,
                additional_metadata=doc_metadata,
            )

        return chunks