跳到主要内容
此示例演示了如何将 DedocLangChain 结合用作 DocumentLoader

概览

Dedoc 是一个开源的库/服务,可从各种格式的文件中提取文本、表格、附加文件和文档结构(例如,标题、列表项等)。 Dedoc 支持 DOCXXLSXPPTXEMLHTMLPDF、图像等。支持格式的完整列表可以在这里找到。

集成详情

类别本地可序列化JS 支持
DedocFileLoaderlangchain_community测试版
DedocPDFLoaderlangchain_community测试版
DedocAPIFileLoaderlangchain_community测试版

加载器功能

提供了懒加载和异步加载的方法,但实际上,文档加载是同步执行的。
来源文档延迟加载异步支持
DedocFileLoader
DedocPDFLoader
DedocAPIFileLoader

设置

  • 要访问 DedocFileLoaderDedocPDFLoader 文档加载器,您需要安装 dedoc 集成包。
  • 要访问 DedocAPIFileLoader,您需要运行 Dedoc 服务,例如 Docker 容器(更多详情请参阅文档
docker pull dedocproject/dedoc
docker run -p 1231:1231
Dedoc 的安装说明在这里
# Install package
pip install --quiet "dedoc[torch]"
Note: you may need to restart the kernel to use updated packages.

实例化

from langchain_community.document_loaders import DedocFileLoader

loader = DedocFileLoader("./example_data/state_of_the_union.txt")

加载

docs = loader.load()
docs[0].page_content[:100]
'\nMadam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and t'

延迟加载

docs = loader.lazy_load()

for doc in docs:
    print(doc.page_content[:100])
    break
Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and t

API 参考

有关配置和调用 Dedoc 加载器的详细信息,请参阅 API 参考

加载任何文件

要自动处理支持格式的任何文件,DedocFileLoader 会很有用。该文件加载器会自动检测具有正确扩展名的文件类型。 文件解析过程可以在 DedocFileLoader 类初始化期间通过 dedoc_kwargs进行配置。这里给出了部分选项用法的基本示例,有关配置参数的更多详细信息,请参阅 DedocFileLoader 的文档和dedoc 文档

基本示例

from langchain_community.document_loaders import DedocFileLoader

loader = DedocFileLoader("./example_data/state_of_the_union.txt")

docs = loader.load()

docs[0].page_content[:400]
'\nMadam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.  \n\n\n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\n\n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\n\n\nWith a duty to one another to the American people to '

分割模式

DedocFileLoader 支持将文档分割成不同部分(每个部分单独返回)的多种类型。为此,split 参数与以下选项一起使用
  • document (默认值):文档文本作为单个 langchain Document 对象返回(不分割);
  • page:将文档文本分割成页面(适用于 PDFDJVUPPTXPPTODP);
  • node:将文档文本分割成 Dedoc 树节点(标题节点、列表项节点、纯文本节点);
  • line:将文档文本分割成文本行。
loader = DedocFileLoader(
    "./example_data/layout-parser-paper.pdf",
    split="page",
    pages=":2",
)

docs = loader.load()

len(docs)
2

处理表格

当在加载器初始化期间将 with_tables 参数设置为 True 时(默认为 with_tables=True),DedocFileLoader 支持处理表格。 表格不会被分割——每个表格对应一个 langchain Document 对象。对于表格,Document 对象具有额外的 metadata 字段 type="table" 和带有表格 HTML 表示的 text_as_html
loader = DedocFileLoader("./example_data/mlb_teams_2012.csv")

docs = loader.load()

docs[1].metadata["type"], docs[1].metadata["text_as_html"][:200]
('table',
 '<table border="1" style="border-collapse: collapse; width: 100%;">\n<tbody>\n<tr>\n<td colspan="1" rowspan="1">Team</td>\n<td colspan="1" rowspan="1"> &quot;Payroll (millions)&quot;</td>\n<td colspan="1" r')

处理附加文件

当在加载器初始化期间将 with_attachments 设置为 True 时(默认为 with_attachments=False),DedocFileLoader 支持处理附加文件。 附件会根据 split 参数进行分割。对于附件,langchain Document 对象有一个额外的元数据字段 type="attachment"
loader = DedocFileLoader(
    "./example_data/fake-email-attachment.eml",
    with_attachments=True,
)

docs = loader.load()

docs[1].metadata["type"], docs[1].page_content
('attachment',
 '\nContent-Type\nmultipart/mixed; boundary="0000000000005d654405f082adb7"\nDate\nFri, 23 Dec 2022 12:08:48 -0600\nFrom\nMallori Harrell [mallori@unstructured.io](mailto:mallori@unstructured.io)\nMIME-Version\n1.0\nMessage-ID\n[CAPgNNXSzLVJ-d1OCX_TjFgJU7ugtQrjFybPtAMmmYZzphxNFYg@mail.gmail.com](mailto:CAPgNNXSzLVJ-d1OCX_TjFgJU7ugtQrjFybPtAMmmYZzphxNFYg@mail.gmail.com)\nSubject\nFake email with attachment\nTo\nMallori Harrell [mallori@unstructured.io](mailto:mallori@unstructured.io)')

加载 PDF 文件

如果您只想处理 PDF 文档,可以使用仅支持 PDFDedocPDFLoader。该加载器支持用于文档分割、表格和附件提取的相同参数。 Dedoc 可以提取带或不带文本层的 PDF,并能自动检测其存在和正确性。有几种 PDF 处理程序可用,您可以使用 pdf_with_text_layer 参数选择其中之一。请参阅参数说明以获取更多详细信息。 对于没有文本层的 PDF,应安装 Tesseract OCR 及其语言包。在这种情况下,此说明可能会有用。
from langchain_community.document_loaders import DedocPDFLoader

loader = DedocPDFLoader(
    "./example_data/layout-parser-paper.pdf", pdf_with_text_layer="true", pages="2:2"
)

docs = loader.load()

docs[0].page_content[:400]
'\n2\n\nZ. Shen et al.\n\n37], layout detection [38, 22], table detection [26], and scene text detection [4].\n\nA generalized learning-based framework dramatically reduces the need for the\n\nmanual specification of complicated rules, which is the status quo with traditional\n\nmethods. DL has the potential to transform DIA pipelines and benefit a broad\n\nspectrum of large-scale document digitization projects.\n'

Dedoc API

如果您想以更少的设置快速启动和运行,可以将 Dedoc 作为服务使用。DedocAPIFileLoader 无需安装 dedoc 库即可使用。 该加载器支持与 DedocFileLoader 相同的参数,并能自动检测输入文件类型。 要使用 DedocAPIFileLoader,您应该运行 Dedoc 服务,例如 Docker 容器(请参阅文档以获取更多详细信息):
docker pull dedocproject/dedoc
docker run -p 1231:1231
请不要在您的代码中使用我们的演示 URL https://dedoc-readme.hf.space
from langchain_community.document_loaders import DedocAPIFileLoader

loader = DedocAPIFileLoader(
    "./example_data/state_of_the_union.txt",
    url="https://dedoc-readme.hf.space",
)

docs = loader.load()

docs[0].page_content[:400]
'\nMadam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans.  \n\n\n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\n\n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\n\n\nWith a duty to one another to the American people to '

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.