이 샘플은 DedocLangChain과 함께 DocumentLoader로 사용하는 방법을 보여줍니다.

Overview

Dedoc은 다양한 형식의 파일에서 텍스트, 표, 첨부 파일 및 문서 구조(예: 제목, 목록 항목 등)를 추출하는 오픈 소스 라이브러리/서비스입니다. DedocDOCX, XLSX, PPTX, EML, HTML, PDF, 이미지 등을 지원합니다. 지원되는 형식의 전체 목록은 여기에서 확인할 수 있습니다.

Integration details

ClassPackageLocalSerializableJS support
DedocFileLoaderlangchain_communitybeta
DedocPDFLoaderlangchain_communitybeta
DedocAPIFileLoaderlangchain_communitybeta

Loader features

lazy loading 및 async loading 메서드를 사용할 수 있지만, 실제로 문서 로딩은 동기적으로 실행됩니다.
SourceDocument Lazy LoadingAsync Support
DedocFileLoader
DedocPDFLoader
DedocAPIFileLoader

Setup

  • DedocFileLoaderDedocPDFLoader document loader에 액세스하려면 dedoc integration package를 설치해야 합니다.
  • 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")

Load

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

Lazy Load

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 reference

Dedoc loader 구성 및 호출에 대한 자세한 정보는 API reference를 참조하세요:

Loading any file

지원되는 형식의 모든 파일을 자동으로 처리하려면 DedocFileLoader를 사용할 수 있습니다. file loader는 올바른 확장자를 가진 파일 유형을 자동으로 감지합니다. 파일 파싱 프로세스는 DedocFileLoader 클래스 초기화 시 dedoc_kwargs를 통해 구성할 수 있습니다. 여기서는 일부 옵션 사용의 기본 예제가 제공되며, 구성 매개변수에 대한 자세한 내용은 DedocFileLoader 문서 및 dedoc 문서를 참조하세요.

Basic example

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 '

Modes of split

DedocFileLoader는 문서를 부분으로 분할하는 다양한 유형을 지원합니다(각 부분은 별도로 반환됩니다). 이를 위해 다음 옵션과 함께 split 매개변수가 사용됩니다:
  • document (기본값): 문서 텍스트가 단일 langchain Document 객체로 반환됩니다 (분할하지 않음);
  • 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

Handling tables

DedocFileLoader는 loader 초기화 시 with_tables 매개변수가 True로 설정된 경우 표 처리를 지원합니다 (기본값은 with_tables=True). 표는 분할되지 않으며 각 표는 하나의 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')

Handling attached files

DedocFileLoader는 loader 초기화 시 with_attachmentsTrue로 설정된 경우 첨부 파일 처리를 지원합니다 (기본값은 with_attachments=False). 첨부 파일은 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 [[email protected]](mailto:[email protected])\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 [[email protected]](mailto:[email protected])')

Loading PDF file

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을 서비스로 사용할 수 있습니다. DedocAPIFileLoaderdedoc 라이브러리 설치 없이 사용할 수 있습니다. loader는 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 '

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I