### Setup Tokenizer and Lines for Chunking Example
Source: https://docling-project.github.io/docling/_generated/examples/line_based_chunking
Initializes a HuggingFaceTokenizer with a small token limit and defines sample lines for testing chunking behavior. This setup is crucial for demonstrating overflow scenarios.
```python
# Setup tokenizer with a very small token limit
tokenizer = HuggingFaceTokenizer(
tokenizer=AutoTokenizer.from_pretrained("sentence-transformers/all-MiniLM-L6-v2"),
max_tokens=30, # Very small limit to force overflow
)
# Create chunker with a longer prefix
prefix = (
"| Name | Age | Department | Location |\n|------|-----|------------|----------|\n"
)
print(f"Prefix token count: {tokenizer.count_tokens(prefix)}")
print(f"Max tokens: {tokenizer.get_max_tokens()}\n")
# Sample lines - some will be too long with prefix
lines = [
"| Alice Johnson | 30 | Engineering | San Francisco |\n",
"| Bob Smith | 25 | Marketing | New York |\n",
"| Charlie Brown with a very long name | 35 | Sales Department | Los Angeles |\n",
]
# Check token counts for each line
print("Token counts:")
for i, line in enumerate(lines, 1):
line_tokens = tokenizer.count_tokens(line)
with_prefix = line_tokens + tokenizer.count_tokens(prefix)
print(f" Line {i}: {line_tokens} tokens (with prefix: {with_prefix} tokens)")
print()
```
--------------------------------
### Run Docling Serve with Docker Compose (NVIDIA GPU)
Source: https://docling-project.github.io/docling/usage/api_server/deployment
Starts Docling Serve using Docker Compose with NVIDIA GPU support. This requires a `compose-nvidia.yaml` file and appropriate NVIDIA drivers and toolkit installed on the host.
```bash
# NVIDIA GPU
docker compose -f compose-nvidia.yaml up -d
```
--------------------------------
### Run Ollama Example with Granite-Docling
Source: https://docling-project.github.io/docling/_generated/examples/vlm_pipeline_api_model
Demonstrates using the Granite-Docling preset with the Ollama API runtime. Ensure Ollama is installed and running, and the specified model is pulled. This example configures VLM pipeline options for Ollama.
```python
def run_ollama_example(input_doc_path: Path) -> bool:
"""Example 2: Using Granite-Docling preset with Ollama.
Returns:
True if example ran successfully, False if skipped
"""
print("\n" + "=" * 70)
print("Example 2: Granite-Docling with Ollama (pre-configured API type)")
print("=" * 70)
print("\nPrerequisites:")
print("- Install Ollama: https://ollama.ai")
print("- Pull model: ollama pull ibm/granite-docling:258m")
print()
# Check if Ollama is running
try:
response = requests.get("http://localhost:11434/api/tags", timeout=2)
if response.status_code != 200:
print("WARNING: Ollama server not responding correctly")
print("Skipping Ollama example.\n")
return False
except requests.exceptions.RequestException:
print("WARNING: Ollama server not running at http://localhost:11434")
print("Skipping Ollama example.\n")
return False
# Check and pull the model
model_name = "ibm/granite-docling:258m"
if not check_and_pull_ollama_model(model_name):
print("Skipping Ollama example.\n")
return False
```
--------------------------------
### Install flash-attn Package
Source: https://docling-project.github.io/docling/faq
Install the flash-attn package. Building from sources requires a CUDA development environment. Pre-built wheels are an alternative if available for your setup.
```bash
# Building from sources (required the CUDA dev environment)
pip install flash-attn
```
```bash
# Using pre-built wheels (not available in all possible setups)
FLASH_ATTENTION_SKIP_CUDA_BUILD=TRUE pip install flash-attn
```
--------------------------------
### Building a RAG Pipeline with LangChain and DoclingLoader
Source: https://docling-project.github.io/docling/usage/processing_audio_media
This example demonstrates how to use DoclingLoader to load and chunk audio files from a directory, then index them into a FAISS vector store with OpenAI embeddings for retrieval. Requires 'langchain-docling', 'langchain-openai', and 'faiss-cpu' or 'faiss-gpu' to be installed.
```python
from langchain_docling import DoclingLoader
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
# Load and chunk all audio files in a directory
loader = DoclingLoader("recordings/")
docs = loader.load()
# Embed and index
vectorstore = FAISS.from_documents(docs, OpenAIEmbeddings())
retriever = vectorstore.as_retriever()
# Query in natural language
results = retriever.invoke("What did we decide about the auth service in Q3?")
```
--------------------------------
### Install Docling and Dependencies
Source: https://docling-project.github.io/docling/examples/agent_skill/docling-document-intelligence/SKILL
Install the docling package and its core components. For OpenAI tokenizer support, install the optional chunking-openai extra.
```bash
pip install docling docling-core
# For OpenAI tokenizer support:
pip install 'docling-core[chunking-openai]'
```
--------------------------------
### Install and Serve vLLM Backend
Source: https://docling-project.github.io/docling/examples/agent_skill/docling-document-intelligence/pipelines
Install the vLLM library and serve a Docling model. This is for setting up a high-throughput backend for VLM processing.
```bash
pip install vllm
vllm serve ibm-granite/granite-docling-258M
```
--------------------------------
### Run Docling Serve with Docker Compose (AMD GPU)
Source: https://docling-project.github.io/docling/usage/api_server/deployment
Starts Docling Serve using Docker Compose with AMD GPU support. This requires a `compose-amd.yaml` file and appropriate AMD ROCm installation on the host.
```bash
# AMD GPU
docker compose -f compose-amd.yaml up -d
```
--------------------------------
### Install Dependencies for Qdrant and Docling
Source: https://docling-project.github.io/docling/_generated/examples/retrieval_qdrant
Install the necessary Python packages for Qdrant client, Docling, and FastEmbed. Use `--no-warn-conflicts` to suppress dependency warnings and `-q` for quiet installation.
```python
%pip install --no-warn-conflicts -q qdrant-client docling fastembed
```
--------------------------------
### Install Docling and Dependencies
Source: https://docling-project.github.io/docling/_generated/examples/serialization
Installs the necessary packages for using Docling, including the core library and Rich for enhanced console output. A kernel restart might be required after installation.
```bash
%pip install -qU pip docling docling-core~=2.29 rich
```
--------------------------------
### Install Docling
Source: https://docling-project.github.io/docling/getting_started/rtx
Install the Docling library with all its dependencies. This command ensures that all necessary packages, including GPU support if configured, are installed.
```bash
pip install docling
```
--------------------------------
### Main function to run VLM examples and summarize results
Source: https://docling-project.github.io/docling/_generated/examples/vlm_pipeline_api_model
Configures logging, defines the input document path, and runs various VLM examples (LM Studio, Ollama, VLLM, watsonx.ai). It then prints a summary indicating which examples ran successfully and which were skipped, along with reasons for skipping.
```python
def main():
logging.basicConfig(level=logging.INFO)
data_folder = Path(__file__).parent / "../../tests/data"
input_doc_path = data_folder / "pdf/sources/2305.03393v1-pg9.pdf"
# Track which examples ran
results = {
"LM Studio": run_lmstudio_example(input_doc_path),
"Ollama": run_ollama_example(input_doc_path),
"VLLM": run_vllm_example(input_doc_path),
"watsonx.ai": run_watsonx_example(input_doc_path),
}
# Print summary
print("\n" + "=" * 70)
print("SUMMARY")
print("=" * 70)
ran = [name for name, success in results.items() if success]
skipped = [name for name, success in results.items() if not success]
if ran:
print(f"\n✓ Examples that ran successfully ({len(ran)}):")
for name in ran:
print(f" - {name}")
if skipped:
print(f"\n⊘ Examples that were skipped ({len(skipped)}):")
for name in skipped:
reason = "Server not running"
if name == "watsonx.ai":
if os.environ.get("CI"):
reason = "Running in CI environment"
else:
reason = "Credentials not found (WX_API_KEY, WX_PROJECT_ID)"
print(f" - {name}: {reason}")
print()
if __name__ == "__main__":
main()
```
--------------------------------
### Install Development Dependencies
Source: https://docling-project.github.io/docling/getting_started/installation
Use this command to install all extras for development, excluding the 'feat-ocr-nemotron' extra which has specific system requirements.
```bash
uv sync --all-extras --no-extra feat-ocr-nemotron
```
--------------------------------
### Install Docling and Docling-Core
Source: https://docling-project.github.io/docling/examples/agent_skill/docling-document-intelligence
Install the necessary Docling packages using pip. This is a prerequisite for using the docling CLI and evaluator.
```bash
pip install docling docling-core
```
--------------------------------
### Install Docling
Source: https://docling-project.github.io/docling/_generated/examples/epub_conversion
Install the Docling library using pip. This is a prerequisite for using Docling's features.
```bash
%pip install -q docling
```
--------------------------------
### Install Docling and Dependencies
Source: https://docling-project.github.io/docling/examples/serialization.ipynb
Installs the necessary packages for using Docling, including the core library, serializers, and rich for enhanced output.
```python
%pip install -qU pip docling docling-core~=2.29 rich
```
--------------------------------
### Display Example Image
Source: https://docling-project.github.io/docling/examples/extraction.ipynb
Display an example image using IPython.HTML for visual reference during extraction.
```python
file_path = (
"https://upload.wikimedia.org/wikipedia/commons/9/9f/Swiss_QR-Bill_example.jpg"
)
display.HTML(f"
")
```
--------------------------------
### Install Docling on macOS Intel with uv
Source: https://docling-project.github.io/docling/getting_started/installation
Install Docling and compatible PyTorch versions for macOS Intel (x86_64) using uv.
```bash
# For uv users
uv add torch==2.2.2 torchvision==0.17.2 docling
```
--------------------------------
### Install and Import HybridChunker with OpenAI tokenizers
Source: https://docling-project.github.io/docling/concepts/chunking
Install the chunking-openai extra for OpenAI tokenizers and import HybridChunker from docling-core.
```bash
pip install 'docling-core[chunking-openai]'
```
```python
from docling_core.transforms.chunker.hybrid_chunker import HybridChunker
```
--------------------------------
### Display Example Input Image
Source: https://docling-project.github.io/docling/_generated/examples/extraction
Display an example input image using its URL to visualize the document being processed.
```python
file_path = (
"https://upload.wikimedia.org/wikipedia/commons/9/9f/Swiss_QR-Bill_example.jpg"
)
display.HTML(f"
")
```
--------------------------------
### Install Docling with PyTorch CPU support on Linux
Source: https://docling-project.github.io/docling/getting_started/installation
Install Docling with PyTorch CPU-only support for Linux systems using pip.
```bash
# Example for installing on the Linux cpu-only version
pip install docling --extra-index-url https://download.pytorch.org/whl/cpu
```
--------------------------------
### Install Docling with XBRL Support
Source: https://docling-project.github.io/docling/examples/xbrl_conversion.ipynb
Install the Docling library with XBRL support using pip. This command ensures all necessary dependencies for XBRL processing are included.
```bash
%pip install -q docling
```
--------------------------------
### Input Resolution Examples
Source: https://docling-project.github.io/docling/examples/agent_skill/docling-document-intelligence/SKILL
Demonstrates how to use the `docling` CLI to process local file paths and URLs.
```bash
docling path/to/file.pdf
docling https://example.com/a.pdf
```
--------------------------------
### Initialize DocumentConverter with Default Settings
Source: https://docling-project.github.io/docling/reference/document_converter
Create a DocumentConverter instance allowing all supported input formats. No specific setup is required beyond instantiation.
```python
>>> converter = DocumentConverter()
```
--------------------------------
### Install Dependencies for Docling RAG
Source: https://docling-project.github.io/docling/_generated/examples/rag_llamaindex
Installs necessary LlamaIndex packages and related libraries for the Docling RAG example. Use `--no-warn-conflicts` for Colab environments.
```python
%pip install -q --progress-bar off --no-warn-conflicts llama-index-core llama-index-readers-docling llama-index-node-parser-docling llama-index-embeddings-huggingface llama-index-llms-huggingface-api llama-index-vector-stores-milvus llama-index-readers-file python-dotenv
```
--------------------------------
### setup.py Entrypoint for Docling Plugin
Source: https://docling-project.github.io/docling/concepts/plugins
Defines a Docling plugin entrypoint in a setup.py file using the entry_points argument in setup().
```python
from setuptools import setup
setup(
# ...,
entry_points = {
'docling': [
'your_plugin_name = "your_package.module"'
]
}
)
```
--------------------------------
### Convert with a File Size Limit
Source: https://docling-project.github.io/docling/reference/document_converter
Shows how to use `convert_all` with a `max_file_size` parameter to limit the size of documents processed, specified in bytes (20 MB in this example).
```python
>>> results = converter.convert_all(
... paths, max_file_size=20 * 1024 * 1024
... )
```
--------------------------------
### Install and Run Docling Serve (Local Engine)
Source: https://docling-project.github.io/docling/usage/api_server/deployment
Installs the Docling Serve package with UI support and runs the server in production mode. The local engine is the default and suitable for single-machine use.
```bash
pip install "docling-serve[ui]"
docling-serve run --enable-ui # production-style: reload off, binds 0.0.0.0, UI off by default
# docling-serve dev # dev: auto-reload, binds 127.0.0.1, UI on (localhost only)
```
--------------------------------
### Setup DPK Experiment Folders and Articles
Source: https://docling-project.github.io/docling/_generated/examples/dpk-ingest-chunk-tokenize
Initializes the DPK experiment by creating a temporary data folder and defining the list of articles to be processed. Ensures at least one document is downloaded.
```python
import os
import tempfile
datafolder = tempfile.mkdtemp(dir=os.getcwd())
articles = ["Science,_technology,_engineering,_and_mathematics"]
assert load_corpus(articles, datafolder) > 0, "Faild to download any documents"
```
--------------------------------
### Example Jobkit Configuration File
Source: https://docling-project.github.io/docling/usage/jobkit
This configuration file demonstrates how to set Docling conversion options, specify Google Drive as a source with authentication details, and configure S3 as a target.
```yaml
options: # Example Docling's conversion options
do_ocr: false
sources: # Source location (here Google Drive)
- kind: google_drive
path_id: 1X6B3j7GWlHfIPSF9VUkasN-z49yo1sGFA9xv55L2hSE
token_path: "./dev/google_drive/google_drive_token.json"
credentials_path: "./dev/google_drive/google_drive_credentials.json"
target: # Target location (here S3)
kind: s3
endpoint: localhost:9000
verify_ssl: false
bucket: docling-target
access_key: minioadmin
secret_key: minioadmin
```
--------------------------------
### Configure PDF Pipeline with Tesseract OCR
Source: https://docling-project.github.io/docling/getting_started/installation
Example of configuring the DocumentConverter to use Tesseract OCR for PDF processing. Ensure Tesseract is installed and accessible.
```python
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import (
TesseractOcrOptions,
PdfPipelineOptions,
)
from docling.document_converter import DocumentConverter, PdfFormatOption
pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = True
pipeline_options.ocr_options = TesseractOcrOptions() # Use Tesseract
doc_converter = DocumentConverter(
format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
```
--------------------------------
### SimplePipeline Get Default Options Class Method
Source: https://docling-project.github.io/docling/reference/document_converter
Class method to retrieve the default options for the conversion pipeline. This method returns a ConvertPipelineOptions object.
```python
get_default_options() -> ConvertPipelineOptions
```
--------------------------------
### setup.cfg Entrypoint for Docling Plugin
Source: https://docling-project.github.io/docling/concepts/plugins
Defines a Docling plugin entrypoint in a setup.cfg file using the [options.entry_points] section.
```ini
[options.entry_points]
docling =
your_plugin_name = your_package.module
```
--------------------------------
### FloatingMeta Schema Definition with Examples
Source: https://docling-project.github.io/docling/reference/document_converter
Defines the FloatingMeta model for metadata associated with floating elements, including examples for summary, language, and entities. The summary provides a condensed natural-language description, language uses BCP 47 codes, and entities include extracted named entities with optional types and spans.
```json
{
"additionalProperties": true,
"description": "Metadata model for floating.",
"properties": {
"summary": {
"anyOf": [
{
"$ref": "#/$defs/SummaryMetaField"
},
{
"type": "null"
}
],
"default": null,
"description": "A condensed natural-language summary of the content rooted at this node.",
"examples": [
{
"text": "A short company/location statement."
}
]
},
"language": {
"anyOf": [
{
"$ref": "#/$defs/LanguageMetaField"
},
{
"type": "null"
}
],
"default": null,
"description": "The detected human language of the node content, expressed as a BCP 47 code.",
"examples": [
{
"code": "en"
}
]
},
"entities": {
"anyOf": [
{
"$ref": "#/$defs/EntitiesMetaField"
},
{
"type": "null"
}
],
"default": null,
"description": "Named entities extracted from the node text (persons, organisations, locations, etc.). Each mention carries the entity text, an optional type label, and an optional character span.",
"examples": [
{
```
--------------------------------
### Setup Test Document Directory
Source: https://docling-project.github.io/docling/_generated/examples/rag_llamaindex
Prepares a temporary directory and downloads a PDF file to it for testing. This snippet requires `pathlib`, `tempfile`, and `requests`.
```python
from pathlib import Path
from tempfile import mkdtemp
import requests
tmp_dir_path = Path(mkdtemp())
r = requests.get(SOURCE)
with open(tmp_dir_path / f"{Path(SOURCE).name}.pdf", "wb") as out_file:
out_file.write(r.content)
```
--------------------------------
### Set up Document Store for Visual Grounding
Source: https://docling-project.github.io/docling/_generated/examples/visual_grounding
Initialize a document store to hold converted documents. This involves converting sources using the configured converter, saving them as JSON, and mapping their hashes to file paths.
```python
doc_store = {}
doc_store_root = Path(mkdtemp())
for source in SOURCES:
dl_doc = converter.convert(source=source).document
file_path = Path(doc_store_root / f"{dl_doc.origin.binary_hash}.json")
dl_doc.save_as_json(file_path)
doc_store[dl_doc.origin.binary_hash] = file_path
```
--------------------------------
### Install uv for Virtual Environment Management
Source: https://docling-project.github.io/docling/_generated/examples/rag_opensearch
Installs the uv tool, a fast Python package installer and virtual environment manager. Use this to create and activate a virtual environment before installing project dependencies.
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```
```bash
uv venv
source .venv/bin/activate
```
--------------------------------
### StandardPdfPipeline
Source: https://docling-project.github.io/docling/reference/document_converter
High-performance PDF pipeline with multi-threaded stages. This class provides methods to execute conversions, get default options, and check backend support.
```APIDOC
## Class: StandardPdfPipeline
### Description
High-performance PDF pipeline with multi-threaded stages.
### Methods
* **`execute`**– Executes the conversion process.
* **`get_default_options`**– Retrieves the default options for the pipeline.
* **`is_backend_supported`**– Checks if a given backend is supported by the pipeline.
### Attributes
* **`artifacts_path`** (`Optional[Path]`) – Path to store artifacts.
* **`build_pipe`** (`List[Callable]`) – The list of callables that form the build pipeline.
* **`enrichment_pipe`**– The enrichment pipeline.
* **`keep_images`**– Flag to indicate whether to keep images.
* **`pipeline_options`** (`ThreadedPdfPipelineOptions`) – Options for the threaded PDF pipeline.
```
--------------------------------
### Install Dependencies
Source: https://docling-project.github.io/docling/examples/retrieval_qdrant.ipynb
Installs the necessary Python packages for Qdrant client, Docling, and FastEmbed. A kernel restart might be required after installation.
```python
%pip install --no-warn-conflicts -q qdrant-client docling fastembed
```
--------------------------------
### Start vLLM Inference Server for Docling
Source: https://docling-project.github.io/docling/usage/gpu
Launch the vLLM inference server with optimal parameters for Granite Docling. Ensure the server is running before configuring Docling.
```bash
vllm serve ibm-granite/granite-docling-258M \
--host 127.0.0.1 --port 8000 \
--max-num-seqs 512 \
--max-num-batched-tokens 8192 \
--enable-chunked-prefill \
--gpu-memory-utilization 0.9
```
--------------------------------
### Upload File with Options (Multipart)
Source: https://docling-project.github.io/docling/usage/api_server/rest_api
This example demonstrates uploading a file using multipart/form-data. Options like `from_formats`, `to_formats`, `do_ocr`, `image_export_mode`, and `table_mode` can be passed as form fields.
```bash
curl -X POST "http://localhost:5001/v1/convert/file" \
-H "Content-Type: multipart/form-data" \
-F "files=@2206.01062v1.pdf;type=application/pdf" \
-F "from_formats=pdf" \
-F "to_formats=md" \
-F "do_ocr=true" \
-F "image_export_mode=embedded" \
-F "table_mode=fast"
```
--------------------------------
### Install Docling with VLM Support
Source: https://docling-project.github.io/docling/examples/agent_skill/docling-document-intelligence/pipelines
Install Docling with VLM capabilities using pip. This command installs PyTorch and Transformers, which are required for local VLM inference.
```bash
pip install docling[vlm]
# or manually:
pip install torch transformers accelerate
```
--------------------------------
### Initialize Qdrant Client and Document Converter
Source: https://docling-project.github.io/docling/_generated/examples/retrieval_qdrant
Set up the Qdrant client for in-memory use or connect to a local instance. Configure the `DocumentConverter` to accept HTML input and set the dense and sparse embedding models for the Qdrant client.
```python
COLLECTION_NAME = "docling"
doc_converter = DocumentConverter(allowed_formats=[InputFormat.HTML])
client = QdrantClient(location=":memory:")
# The :memory: mode is a Python imitation of Qdrant's APIs for prototyping and CI.
# For production deployments, use the Docker image: docker run -p 6333:6333 qdrant/qdrant
# client = QdrantClient(location="http://localhost:6333")
client.set_model("sentence-transformers/all-MiniLM-L6-v2")
client.set_sparse_model("Qdrant/bm25")
```
--------------------------------
### Install Dependencies
Source: https://docling-project.github.io/docling/_generated/examples/rag_milvus
Installs the necessary Python packages for Milvus, Docling, OpenAI, and PyTorch. If using Google Colab, a runtime restart may be required after installation.
```bash
! pip install --upgrade "pymilvus[milvus-lite]" docling openai torch
```
--------------------------------
### Setup RQ Engine for Docling Serve
Source: https://docling-project.github.io/docling/usage/api_server/deployment
Sets up the distributed RQ engine for Docling Serve. This involves running a Redis instance, the API server configured for RQ, and one or more RQ worker processes.
```bash
# 1) Redis
docker run -p 6379:6379 redis:7-alpine
# 2) API server (enqueues jobs)
DOCLING_SERVE_ENG_KIND=rq \
DOCLING_SERVE_ENG_RQ_REDIS_URL=redis://localhost:6379/0 \
docling-serve run
# 3) one or more workers (do the conversion)
DOCLING_SERVE_ENG_KIND=rq \
DOCLING_SERVE_ENG_RQ_REDIS_URL=redis://localhost:6379/0 \
docling-serve rq-worker
```
--------------------------------
### Install Docling with VLM
Source: https://docling-project.github.io/docling/_generated/examples/pictures_description
Install the docling library with visual-language model (VLM) support and IPython for interactive environments. A kernel restart may be required after installation.
```python
%pip install -q docling[vlm] ipython
```
--------------------------------
### Install Dependencies for RAG with Milvus
Source: https://docling-project.github.io/docling/examples/rag_milvus.ipynb
Installs necessary Python packages including PyMilvus, Docling, OpenAI, and PyTorch. Restart the runtime if using Google Colab after installation.
```bash
! pip install --upgrade "pymilvus[milvus-lite]" docling openai torch
```
--------------------------------
### Setup and Environment Configuration
Source: https://docling-project.github.io/docling/examples/rag_llamaindex.ipynb
Configures environment variables, loads dotenv, filters warnings, and sets Hugging Face tokenizers parallelism. Retrieves environment variables from Colab or OS.
```python
import os
from pathlib import Path
from tempfile import mkdtemp
from warnings import filterwarnings
from dotenv import load_dotenv
def _get_env_from_colab_or_os(key):
try:
from google.colab import userdata
try:
return userdata.get(key)
except userdata.SecretNotFoundError:
pass
except ImportError:
pass
return os.getenv(key)
load_dotenv()
filterwarnings(action="ignore", category=UserWarning, module="pydantic")
filterwarnings(action="ignore", category=FutureWarning, module="easyocr")
# https://github.com/huggingface/transformers/issues/5486:
os.environ["TOKENIZERS_PARALLELISM"] = "false"
```
--------------------------------
### Run Picture Description API Usage Example with Shell Wrapper
Source: https://docling-project.github.io/docling/_generated/examples/picture_description_api_usage
Use the companion shell script to run the picture description API usage example. The PDF argument is optional.
```bash
docs/examples/run_picture_description_api_usage.sh path/to/input.pdf
```
--------------------------------
### Install RAG Dependencies with uv
Source: https://docling-project.github.io/docling/_generated/examples/rag_opensearch
Installs the necessary Python packages for RAG using OpenSearch, Docling, and LlamaIndex. This command uses uv pip to install quietly and without progress indicators.
```python
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
! uv pip install -q --no-progress notebook ipywidgets docling llama-index-readers-file llama-index-readers-docling llama-index-readers-elasticsearch llama-index-node-parser-docling llama-index-vector-stores-opensearch llama-index-embeddings-huggingface llama-index-llms-ollama
```
--------------------------------
### Run LM Studio Example with Granite-Docling
Source: https://docling-project.github.io/docling/_generated/examples/vlm_pipeline_api_model
Demonstrates using the Granite-Docling preset with the LM Studio API runtime. Ensure LM Studio is running and the model is loaded before execution. This example configures the VLM pipeline options for API communication.
```python
def run_lmstudio_example(input_doc_path: Path) -> bool:
"""Example 1: Using Granite-Docling preset with LM Studio API runtime.
Returns:
True if example ran successfully, False if skipped
"""
print("=" * 70)
print("Example 1: Granite-Docling with LM Studio (pre-configured API type)")
print("=" * 70)
print("\nPrerequisites:")
print("- Start LM Studio: lms server start")
print("- Model will be loaded automatically if not already loaded")
print(" (or manually: lms load granite-docling-258m-mlx)")
print()
# Check if LM Studio is running
try:
response = requests.get("http://localhost:1234/v1/models", timeout=2)
if response.status_code != 200:
print("WARNING: LM Studio server not responding correctly")
print("Skipping LM Studio example.\n")
return False
except requests.exceptions.RequestException:
print("WARNING: LM Studio server not running at http://localhost:1234")
print("Skipping LM Studio example.\n")
return False
# Check and load the model
# Note: LM Studio uses a different model ID than the HuggingFace repo
model_name = "granite-docling-258m-mlx"
if not check_and_load_lmstudio_model(model_name):
print("Skipping LM Studio example.\n")
return False
# Use granite_docling preset with LM Studio API runtime
# The preset is pre-configured for LM Studio API type
vlm_options = VlmConvertOptions.from_preset(
"granite_docling",
engine_options=ApiVlmEngineOptions(
runtime_type=VlmEngineType.API_LMSTUDIO,
# url is pre-configured for LM Studio (http://localhost:1234/v1/chat/completions)
# model name is pre-configured from the preset
timeout=90,
),
)
pipeline_options = VlmPipelineOptions(
vlm_options=vlm_options,
enable_remote_services=True, # Required for API runtimes
)
print("\nOther API types are also pre-configured:")
print("- VlmEngineType.API_OLLAMA: http://localhost:11434/v1/chat/completions")
print("- VlmEngineType.API_OPENAI: https://api.openai.com/v1/chat/completions")
print("- VlmEngineType.API: Generic API endpoint (you specify the URL)")
print("\nEach preset has pre-configured model names for these API types.\n")
doc_converter = DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_options=pipeline_options,
pipeline_cls=VlmPipeline,
)
}
)
result = doc_converter.convert(input_doc_path)
print(result.document.export_to_markdown())
return True
```
--------------------------------
### Install Docling with pip
Source: https://docling-project.github.io/docling/getting_started/installation
Use this command to install the Docling package using pip.
```bash
pip install docling
```
--------------------------------
### Install Docling and Transformers
Source: https://docling-project.github.io/docling/examples/line_based_chunking.ipynb
Installs the necessary Python packages for using Docling and HuggingFace tokenizers.
```python
%pip install -qU pip docling transformers
```
--------------------------------
### Environment Setup and Configuration
Source: https://docling-project.github.io/docling/_generated/examples/visual_grounding
Sets up environment variables, including Hugging Face token, and defines configuration parameters for embedding models, generation models, and data sources. It also configures the prompt template and vector store path.
```python
import os
from pathlib import Path
from tempfile import mkdtemp
from dotenv import load_dotenv
from langchain_core.prompts import PromptTemplate
from langchain_docling.loader import ExportType
def _get_env_from_colab_or_os(key):
try:
from google.colab import userdata
try:
return userdata.get(key)
except userdata.SecretNotFoundError:
pass
except ImportError:
pass
return os.getenv(key)
load_dotenv()
# https://github.com/huggingface/transformers/issues/5486:
os.environ["TOKENIZERS_PARALLELISM"] = "false"
HF_TOKEN = _get_env_from_colab_or_os("HF_TOKEN")
SOURCES = ["https://arxiv.org/pdf/2408.09869"] # Docling Technical Report
EMBED_MODEL_ID = "sentence-transformers/all-MiniLM-L6-v2"
GEN_MODEL_ID = "mistralai/Mixtral-8x7B-Instruct-v0.1"
QUESTION = "Which are the main AI models in Docling?"
PROMPT = PromptTemplate.from_template(
"Context information is below.\n---------------------\n{context}\n---------------------\nGiven the context information and not prior knowledge, answer the query.\nQuery: {input}\nAnswer:\n",
)
TOP_K = 3
MILVUS_URI = str(Path(mkdtemp()) / "docling.db")
```
--------------------------------
### Install Docling and Transformers
Source: https://docling-project.github.io/docling/_generated/examples/line_based_chunking
Installs the necessary Python packages for using Docling and Hugging Face Transformers.
```bash
%pip install -qU pip docling transformers
```
--------------------------------
### Install and Import LineBasedTokenChunker from docling-core
Source: https://docling-project.github.io/docling/concepts/chunking
Install the chunking extra and import LineBasedTokenChunker from docling-core for line-based tokenization.
```bash
pip install 'docling-core[chunking]'
```
```python
from docling_core.transforms.chunker.line_chunker import LineBasedTokenChunker
```
--------------------------------
### Install Docling with uv
Source: https://docling-project.github.io/docling/getting_started/installation
Use this command to add the Docling package to your project using uv.
```bash
uv add docling
```
--------------------------------
### Formatted Invoice Output Example
Source: https://docling-project.github.io/docling/_generated/examples/extraction
An example of the formatted string output after accessing structured invoice data.
```text
Invoice #3139 was sent by Robert Schneider to Pia Rutschmann at Rue du Lac 1268.
```
--------------------------------
### Initialize MilvusClient
Source: https://docling-project.github.io/docling/_generated/examples/rag_milvus
Creates an instance of MilvusClient. Use a local file path for Milvus Lite or a server URI for a running Milvus instance or Zilliz Cloud.
```python
from pymilvus import MilvusClient
milvus_client = MilvusClient(uri="./milvus_demo.db")
collection_name = "my_rag_collection"
```
--------------------------------
### Example Query and Response with Docling Sources
Source: https://docling-project.github.io/docling/_generated/examples/rag_llamaindex
An example of a query and its corresponding answer, including the source nodes with their text and metadata, demonstrating the grounding information preserved by Docling.
```text
Q: Which are the main AI models in Docling?
A: The main AI models in Docling are a layout analysis model and TableFormer. The layout analysis model is an accurate object-detector for page elements, and TableFormer is a state-of-the-art table structure recognition model.
Sources:
[('As part of Docling, we initially release two highly capable AI models to the open-source community, which have been developed and published recently by our team. The first model is a layout analysis model, an accurate object-detector for page elements [13]. The second model is TableFormer [12, 9], a state-of-the-art table structure recognition model. We provide the pre-trained weights (hosted on huggingface) and a separate package for the inference code as docling-ibm-models . Both models are also powering the open-access deepsearch-experience, our cloud-native service for knowledge exploration tasks.',
{'schema_name': 'docling_core.transforms.chunker.DocMeta',
'version': '1.0.0',
'doc_items': [{'self_ref': '#/texts/34',
'parent': {'$ref': '#/body'},
'children': [],
'label': 'text',
'prov': [{'page_no': 3,
'bbox': {'l': 107.07593536376953,
't': 406.1695251464844,
'r': 504.1148681640625,
'b': 330.2677307128906,
'coord_origin': 'BOTTOMLEFT'},
'charspan': [0, 608]}]}],
'headings': ['3.2 AI models'],
'origin': {'mimetype': 'application/pdf',
'binary_hash': 14981478401387673002,
'filename': '2408.09869v3.pdf'}}),
('With Docling , we open-source a very capable and efficient document conversion tool which builds on the powerful, specialized AI models and datasets for layout analysis and table structure recognition we developed and presented in the recent past [12, 13, 9]. Docling is designed as a simple, self-contained python library with permissive license, running entirely locally on commodity hardware. Its code architecture allows for easy extensibility and addition of new features and models.',
{'schema_name': 'docling_core.transforms.chunker.DocMeta',
'version': '1.0.0',
'doc_items': [{'self_ref': '#/texts/9',
'parent': {'$ref': '#/body'},
'children': [],
'label': 'text',
'prov': [{'page_no': 1,
'bbox': {'l': 107.0031967163086,
't': 136.7283935546875,
'r': 504.04998779296875,
'b': 83.30133056640625,
'coord_origin': 'BOTTOMLEFT'},
'charspan': [0, 488]}]}],
'headings': ['1 Introduction'],
'origin': {'mimetype': 'application/pdf',
'binary_hash': 14981478401387673002,
'filename': '2408.09869v3.pdf'}})]
```