Skip to main content
This guide provides a quick overview for getting started with the PyMuPDF4LLMLoader document loader. For detailed documentation of all PyMuPDF4LLMLoader features and configurations head to the GitHub repository.

概述

集成详情

加载器特性

设置

To access PyMuPDF4LLM document loader you’ll need to install the langchain-pymupdf4llm integration package.

凭证

No credentials are required to use PyMuPDF4LLMLoader. 要启用模型调用的自动追踪,请设置你的 LangSmith API 密钥:

安装

安装 langchain-community and langchain-pymupdf4llm

初始化

现在我们可以实例化模型对象并加载文档:

加载

惰性加载

metadata 属性至少包含以下键:
  • source
  • page (if in mode page)
  • total_page
  • creationdate
  • creator
  • producer
额外的元数据因解析器而异。 这些信息可能很有用(例如用于分类你的 PDF)。

Splitting mode & custom pages delimiter

加载 PDF 文件时,你可以用两种不同的方式分割它:
  • By page
  • As a single text flow
默认情况下,PyMuPDF4LLMLoader will split the PDF by page.

Extract the PDF by page. each page is extracted as a langchain document object

In this mode the pdf is split by pages and the resulting Documents metadata contains the page (page number). But in some cases we could want to process the pdf as a single text flow (so we don’t cut some paragraphs in half). In this case you can use the single mode :

Extract the whole PDF as a single langchain document object

Logically, in this mode, the page (page_number) metadata disappears. Here’s how to clearly identify where pages end in the text flow :

Add a custom pages_delimiter to identify where are ends of pages in single mode

默认pages_delimiter is \n-----\n\n. This could simply be \n, or \f to clearly indicate a page change, or <!— PAGE BREAK —> for seamless injection in a Markdown viewer without a visual effect.

Extract images from the PDF

You can extract images from your PDFs (in text form) with a choice of three different solutions:
  • rapidOCR (lightweight Optical Character Recognition tool)
  • Tesseract (OCR tool with high precision)
  • Multimodal language model
The result is inserted at the end of text of the page.

Extract images from the PDF with rapidOCR

请注意,RapidOCR 设计用于处理中文和英文,不支持其他语言。

Extract images from the PDF with tesseract

Extract images from the PDF with multimodal model

Extract tables from the PDF

With PyMUPDF4LLM you can extract tables from your PDFs in markdown format :

Working with files

许多文档加载器涉及文件解析。这类加载器之间的差异通常在于文件的解析方式,而不是文件的加载方式。 For example, you can use open to read the binary content of either a PDF or a markdown file, but you need different parsing logic to convert that binary data into text. As a result, it can be helpful to decouple the parsing logic from the loading logic, which makes it easier to reuse a given parser regardless of how the data was loaded. 你可以使用此策略用相同的解析参数分析不同的文件。

API 参考

For detailed documentation of all PyMuPDF4LLMLoader features and configurations head to the GitHub repository: github.com/lakinduboteju/langchain-pymupdf4llm