Skip to main content
Open In ColabOpen on GitHub

Dedoc

这示例展示了DedocLangChain结合使用的用法,作为DocumentLoader的一个例子。

概览

Dedoc 是一个 开源 库/服务,用于从各种格式的文件中提取文本、表格、附件文件和文档结构 (例如标题、列表项等)。

Dedoc 支持 DOCX, XLSX, PPTX, EML, HTML, PDF、图像等更多格式。 支持的完整格式列表可在此处找到:此处

集成细节

Class本地序列化JS支持
DedocFileLoaderlangchain_communitybeta
DedocPDFLoaderlangchain_communitybeta
DedocAPIFileLoaderlangchain_communitybeta

加载器功能

懒加载和异步加载的方法可用,但实际上文档加载是同步执行的。

来源文档延迟加载异步支持
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.

Instantiation

from langchain_community.document_loaders import DedocFileLoader

loader = DedocFileLoader("./example_data/state_of_the_union.txt")
API 参考:DedocFileLoader

加载

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]
API 参考:DedocFileLoader
'\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 '

Modes of split

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

处理表格

DedocFileLoader 在加载器初始化时(默认为with_tables=True),将with_tables参数设置为True时支持处理表格。

表格不会被拆分 - 每个表格对应一个langchain Document对象。 对于表格,Document对象具有额外的metadata字段type="table"text_as_html,带有表格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')

处理附件文件

DedocFileLoader 在加载器初始化时(默认为with_attachments=False)将with_attachments设置为True时支持附加文件处理。

根据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>\nMIME-Version\n1.0\nMessage-ID\n<CAPgNNXSzLVJ-d1OCX_TjFgJU7ugtQrjFybPtAMmmYZzphxNFYg@mail.gmail.com>\nSubject\nFake email with attachment\nTo\nMallori Harrell <mallori@unstructured.io>')

加载PDF文件

如果您只想处理仅包含PDF文档的情况,可以使用DedocPDFLoader并提供PDF支持。 加载器支持相同的参数用于文档分割、表格和附件提取。

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]
API 参考:DedocPDFLoader
'\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 '