Skip to main content

An integration package connecting PyMuPDF4LLM to LangChain

Project description

langchain-pymupdf4llm

An integration package connecting PyMuPDF4LLM to LangChain as a Document Loader.

Introduction

langchain-pymupdf4llm is a powerful LangChain integration package that seamlessly incorporates the capabilities of PyMuPDF4LLM as a LangChain Document Loader. This package is designed to facilitate the process of extracting and converting PDF content into Markdown format, making it an ideal tool for integrating with Large Language Models (LLMs) and Retrieval-Augmented Generation (RAG) environments.

Features

The core functionality of this integration relies on PyMuPDF4LLM, which is designed to convert PDF pages to Markdown using the robust PyMuPDF library. Key features inherited from PyMuPDF4LLM include:

  • Markdown Extraction: Converts standard text and tables into GitHub-compatible Markdown format.
  • Advanced Formatting: Detects and formats headers based on font size, bold and italic text, mono-spaced text, code blocks, as well as ordered and unordered lists.
  • Multi-Column and Graphics Support: Easily manages multi-column pages and extracts images and vector graphics.

For more detailed information on PyMuPDF4LLM, visit the official documentation webpage.

The integration provided by langchain-pymupdf4llm adds additional features:

  • Markdown Content with Image Descriptions: When image extraction is enabled, images are included in the Markdown output with descriptive text provided by an image parser instance provided during initialization of the Document Loader.

Installation

Install the package using pip to start using the Document Loader:

# When released, will be available as a PyPi package
pip install -U langchain-pymupdf4llm

# For now, install from GitHub repository
# pip install git+https://github.com/lakinduboteju/langchain-pymupdf4llm.git@main

# pip install -qU langchain_community

Usage

You can easily integrate and use the PyMuPDF4LLMLoader in your Python application for loading and parsing PDFs. Below is an example of how to set up and utilize this loader.

Import and Instantiate the Loader

Begin by importing the necessary class and creating an instance of PyMuPDF4LLMLoader:

from langchain_pymupdf4llm import PyMuPDF4LLMLoader

# from langchain_community.document_loaders.parsers import (
#     TesseractBlobParser,
#     RapidOCRBlobParser,
#     LLMImageBlobParser,
# )

loader = PyMuPDF4LLMLoader(
    file_path="/path/to/input.pdf",
    # Headers to use for GET request to download a file from a web path
    # (if file_path is a web url)
    ## headers=None,

    # Password for opening encrypted PDF
    ## password=None,

    # Extraction mode, either "single" for the entire document or
    # "page" for page-wise extraction.
    mode="single",

    # Delimiter to separate pages in single-mode extraction
    # default value is "\n-----\n\n"
    pages_delimiter="\n\f",

    # Enable images extraction (as text based on images_parser)
    ## extract_images=True,

    # Image parser generates text for a provided image blob
    ## images_parser=TesseractBlobParser(),
    ## images_parser=RapidOCRBlobParser(),
    ## images_parser=LLMImageBlobParser(model=ChatOpenAI(
    ##     model="gpt-4o-mini",
    ##     max_tokens=1024
    ## )),

    # Table extraction strategy to use. Options are
    # "lines_strict", "lines", or "text". "lines_strict" is the default
    # strategy and is the most accurate for tables with column and row lines,
    # but may not work well with all documents.
    # "lines" is a less strict strategy that may work better with
    # some documents.
    # "text" is the least strict strategy and may work better
    # with documents that do not have tables with lines.
    ## table_strategy="lines",

    # Mono-spaced text will not be parsed as code blocks
    ## ignore_code=True
)

Lazy Load Documents

Use the lazy_load() method to load documents efficiently. This approach saves resources by loading pages on-demand:

docs = []
docs_lazy = loader.lazy_load()

for doc in docs_lazy:
    docs.append(doc)
print(docs[0].page_content[:100])
print(docs[0].metadata)

Asynchronous Loading

For applications that benefit from asynchronous operations, load documents using the aload() method:

docs = await loader.aload()
print(docs[0].page_content[:100])
print(docs[0].metadata)

Contribute

Development Instructions

  1. Bring up development environment on Docker.

    # Build Docker image for dev env
    bash ./docker_build_dev_env.sh
    
    # Run dev env on Docker container
    bash ./docker_run_dev_env.sh
    
    # Start bash session on Docker container
    docker exec -it langchain-pymupdf4llm-dev bash
    
    # exit
    # docker stop langchain-pymupdf4llm-dev
    # docker start langchain-pymupdf4llm-dev
    # docker exec -it langchain-pymupdf4llm-dev bash
    # docker stop langchain-pymupdf4llm-dev
    # docker rm langchain-pymupdf4llm-dev
    # bash ./docker_run_dev_env.sh
    
  2. Develop on Docker development environment.

    poetry install --with dev,test
    
  3. Create example documents for tests using LaTeX.

    apt update -y
    apt install -y texlive
    
    cd ./tests/examples
    pdflatex sample_1.tex
    
  4. Use Jupyter.

    poetry run \
    jupyter notebook --allow-root --ip=0.0.0.0
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_pymupdf4llm-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

langchain_pymupdf4llm-0.1.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file langchain_pymupdf4llm-0.1.1.tar.gz.

File metadata

  • Download URL: langchain_pymupdf4llm-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.9.21 Linux/5.10.104-linuxkit

File hashes

Hashes for langchain_pymupdf4llm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 175c88e7ebc55e16117f8face75266ca699cbff5650b167b8351418b3d99408a
MD5 224bc06e61b50611208eac48bc9982ff
BLAKE2b-256 bb6f2e69bca64f713034cc3f3d2d05cbdfb4914f0c009813e96d8fddf11c10ea

See more details on using hashes here.

File details

Details for the file langchain_pymupdf4llm-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_pymupdf4llm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 599138c45f5a36f1f3b94294eca8027963de693c57b2d741221bd66bb074ef17
MD5 8ad1bb9bd7e2406d4e69f8f3376e6da8
BLAKE2b-256 440c03ef46c23ca3dc83ade75c2ded4930de50a2772b90a764408b7554f452fb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page