"This is a code recipe that uses [OpenSearch](https://opensearch.org/), an open-source search and analytics tool,\n",
"and the [LlamaIndex](https://github.com/run-llama/llama_index) framework to perform RAG over documents parsed by [Docling](https://docling-project.github.io/docling/).\n",
"\n",
"In this notebook, we accomplish the following:\n",
"* 📚 Parse documents using Docling's document conversion capabilities\n",
"* 🧩 Perform hierarchical chunking of the documents using Docling\n",
"* 🔢 Generate text embeddings on document chunks\n",
"* 🤖 Perform RAG using OpenSearch and the LlamaIndex framework\n",
"* 🛠️ Leverage the transformation and structure capabilities of Docling documents for RAG\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Preparation\n",
"\n",
"### Running the notebook\n",
"\n",
"For running this notebook on your machine, you can use applications like [Jupyter Notebook](https://jupyter.org/install) or [Visual Studio Code](https://code.visualstudio.com/docs/datascience/jupyter-notebooks).\n",
"\n",
"💡 For best results, please use **GPU acceleration** to run this notebook.\n",
"\n",
"### Virtual environment\n",
"\n",
"Before installing dependencies and to avoid conflicts in your environment, it is advisable to use a [virtual environment (venv)](https://docs.python.org/3/library/venv.html).\n",
"For instance, [uv](https://docs.astral.sh/uv/) is a popular tool to manage virtual environments and dependencies. You can install it with:\n",
"/Users/ceb/git/docling/.venv/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py:2249: UnsupportedFieldAttributeWarning: The 'validate_default' attribute with value True was provided to the `Field()` function, which has no effect in the context it was used. 'validate_default' is field-specific metadata, and can only be attached to a model field using `Annotated` metadata or by assignment. This may have happened because an `Annotated` type alias using the `type` statement was used, or if the `Field()` function was attached to a single member of a union type.\n",
"Part of what makes Docling so remarkable is the fact that it can run on commodity hardware. This means that this notebook can be run on a local machine with GPU acceleration. If you're using a MacBook with a silicon chip, Docling integrates seamlessly with Metal Performance Shaders (MPS). MPS provides out-of-the-box GPU acceleration for macOS, seamlessly integrating with PyTorch and TensorFlow, offering energy-efficient performance on Apple Silicon, and broad compatibility with all Metal-supported GPUs.\n",
"\n",
"The code below checks if a GPU is available, either via CUDA or MPS."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MPS GPU is enabled.\n"
]
}
],
"source": [
"# Check if GPU or MPS is available\n",
"if torch.cuda.is_available():\n",
" device = torch.device(\"cuda\")\n",
" print(f\"CUDA GPU is enabled: {torch.cuda.get_device_name(0)}\")\n",
"elif torch.backends.mps.is_available():\n",
" device = torch.device(\"mps\")\n",
" print(\"MPS GPU is enabled.\")\n",
"else:\n",
" raise OSError(\n",
" \"No GPU or MPS device found. Please check your environment and ensure GPU or MPS support is configured.\"\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Local OpenSearch instance\n",
"\n",
"To run the notebook locally, we can pull an OpenSearch image and run a single node for local development.\n",
"You can use a container tool like [Podman](https://podman.io/) or [Docker](https://www.docker.com/).\n",
"In the interest of simplicity, we disable the SSL option for this example.\n",
"\n",
"💡 The version of the OpenSearch instance needs to be compatible with the version of the [OpenSearch Python Client](https://github.com/opensearch-project/opensearch-py) library,\n",
"since this library is used by the LlamaIndex framework, which we leverage in this notebook.\n",
"We will use [HuggingFace](https://huggingface.co/) and [Ollama](https://ollama.com/) to run language models on your local computer, rather than relying on cloud services.\n",
"print(f\"The embedding dimension is {embed_dim}.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Process Data Using Docling\n",
"\n",
"Docling can parse various document formats into a unified representation ([DoclingDocument](https://docling-project.github.io/docling/concepts/docling_document/)), which can then be exported to different output formats. For a full list of supported input and output formats, please refer to [Supported formats](https://docling-project.github.io/docling/usage/supported_formats/) section of Docling's documentation.\n",
"In this recipe, we will use a single PDF file, the [Docling Technical Report](https://arxiv.org/pdf/2408.09869). We will process it using the [Hybrid Chunker](https://docling-project.github.io/docling/concepts/chunking/#hybrid-chunker) provided by Docling to generate structured, hierarchical chunks suitable for downstream RAG tasks."
"We will convert the original PDF file into a `DoclingDocument` format using a `DoclingReader` object. We specify the JSON export type to retain the document hierarchical structure as an input for the next step (chunking the document)."
"- `DoclingNodeParser` executes the document-based chunking with the hybrid chunker, which leverages the tokenizer of the embedding model to ensure that the resulting chunks fit within the model input text limit.\n",
"- `MetadataTransform` is a custom transformation to ensure that generated chunk metadata is best formatted for indexing with OpenSearch\n",
"\n",
"\n",
"💡 For demonstration purposes, we configure the hybrid chunker to produce chunks capped at 200 tokens. The optimal limit will vary according to the specific requirements of the AI application in question.\n",
"If this value is omitted, the chunker automatically derives the maximum size from the tokenizer. This safeguard guarantees that each chunk remains within the bounds supported by the underlying embedding model."
"We then initialize the index using our sample data (a single PDF file), the Docling node parser, and the OpenSearch client that we just created.\n",
"\n",
"💡 You may get a warning message like:\n",
"> Token indices sequence length is longer than the specified maximum sequence length for this model\n",
"\n",
"This is a _false alarm_ and you may get more background explanation in [Docling's FAQ](https://docling-project.github.io/docling/faq/#hybridchunker-triggers-warning-token-indices-sequence-length-is-longer-than-the-specified-maximum-sequence-length-for-this-model) page."
"In this section, we will see how to assemble a RAG system, execute a query, and get a generated response.\n",
"\n",
"We will also describe how to leverage Docling capabilities to improve RAG results.\n",
"\n",
"\n",
"### Run a query\n",
"\n",
"With LlamaIndex's query engine, we can simply run a RAG system as follows:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">👤: Which are the main AI models in Docling?\n",
"🤖: The two main AI models used in Docling are:\n",
"\n",
"<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>. A layout analysis model, an accurate object-detector for page elements \n",
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">👤: What is the time to solution with the native backend on Intel?\n",
"🤖: The time to solution <span style=\"font-weight: bold\">(</span>TTS<span style=\"font-weight: bold\">)</span> for the native backend on Intel is:\n",
"- For Apple M3 Max <span style=\"font-weight: bold\">(</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span> cores<span style=\"font-weight: bold\">)</span>: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">375</span> seconds \n",
"So the TTS with the native backend on Intel ranges from approximately <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">244</span> to <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">375</span> seconds\n",
"Token indices sequence length is longer than the specified maximum sequence length for this model (538 > 512). Running this sequence through the model will result in indexing errors\n"
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">👤: What is the time to solution with the native backend on Intel?\n",
"🤖: The table shows that for the native backend on Intel systems, the time-to-solution \n",
"Observe that the generated response is now more accurate. Refer to the [Advanced chunking & serialization](https://docling-project.github.io/docling/examples/advanced_chunking_and_serialization/) example for more details on serialization strategies."
"\u001b[2;32m│ │ \u001b[0m\u001b[32m'text'\u001b[0m: \u001b[32m'- \u001b[0m\u001b[32m[\u001b[0m\u001b[32m13\u001b[0m\u001b[32m]\u001b[0m\u001b[32m B. Pfitzmann, C. Auer, M. Dolfi, A. S. Nassar, and P. Staar. Doclaynet: a large humanannotated dataset for document-layout segmentation. pages 3743-3751, 2022.\\n- \u001b[0m\u001b[32m[\u001b[0m\u001b[32m14\u001b[0m\u001b[32m]\u001b[0m\u001b[32m pypdf Maintainers. pypdf: '\u001b[0m+\u001b[1;36m314\u001b[0m,\n",
"We may want to restrict the retrieval to only those chunks containing tabular data, expecting to retrieve more quantitative information for our type of question:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"How does pypdfium perform?\n"
]
},
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"font-weight: bold\">[</span>\n",
"<span style=\"color: #7fbf7f; text-decoration-color: #7fbf7f\">│ │ </span><span style=\"color: #008000; text-decoration-color: #008000\">'text'</span>: <span style=\"color: #008000; text-decoration-color: #008000\">'Table 1: Runtime characteristics of Docling with the standard model pipeline and settings, on our test dataset of 225 pages, on two different systems. OCR is disabled. We show the time-to-solution (TT'</span>+<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">515</span>,\n",
"\u001b[2;32m│ │ \u001b[0m\u001b[32m'text'\u001b[0m: \u001b[32m'Table 1: Runtime characteristics of Docling with the standard model pipeline and settings, on our test dataset of 225 pages, on two different systems. OCR is disabled. We show the time-to-solution \u001b[0m\u001b[32m(\u001b[0m\u001b[32mTT'\u001b[0m+\u001b[1;36m515\u001b[0m,\n",
"Hybrid search combines keyword and semantic search to improve search relevance. To avoid relying on traditional score normalization techniques, the reciprocal rank fusion (RRF) feature on hybrid search can significantly improve the relevance of the retrieved chunks in our RAG system.\n",
"\n",
"First, create a search pipeline and specify RRF as technique:"
"The first retriever, which entirely relies on semantic (vector) search, fails to catch the supporting chunk for the given question in the top 1 position.\n",
"Note that we highlight few expected keywords for illustration purposes.\n"
"Optionally, you can configure custom pipeline features and runtime options, such as \n",
"turning on or off features <span style=\"font-weight: bold\">(</span>e.g. OCR, table structure recognition<span style=\"font-weight: bold\">)</span>, enforcing limits on \n",
"the input document size, and defining the budget of CPU threads. Advanced usage examples\n",
"and options are documented in the README file. <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">Docling also provides a Dockerfile</span> to \n",
"demonstrate how to install and run it inside a container.\n",
"source = \u001b[32m\"https://arxiv.org/pdf/2206.01062\"\u001b[0m # PDF path or URL converter = \n",
"\u001b[1;35mDocumentConverter\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m result = \u001b[1;35mconverter.convert_single\u001b[0m\u001b[1m(\u001b[0msource\u001b[1m)\u001b[0m \n",
"\u001b[1;35mprint\u001b[0m\u001b[1m(\u001b[0m\u001b[1;35mresult.render_as_markdown\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m\u001b[1m)\u001b[0m # output: \u001b[32m\"## DocLayNet: A Large Human -Annotated \u001b[0m\n",
"\u001b[32mDataset for Document -Layout Analysis \u001b[0m\u001b[32m[\u001b[0m\u001b[32m...\u001b[0m\u001b[32m]\u001b[0m\u001b[32m\"\u001b[0m\n",
"```\n",
"Optionally, you can configure custom pipeline features and runtime options, such as \n",
"turning on or off features \u001b[1m(\u001b[0me.g. OCR, table structure recognition\u001b[1m)\u001b[0m, enforcing limits on \n",
"the input document size, and defining the budget of CPU threads. Advanced usage examples\n",
"and options are documented in the README file. \u001b[1;33mDocling also provides a Dockerfile\u001b[0m to \n",
"demonstrate how to install and run it inside a container.\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"QUERY = \"Does Docling project provide a Dockerfile?\"\n",
"<span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">print</span><span style=\"font-weight: bold\">(</span><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">result.render_as_markdown</span><span style=\"font-weight: bold\">())</span> # output: <span style=\"color: #008000; text-decoration-color: #008000\">\"## DocLayNet: A Large Human -Annotated </span>\n",
"<span style=\"color: #008000; text-decoration-color: #008000\">Dataset for Document -Layout Analysis [...]\"</span>\n",
"```\n",
"Optionally, you can configure custom pipeline features and runtime options, such as \n",
"turning on or off features <span style=\"font-weight: bold\">(</span>e.g. OCR, table structure recognition<span style=\"font-weight: bold\">)</span>, enforcing limits on \n",
"the input document size, and defining the budget of CPU threads. Advanced usage examples\n",
"and options are documented in the README file. <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">Docling also provides a Dockerfile</span> to \n",
"demonstrate how to install and run it inside a container.\n",
"source = \u001b[32m\"https://arxiv.org/pdf/2206.01062\"\u001b[0m # PDF path or URL converter = \n",
"\u001b[1;35mDocumentConverter\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m result = \u001b[1;35mconverter.convert_single\u001b[0m\u001b[1m(\u001b[0msource\u001b[1m)\u001b[0m \n",
"\u001b[1;35mprint\u001b[0m\u001b[1m(\u001b[0m\u001b[1;35mresult.render_as_markdown\u001b[0m\u001b[1m(\u001b[0m\u001b[1m)\u001b[0m\u001b[1m)\u001b[0m # output: \u001b[32m\"## DocLayNet: A Large Human -Annotated \u001b[0m\n",
"\u001b[32mDataset for Document -Layout Analysis \u001b[0m\u001b[32m[\u001b[0m\u001b[32m...\u001b[0m\u001b[32m]\u001b[0m\u001b[32m\"\u001b[0m\n",
"We therefore decided to provide multiple backend choices, and additionally open-source a\n",
"custombuilt PDF parser, which is based on the low-level qpdf <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span><span style=\"font-weight: bold\">]</span> library. It is made \n",
"available in a separate package named docling-parse and powers the default PDF backend \n",
"in Docling. As an alternative, we provide a PDF backend relying on pypdfium , which may \n",
"be a safe backup choice in certain cases, e.g. if issues are seen with particular font \n",
"Using small chunks can offer several benefits: it increases retrieval precision and it keeps the answer generation tightly focused, which improves accuracy, reduces hallucination, and speeds up inferece.\n",
"However, your RAG system may overlook contextual information necessary for producing a fully grounded response.\n",
"\n",
"Docling's preservation of document structure enables you to employ various strategies for enriching the context available during answer generation within the RAG pipeline.\n",
"For example, after identifying the most relevant chunk, you might include adjacent chunks from the same section as additional groudning material before generating the final answer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following example, the generated response is wrong, since the top retrieved chunks do not contain all the information that is required to answer the question."
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">👤: According to the tests with arXiv and IBM Redbooks, which backend should I use if I \n",
"have limited resources and complex tables?\n",
"🤖: According to the tests in this section using both the MacBook Pro M3 Max and \n",
"bare-metal server running Ubuntu <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">20.04</span> LTS on an Intel Xeon E5-<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2690</span> CPU with a fixed \n",
"thread budget of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span>, Docling achieved faster processing speeds when using the \n",
"custom-built PDF backend based on the low-level qpdf library <span style=\"font-weight: bold\">(</span>docling-parse<span style=\"font-weight: bold\">)</span> compared to\n",
"the alternative PDF backend relying on pypdfium.\n",
"\n",
"Furthermore, the context mentions that Docling provides a separate package named \n",
"docling-ibm-models which includes pre-trained weights and inference code for \n",
"TableFormer, a state-of-the-art table structure recognition model. This suggests that if\n",
"you have complex tables in your documents, using this specialized table recognition \n",
"model could be beneficial.\n",
"\n",
"Therefore, based on the tests with arXiv papers and IBM Redbooks, if you have limited \n",
"resources <span style=\"font-weight: bold\">(</span>likely referring to computational power<span style=\"font-weight: bold\">)</span> and need to process documents \n",
"containing complex tables, it would be recommended to use the docling-parse PDF backend \n",
"along with the TableFormer AI model from docling-ibm-models. This combination should \n",
"provide a good balance of performance and table recognition capabilities for your \n",
"specific needs.\n",
"</pre>\n"
],
"text/plain": [
"👤: According to the tests with arXiv and IBM Redbooks, which backend should I use if I \n",
"have limited resources and complex tables?\n",
"🤖: According to the tests in this section using both the MacBook Pro M3 Max and \n",
"bare-metal server running Ubuntu \u001b[1;36m20.04\u001b[0m LTS on an Intel Xeon E5-\u001b[1;36m2690\u001b[0m CPU with a fixed \n",
"thread budget of \u001b[1;36m4\u001b[0m, Docling achieved faster processing speeds when using the \n",
"custom-built PDF backend based on the low-level qpdf library \u001b[1m(\u001b[0mdocling-parse\u001b[1m)\u001b[0m compared to\n",
"the alternative PDF backend relying on pypdfium.\n",
"\n",
"Furthermore, the context mentions that Docling provides a separate package named \n",
"docling-ibm-models which includes pre-trained weights and inference code for \n",
"TableFormer, a state-of-the-art table structure recognition model. This suggests that if\n",
"you have complex tables in your documents, using this specialized table recognition \n",
"model could be beneficial.\n",
"\n",
"Therefore, based on the tests with arXiv papers and IBM Redbooks, if you have limited \n",
"resources \u001b[1m(\u001b[0mlikely referring to computational power\u001b[1m)\u001b[0m and need to process documents \n",
"containing complex tables, it would be recommended to use the docling-parse PDF backend \n",
"along with the TableFormer AI model from docling-ibm-models. This combination should \n",
"provide a good balance of performance and table recognition capabilities for your \n",
"specific needs.\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"QUERY = \"According to the tests with arXiv and IBM Redbooks, which backend should I use if I have limited resources and complex tables?\"\n",
"In this section, we establish some reference numbers for the processing speed of Docling\n",
"and the resource budget it requires. All tests in this section are run with default \n",
"options on our standard test set distributed with Docling, which consists of three \n",
"papers from arXiv and two IBM Redbooks, with a total of <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">225</span> pages. Measurements were \n",
"taken using both available PDF backends on two different hardware systems: one MacBook \n",
"Pro M3 Max, and one bare-metal server running Ubuntu <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">20.04</span> LTS on an Intel Xeon E5-<span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">2690</span> \n",
"CPU. For reproducibility, we fixed the thread budget <span style=\"font-weight: bold\">(</span>through setting OMP NUM THREADS \n",
"environment variable <span style=\"font-weight: bold\">)</span> once to <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span> <span style=\"font-weight: bold\">(</span>Docling default<span style=\"font-weight: bold\">)</span> and once to <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">16</span> <span style=\"font-weight: bold\">(</span>equal to full core \n",
"count on the test hardware<span style=\"font-weight: bold\">)</span>. All results are shown in Table <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">1</span>.\n",
"In this section, we establish some reference numbers for the processing speed of Docling\n",
"and the resource budget it requires. All tests in this section are run with default \n",
"options on our standard test set distributed with Docling, which consists of three \n",
"papers from arXiv and two IBM Redbooks, with a total of \u001b[1;36m225\u001b[0m pages. Measurements were \n",
"taken using both available PDF backends on two different hardware systems: one MacBook \n",
"Pro M3 Max, and one bare-metal server running Ubuntu \u001b[1;36m20.04\u001b[0m LTS on an Intel Xeon E5-\u001b[1;36m2690\u001b[0m \n",
"CPU. For reproducibility, we fixed the thread budget \u001b[1m(\u001b[0mthrough setting OMP NUM THREADS \n",
"environment variable \u001b[1m)\u001b[0m once to \u001b[1;36m4\u001b[0m \u001b[1m(\u001b[0mDocling default\u001b[1m)\u001b[0m and once to \u001b[1;36m16\u001b[0m \u001b[1m(\u001b[0mequal to full core \n",
"count on the test hardware\u001b[1m)\u001b[0m. All results are shown in Table \u001b[1;36m1\u001b[0m.\n"
"We therefore decided to provide multiple backend choices, and additionally open-source a\n",
"custombuilt PDF parser, which is based on the low-level qpdf <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">4</span><span style=\"font-weight: bold\">]</span> library. It is made \n",
"available in a separate package named docling-parse and powers the default PDF backend \n",
"in Docling. As an alternative, we provide a PDF backend relying on pypdfium , which may \n",
"be a safe backup choice in certain cases, e.g. if issues are seen with particular font \n",
"As part of Docling, we initially release two highly capable AI models to the open-source\n",
"community, which have been developed and published recently by our team. The first model\n",
"is a layout analysis model, an accurate object-detector for page elements <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">13</span><span style=\"font-weight: bold\">]</span>. The \n",
"second model is TableFormer <span style=\"font-weight: bold\">[</span><span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">12</span>, <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">9</span><span style=\"font-weight: bold\">]</span>, a state-of-the-art table structure recognition \n",
"model. We provide the pre-trained weights <span style=\"font-weight: bold\">(</span>hosted on huggingface<span style=\"font-weight: bold\">)</span> and a separate package\n",
"for the inference code as docling-ibm-models . Both models are also powering the \n",
"open-access deepsearch-experience, our cloud-native service for knowledge exploration \n",
"As part of Docling, we initially release two highly capable AI models to the open-source\n",
"community, which have been developed and published recently by our team. The first model\n",
"is a layout analysis model, an accurate object-detector for page elements \u001b[1m[\u001b[0m\u001b[1;36m13\u001b[0m\u001b[1m]\u001b[0m. The \n",
"second model is TableFormer \u001b[1m[\u001b[0m\u001b[1;36m12\u001b[0m, \u001b[1;36m9\u001b[0m\u001b[1m]\u001b[0m, a state-of-the-art table structure recognition \n",
"model. We provide the pre-trained weights \u001b[1m(\u001b[0mhosted on huggingface\u001b[1m)\u001b[0m and a separate package\n",
"for the inference code as docling-ibm-models . Both models are also powering the \n",
"open-access deepsearch-experience, our cloud-native service for knowledge exploration \n",
"Even though the top retrieved chunks are relevant for the question, the key information lays in the paragraph after the first chunk:\n",
"\n",
"> If you need to run Docling in very low-resource environments, please consider configuring the pypdfium backend. While it is faster and more memory efficient than the default docling-parse backend, it will come at the expense of worse quality results, especially in table structure recovery."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We next examine the fragments that immediately precede and follow the top‑retrieved chunk, so long as those neighbors remain within the same section, to preserve the semantic integrity of the context.\n",
"The generated answer is now accurate because it has been grounded in the necessary contextual information.\n",
"\n",
"💡 In a production setting, it may be preferable to persist the parsed documents (i.e., `DoclingDocument` objects) as JSON in an object store or database and then fetch them when you need to traverse the document for context‑expansion scenarios. In this simplified example, however, we will query the OpenSearch index directly to obtain the required chunks."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">👤: According to the tests with arXiv and IBM Redbooks, which backend should I use if I \n",
"have limited resources and complex tables?\n",
"🤖: According to the tests described in the provided context, if you need to run Docling\n",
"in a very low-resource environment and are dealing with complex tables that require \n",
"high-quality table structure recovery, you should consider configuring the pypdfium \n",
"backend. The context mentions that while it is faster and more memory efficient than the\n",
"default docling-parse backend, it may come at the expense of worse quality results, \n",
"especially in table structure recovery. Therefore, for limited resources and complex \n",
"tables where quality is crucial, pypdfium would be a suitable choice despite its \n",
"potential drawbacks compared to the default backend.\n",
"</pre>\n"
],
"text/plain": [
"👤: According to the tests with arXiv and IBM Redbooks, which backend should I use if I \n",
"have limited resources and complex tables?\n",
"🤖: According to the tests described in the provided context, if you need to run Docling\n",
"in a very low-resource environment and are dealing with complex tables that require \n",
"high-quality table structure recovery, you should consider configuring the pypdfium \n",
"backend. The context mentions that while it is faster and more memory efficient than the\n",
"default docling-parse backend, it may come at the expense of worse quality results, \n",
"especially in table structure recovery. Therefore, for limited resources and complex \n",
"tables where quality is crucial, pypdfium would be a suitable choice despite its \n",
"potential drawbacks compared to the default backend.\n"