Dedoc
此範例示範如何將 Dedoc
與 LangChain
結合使用,作為 DocumentLoader
。
概觀
Dedoc 是一個開放原始碼的函式庫/服務,可以從各種格式的檔案中提取文字、表格、附加檔案和文件結構(例如,標題、列表項目等)。
Dedoc
支援 DOCX
、XLSX
、PPTX
、EML
、HTML
、PDF
、圖片等多種格式。 完整的支援格式列表可以在此處找到。
整合細節
類別 | 套件 | 本地 | 可序列化 | JS 支援 |
---|---|---|---|---|
DedocFileLoader | langchain_community | ❌ | 測試版 | ❌ |
DedocPDFLoader | langchain_community | ❌ | 測試版 | ❌ |
DedocAPIFileLoader | langchain_community | ❌ | 測試版 | ❌ |
載入器功能
提供延遲載入和非同步載入的方法,但實際上,文件載入是同步執行的。
來源 | 文件延遲載入 | 非同步支援 |
---|---|---|
DedocFileLoader | ❌ | ❌ |
DedocPDFLoader | ❌ | ❌ |
DedocAPIFileLoader | ❌ | ❌ |
設定
- 要存取
DedocFileLoader
和DedocPDFLoader
文件載入器,您需要安裝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 參考
- https://langchain-python.dev.org.tw/api_reference/community/document_loaders/langchain_community.document_loaders.dedoc.DedocFileLoader.html
- https://langchain-python.dev.org.tw/api_reference/community/document_loaders/langchain_community.document_loaders.pdf.DedocPDFLoader.html
- https://langchain-python.dev.org.tw/api_reference/community/document_loaders/langchain_community.document_loaders.dedoc.DedocAPIFileLoader.html
載入任何檔案
對於自動處理支援格式的任何檔案,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
(預設值):文件文字會以單一 langchainDocument
物件傳回(不分割);page
:將文件文字分割成頁面(適用於PDF
、DJVU
、PPTX
、PPT
、ODP
);node
:將文件文字分割成Dedoc
樹狀節點(標題節點、列表項目節點、原始文字節點);line
:將文件文字分割成文字行。
loader = DedocFileLoader(
"./example_data/layout-parser-paper.pdf",
split="page",
pages=":2",
)
docs = loader.load()
len(docs)
2
處理表格
當在載入器初始化期間將 with_tables
參數設定為 True
時,DedocFileLoader
支援表格處理(預設情況下 with_tables=True
)。
表格不會被分割 - 每個表格對應一個 langchain Document
物件。對於表格,Document
物件會有額外的 metadata
欄位,包含 type="table"
和 text_as_html
,其中 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"> "Payroll (millions)"</td>\n<td colspan="1" r')
處理附加檔案
當 loader 初始化時,將 with_attachments
設定為 True
(預設為 with_attachments=False
),DedocFileLoader
支援附加檔案的處理。
附加檔案會根據 split
參數進行分割。對於附加檔案,langchain Document
物件會有額外的 metadata 欄位 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
文件,您可以使用僅支援 PDF
的 DedocPDFLoader
。此 loader 支援與文件分割、表格和附加檔案提取相同的參數。
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
函式庫的情況下使用。 此 loader 支援與 DedocFileLoader
相同的參數,並且還可以自動偵測輸入檔案類型。
要使用 DedocAPIFileLoader
,您應該運行 Dedoc
服務,例如 Docker
容器 (請參閱 此文件 以取得更多詳細資訊)
docker pull dedocproject/dedoc
docker run -p 1231:1231
請不要在您的程式碼中使用我們的 demo 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 '