mirror of
https://github.com/DS4SD/docling.git
synced 2025-08-02 15:32:30 +00:00
merged with main
Signed-off-by: Peter Staar <taa@zurich.ibm.com>
This commit is contained in:
commit
745615ca12
2
.github/workflows/checks.yml
vendored
2
.github/workflows/checks.yml
vendored
@ -10,7 +10,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Install tesseract
|
- name: Install tesseract
|
||||||
run: sudo apt-get update && sudo apt-get install -y tesseract-ocr tesseract-ocr-eng tesseract-ocr-fra tesseract-ocr-deu tesseract-ocr-spa libleptonica-dev libtesseract-dev pkg-config
|
run: sudo apt-get update && sudo apt-get install -y tesseract-ocr tesseract-ocr-eng tesseract-ocr-fra tesseract-ocr-deu tesseract-ocr-spa tesseract-ocr-script-latn libleptonica-dev libtesseract-dev pkg-config
|
||||||
- name: Set TESSDATA_PREFIX
|
- name: Set TESSDATA_PREFIX
|
||||||
run: |
|
run: |
|
||||||
echo "TESSDATA_PREFIX=$(dpkg -L tesseract-ocr-eng | grep tessdata$)" >> "$GITHUB_ENV"
|
echo "TESSDATA_PREFIX=$(dpkg -L tesseract-ocr-eng | grep tessdata$)" >> "$GITHUB_ENV"
|
||||||
|
5
.github/workflows/docs.yml
vendored
5
.github/workflows/docs.yml
vendored
@ -14,7 +14,10 @@ jobs:
|
|||||||
- uses: ./.github/actions/setup-poetry
|
- uses: ./.github/actions/setup-poetry
|
||||||
- name: Build docs
|
- name: Build docs
|
||||||
run: poetry run mkdocs build --verbose --clean
|
run: poetry run mkdocs build --verbose --clean
|
||||||
|
- name: Make docs LLM ready
|
||||||
|
if: inputs.deploy
|
||||||
|
uses: demodrive-ai/llms-txt-action@ad720693843126e6a73910a667d0eba37c1dea4b
|
||||||
- name: Build and push docs
|
- name: Build and push docs
|
||||||
if: inputs.deploy
|
if: inputs.deploy
|
||||||
run: poetry run mkdocs gh-deploy --force
|
run: poetry run mkdocs gh-deploy --force --dirty
|
||||||
|
|
33
CHANGELOG.md
33
CHANGELOG.md
@ -1,3 +1,36 @@
|
|||||||
|
## [v2.15.1](https://github.com/DS4SD/docling/releases/tag/v2.15.1) - 2025-01-10
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
* Improve OCR results, stricten criteria before dropping bitmap areas ([#719](https://github.com/DS4SD/docling/issues/719)) ([`5a060f2`](https://github.com/DS4SD/docling/commit/5a060f237d1decd0ff9db9e73478978419315778))
|
||||||
|
* Allow earlier requests versions ([#716](https://github.com/DS4SD/docling/issues/716)) ([`e64b5a2`](https://github.com/DS4SD/docling/commit/e64b5a2f628acc340a6d94ee6f1ada2aa267cecc))
|
||||||
|
|
||||||
|
### Documentation
|
||||||
|
|
||||||
|
* Add pointers to LangChain-side docs ([#718](https://github.com/DS4SD/docling/issues/718)) ([`9a6b5c8`](https://github.com/DS4SD/docling/commit/9a6b5c8c8debc81e0ddcbe91df6afbbeb29e97e6))
|
||||||
|
* Add LangChain docs ([#717](https://github.com/DS4SD/docling/issues/717)) ([`4fa8028`](https://github.com/DS4SD/docling/commit/4fa8028bd8120d7557e1d45ba31e200e130af698))
|
||||||
|
|
||||||
|
## [v2.15.0](https://github.com/DS4SD/docling/releases/tag/v2.15.0) - 2025-01-08
|
||||||
|
|
||||||
|
### Feature
|
||||||
|
|
||||||
|
* Added http header support for document converter and cli ([#642](https://github.com/DS4SD/docling/issues/642)) ([`0ee849e`](https://github.com/DS4SD/docling/commit/0ee849e8bc8cf24d1c5597af3fe20a7fa19a29e0))
|
||||||
|
|
||||||
|
### Fix
|
||||||
|
|
||||||
|
* Correct scaling of debug visualizations, tune OCR ([#700](https://github.com/DS4SD/docling/issues/700)) ([`5cb4cf6`](https://github.com/DS4SD/docling/commit/5cb4cf6f19f91e6c87141e93400c4b54b93aa5d7))
|
||||||
|
* Let BeautifulSoup detect the HTML encoding ([#695](https://github.com/DS4SD/docling/issues/695)) ([`42856fd`](https://github.com/DS4SD/docling/commit/42856fdf79559188ec4617bc5d3a007286f114d2))
|
||||||
|
* **mspowerpoint:** Handle invalid images in PowerPoint slides ([#650](https://github.com/DS4SD/docling/issues/650)) ([`d49650c`](https://github.com/DS4SD/docling/commit/d49650c54ffa60bc6d6106970e104071689bc7b0))
|
||||||
|
|
||||||
|
### Documentation
|
||||||
|
|
||||||
|
* Specify docstring types ([#702](https://github.com/DS4SD/docling/issues/702)) ([`ead396a`](https://github.com/DS4SD/docling/commit/ead396ab407f6bbd43176abd6ed2bed7ed8c7c43))
|
||||||
|
* Add link to rag with granite ([#698](https://github.com/DS4SD/docling/issues/698)) ([`6701f34`](https://github.com/DS4SD/docling/commit/6701f34c855992c52918b210c65a2edb1c827c01))
|
||||||
|
* Add integrations, revamp docs ([#693](https://github.com/DS4SD/docling/issues/693)) ([`2d24fae`](https://github.com/DS4SD/docling/commit/2d24faecd96bfa656b2b8c80f25cdf251a50526a))
|
||||||
|
* Add OpenContracts as an integration ([#679](https://github.com/DS4SD/docling/issues/679)) ([`569038d`](https://github.com/DS4SD/docling/commit/569038df4205703f87517ea58da7902d143e7699))
|
||||||
|
* Add Weaviate RAG recipe notebook ([#451](https://github.com/DS4SD/docling/issues/451)) ([`2b591f9`](https://github.com/DS4SD/docling/commit/2b591f98726ed0d883236dd0550201b95203eebb))
|
||||||
|
* Document Haystack & Vectara support ([#628](https://github.com/DS4SD/docling/issues/628)) ([`fc645ea`](https://github.com/DS4SD/docling/commit/fc645ea531ddc67959640b428007851d641c923e))
|
||||||
|
|
||||||
## [v2.14.0](https://github.com/DS4SD/docling/releases/tag/v2.14.0) - 2024-12-18
|
## [v2.14.0](https://github.com/DS4SD/docling/releases/tag/v2.14.0) - 2024-12-18
|
||||||
|
|
||||||
### Feature
|
### Feature
|
||||||
|
@ -29,7 +29,7 @@ Docling parses documents and exports them to the desired format with ease and sp
|
|||||||
* 🗂️ Reads popular document formats (PDF, DOCX, PPTX, XLSX, Images, HTML, AsciiDoc & Markdown) and exports to HTML, Markdown and JSON (with embedded and referenced images)
|
* 🗂️ Reads popular document formats (PDF, DOCX, PPTX, XLSX, Images, HTML, AsciiDoc & Markdown) and exports to HTML, Markdown and JSON (with embedded and referenced images)
|
||||||
* 📑 Advanced PDF document understanding including page layout, reading order & table structures
|
* 📑 Advanced PDF document understanding including page layout, reading order & table structures
|
||||||
* 🧩 Unified, expressive [DoclingDocument](https://ds4sd.github.io/docling/concepts/docling_document/) representation format
|
* 🧩 Unified, expressive [DoclingDocument](https://ds4sd.github.io/docling/concepts/docling_document/) representation format
|
||||||
* 🤖 Easy integration with 🦙 LlamaIndex & 🦜🔗 LangChain for powerful RAG / QA applications
|
* 🤖 Plug-and-play [integrations](https://ds4sd.github.io/docling/integrations/) incl. LangChain, LlamaIndex, Crew AI & Haystack for agentic AI
|
||||||
* 🔍 OCR support for scanned PDFs
|
* 🔍 OCR support for scanned PDFs
|
||||||
* 💻 Simple and convenient CLI
|
* 💻 Simple and convenient CLI
|
||||||
|
|
||||||
@ -39,7 +39,6 @@ Explore the [documentation](https://ds4sd.github.io/docling/) to discover plenty
|
|||||||
|
|
||||||
* ♾️ Equation & code extraction
|
* ♾️ Equation & code extraction
|
||||||
* 📝 Metadata extraction, including title, authors, references & language
|
* 📝 Metadata extraction, including title, authors, references & language
|
||||||
* 🦜🔗 Native LangChain extension
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ _log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class AsciiDocBackend(DeclarativeDocumentBackend):
|
class AsciiDocBackend(DeclarativeDocumentBackend):
|
||||||
|
|
||||||
def __init__(self, in_doc: InputDocument, path_or_stream: Union[BytesIO, Path]):
|
def __init__(self, in_doc: InputDocument, path_or_stream: Union[BytesIO, Path]):
|
||||||
super().__init__(in_doc, path_or_stream)
|
super().__init__(in_doc, path_or_stream)
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ class DoclingParsePageBackend(PdfPageBackend):
|
|||||||
return cells
|
return cells
|
||||||
|
|
||||||
def get_bitmap_rects(self, scale: float = 1) -> Iterable[BoundingBox]:
|
def get_bitmap_rects(self, scale: float = 1) -> Iterable[BoundingBox]:
|
||||||
AREA_THRESHOLD = 32 * 32
|
AREA_THRESHOLD = 0 # 32 * 32
|
||||||
|
|
||||||
for i in range(len(self._dpage["images"])):
|
for i in range(len(self._dpage["images"])):
|
||||||
bitmap = self._dpage["images"][i]
|
bitmap = self._dpage["images"][i]
|
||||||
@ -163,7 +163,7 @@ class DoclingParsePageBackend(PdfPageBackend):
|
|||||||
l=0, r=0, t=0, b=0, coord_origin=CoordOrigin.BOTTOMLEFT
|
l=0, r=0, t=0, b=0, coord_origin=CoordOrigin.BOTTOMLEFT
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
padbox = cropbox.to_bottom_left_origin(page_size.height)
|
padbox = cropbox.to_bottom_left_origin(page_size.height).model_copy()
|
||||||
padbox.r = page_size.width - padbox.r
|
padbox.r = page_size.width - padbox.r
|
||||||
padbox.t = page_size.height - padbox.t
|
padbox.t = page_size.height - padbox.t
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ class DoclingParseV2PageBackend(PdfPageBackend):
|
|||||||
return cells
|
return cells
|
||||||
|
|
||||||
def get_bitmap_rects(self, scale: float = 1) -> Iterable[BoundingBox]:
|
def get_bitmap_rects(self, scale: float = 1) -> Iterable[BoundingBox]:
|
||||||
AREA_THRESHOLD = 32 * 32
|
AREA_THRESHOLD = 0 # 32 * 32
|
||||||
|
|
||||||
images = self._dpage["sanitized"]["images"]["data"]
|
images = self._dpage["sanitized"]["images"]["data"]
|
||||||
images_header = self._dpage["sanitized"]["images"]["header"]
|
images_header = self._dpage["sanitized"]["images"]["header"]
|
||||||
@ -178,7 +178,7 @@ class DoclingParseV2PageBackend(PdfPageBackend):
|
|||||||
l=0, r=0, t=0, b=0, coord_origin=CoordOrigin.BOTTOMLEFT
|
l=0, r=0, t=0, b=0, coord_origin=CoordOrigin.BOTTOMLEFT
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
padbox = cropbox.to_bottom_left_origin(page_size.height)
|
padbox = cropbox.to_bottom_left_origin(page_size.height).model_copy()
|
||||||
padbox.r = page_size.width - padbox.r
|
padbox.r = page_size.width - padbox.r
|
||||||
padbox.t = page_size.height - padbox.t
|
padbox.t = page_size.height - padbox.t
|
||||||
|
|
||||||
|
@ -37,10 +37,10 @@ class HTMLDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if isinstance(self.path_or_stream, BytesIO):
|
if isinstance(self.path_or_stream, BytesIO):
|
||||||
text_stream = self.path_or_stream.getvalue().decode("utf-8")
|
text_stream = self.path_or_stream.getvalue()
|
||||||
self.soup = BeautifulSoup(text_stream, "html.parser")
|
self.soup = BeautifulSoup(text_stream, "html.parser")
|
||||||
if isinstance(self.path_or_stream, Path):
|
if isinstance(self.path_or_stream, Path):
|
||||||
with open(self.path_or_stream, "r", encoding="utf-8") as f:
|
with open(self.path_or_stream, "rb") as f:
|
||||||
html_content = f.read()
|
html_content = f.read()
|
||||||
self.soup = BeautifulSoup(html_content, "html.parser")
|
self.soup = BeautifulSoup(html_content, "html.parser")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -215,7 +215,7 @@ class HTMLDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
label = DocItemLabel.CODE
|
label = DocItemLabel.CODE
|
||||||
if len(text) == 0:
|
if len(text) == 0:
|
||||||
return
|
return
|
||||||
doc.add_text(parent=self.parents[self.level], label=label, text=text)
|
doc.add_code(parent=self.parents[self.level], label=label, text=text)
|
||||||
|
|
||||||
def handle_paragraph(self, element, idx, doc):
|
def handle_paragraph(self, element, idx, doc):
|
||||||
"""Handles paragraph tags (p)."""
|
"""Handles paragraph tags (p)."""
|
||||||
|
@ -3,19 +3,22 @@ import re
|
|||||||
import warnings
|
import warnings
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Set, Union
|
from typing import List, Optional, Set, Union
|
||||||
|
|
||||||
import marko
|
import marko
|
||||||
import marko.ext
|
import marko.ext
|
||||||
import marko.ext.gfm
|
import marko.ext.gfm
|
||||||
import marko.inline
|
import marko.inline
|
||||||
from docling_core.types.doc import (
|
from docling_core.types.doc import (
|
||||||
|
DocItem,
|
||||||
DocItemLabel,
|
DocItemLabel,
|
||||||
DoclingDocument,
|
DoclingDocument,
|
||||||
DocumentOrigin,
|
DocumentOrigin,
|
||||||
GroupLabel,
|
GroupLabel,
|
||||||
|
NodeItem,
|
||||||
TableCell,
|
TableCell,
|
||||||
TableData,
|
TableData,
|
||||||
|
TextItem,
|
||||||
)
|
)
|
||||||
from marko import Markdown
|
from marko import Markdown
|
||||||
|
|
||||||
@ -27,8 +30,7 @@ _log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
||||||
|
def shorten_underscore_sequences(self, markdown_text: str, max_length: int = 10):
|
||||||
def shorten_underscore_sequences(self, markdown_text, max_length=10):
|
|
||||||
# This regex will match any sequence of underscores
|
# This regex will match any sequence of underscores
|
||||||
pattern = r"_+"
|
pattern = r"_+"
|
||||||
|
|
||||||
@ -90,13 +92,13 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
) from e
|
) from e
|
||||||
return
|
return
|
||||||
|
|
||||||
def close_table(self, doc=None):
|
def close_table(self, doc: DoclingDocument):
|
||||||
if self.in_table:
|
if self.in_table:
|
||||||
_log.debug("=== TABLE START ===")
|
_log.debug("=== TABLE START ===")
|
||||||
for md_table_row in self.md_table_buffer:
|
for md_table_row in self.md_table_buffer:
|
||||||
_log.debug(md_table_row)
|
_log.debug(md_table_row)
|
||||||
_log.debug("=== TABLE END ===")
|
_log.debug("=== TABLE END ===")
|
||||||
tcells = []
|
tcells: List[TableCell] = []
|
||||||
result_table = []
|
result_table = []
|
||||||
for n, md_table_row in enumerate(self.md_table_buffer):
|
for n, md_table_row in enumerate(self.md_table_buffer):
|
||||||
data = []
|
data = []
|
||||||
@ -137,15 +139,19 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
self.in_table = False
|
self.in_table = False
|
||||||
self.md_table_buffer = [] # clean table markdown buffer
|
self.md_table_buffer = [] # clean table markdown buffer
|
||||||
# Initialize Docling TableData
|
# Initialize Docling TableData
|
||||||
data = TableData(num_rows=num_rows, num_cols=num_cols, table_cells=tcells)
|
table_data = TableData(
|
||||||
|
num_rows=num_rows, num_cols=num_cols, table_cells=tcells
|
||||||
|
)
|
||||||
# Populate
|
# Populate
|
||||||
for tcell in tcells:
|
for tcell in tcells:
|
||||||
data.table_cells.append(tcell)
|
table_data.table_cells.append(tcell)
|
||||||
if len(tcells) > 0:
|
if len(tcells) > 0:
|
||||||
doc.add_table(data=data)
|
doc.add_table(data=table_data)
|
||||||
return
|
return
|
||||||
|
|
||||||
def process_inline_text(self, parent_element, doc=None):
|
def process_inline_text(
|
||||||
|
self, parent_element: Optional[NodeItem], doc: DoclingDocument
|
||||||
|
):
|
||||||
# self.inline_text_buffer += str(text_in)
|
# self.inline_text_buffer += str(text_in)
|
||||||
txt = self.inline_text_buffer.strip()
|
txt = self.inline_text_buffer.strip()
|
||||||
if len(txt) > 0:
|
if len(txt) > 0:
|
||||||
@ -156,14 +162,20 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
)
|
)
|
||||||
self.inline_text_buffer = ""
|
self.inline_text_buffer = ""
|
||||||
|
|
||||||
def iterate_elements(self, element, depth=0, doc=None, parent_element=None):
|
def iterate_elements(
|
||||||
|
self,
|
||||||
|
element: marko.block.Element,
|
||||||
|
depth: int,
|
||||||
|
doc: DoclingDocument,
|
||||||
|
parent_element: Optional[NodeItem] = None,
|
||||||
|
):
|
||||||
# Iterates over all elements in the AST
|
# Iterates over all elements in the AST
|
||||||
# Check for different element types and process relevant details
|
# Check for different element types and process relevant details
|
||||||
if isinstance(element, marko.block.Heading):
|
if isinstance(element, marko.block.Heading):
|
||||||
self.close_table(doc)
|
self.close_table(doc)
|
||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
_log.debug(
|
_log.debug(
|
||||||
f" - Heading level {element.level}, content: {element.children[0].children}"
|
f" - Heading level {element.level}, content: {element.children[0].children}" # type: ignore
|
||||||
)
|
)
|
||||||
if element.level == 1:
|
if element.level == 1:
|
||||||
doc_label = DocItemLabel.TITLE
|
doc_label = DocItemLabel.TITLE
|
||||||
@ -172,10 +184,10 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
|
|
||||||
# Header could have arbitrary inclusion of bold, italic or emphasis,
|
# Header could have arbitrary inclusion of bold, italic or emphasis,
|
||||||
# hence we need to traverse the tree to get full text of a header
|
# hence we need to traverse the tree to get full text of a header
|
||||||
strings = []
|
strings: List[str] = []
|
||||||
|
|
||||||
# Define a recursive function to traverse the tree
|
# Define a recursive function to traverse the tree
|
||||||
def traverse(node):
|
def traverse(node: marko.block.BlockElement):
|
||||||
# Check if the node has a "children" attribute
|
# Check if the node has a "children" attribute
|
||||||
if hasattr(node, "children"):
|
if hasattr(node, "children"):
|
||||||
# If "children" is a list, continue traversal
|
# If "children" is a list, continue traversal
|
||||||
@ -209,9 +221,13 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
_log.debug(" - List item")
|
_log.debug(" - List item")
|
||||||
|
|
||||||
snippet_text = str(element.children[0].children[0].children)
|
snippet_text = str(element.children[0].children[0].children) # type: ignore
|
||||||
is_numbered = False
|
is_numbered = False
|
||||||
if parent_element.label == GroupLabel.ORDERED_LIST:
|
if (
|
||||||
|
parent_element is not None
|
||||||
|
and isinstance(parent_element, DocItem)
|
||||||
|
and parent_element.label == GroupLabel.ORDERED_LIST
|
||||||
|
):
|
||||||
is_numbered = True
|
is_numbered = True
|
||||||
doc.add_list_item(
|
doc.add_list_item(
|
||||||
enumerated=is_numbered, parent=parent_element, text=snippet_text
|
enumerated=is_numbered, parent=parent_element, text=snippet_text
|
||||||
@ -221,7 +237,14 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
self.close_table(doc)
|
self.close_table(doc)
|
||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
_log.debug(f" - Image with alt: {element.title}, url: {element.dest}")
|
_log.debug(f" - Image with alt: {element.title}, url: {element.dest}")
|
||||||
doc.add_picture(parent=parent_element, caption=element.title)
|
|
||||||
|
fig_caption: Optional[TextItem] = None
|
||||||
|
if element.title is not None and element.title != "":
|
||||||
|
fig_caption = doc.add_text(
|
||||||
|
label=DocItemLabel.CAPTION, text=element.title
|
||||||
|
)
|
||||||
|
|
||||||
|
doc.add_picture(parent=parent_element, caption=fig_caption)
|
||||||
|
|
||||||
elif isinstance(element, marko.block.Paragraph):
|
elif isinstance(element, marko.block.Paragraph):
|
||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
@ -252,27 +275,21 @@ class MarkdownDocumentBackend(DeclarativeDocumentBackend):
|
|||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
_log.debug(f" - Code Span: {element.children}")
|
_log.debug(f" - Code Span: {element.children}")
|
||||||
snippet_text = str(element.children).strip()
|
snippet_text = str(element.children).strip()
|
||||||
doc.add_text(
|
doc.add_code(parent=parent_element, text=snippet_text)
|
||||||
label=DocItemLabel.CODE, parent=parent_element, text=snippet_text
|
|
||||||
)
|
|
||||||
|
|
||||||
elif isinstance(element, marko.block.CodeBlock):
|
elif isinstance(element, marko.block.CodeBlock):
|
||||||
self.close_table(doc)
|
self.close_table(doc)
|
||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
_log.debug(f" - Code Block: {element.children}")
|
_log.debug(f" - Code Block: {element.children}")
|
||||||
snippet_text = str(element.children[0].children).strip()
|
snippet_text = str(element.children[0].children).strip() # type: ignore
|
||||||
doc.add_text(
|
doc.add_code(parent=parent_element, text=snippet_text)
|
||||||
label=DocItemLabel.CODE, parent=parent_element, text=snippet_text
|
|
||||||
)
|
|
||||||
|
|
||||||
elif isinstance(element, marko.block.FencedCode):
|
elif isinstance(element, marko.block.FencedCode):
|
||||||
self.close_table(doc)
|
self.close_table(doc)
|
||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
_log.debug(f" - Code Block: {element.children}")
|
_log.debug(f" - Code Block: {element.children}")
|
||||||
snippet_text = str(element.children[0].children).strip()
|
snippet_text = str(element.children[0].children).strip() # type: ignore
|
||||||
doc.add_text(
|
doc.add_code(parent=parent_element, text=snippet_text)
|
||||||
label=DocItemLabel.CODE, parent=parent_element, text=snippet_text
|
|
||||||
)
|
|
||||||
|
|
||||||
elif isinstance(element, marko.inline.LineBreak):
|
elif isinstance(element, marko.inline.LineBreak):
|
||||||
self.process_inline_text(parent_element, doc)
|
self.process_inline_text(parent_element, doc)
|
||||||
|
@ -45,7 +45,6 @@ class ExcelTable(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
class MsExcelDocumentBackend(DeclarativeDocumentBackend):
|
||||||
|
|
||||||
def __init__(self, in_doc: "InputDocument", path_or_stream: Union[BytesIO, Path]):
|
def __init__(self, in_doc: "InputDocument", path_or_stream: Union[BytesIO, Path]):
|
||||||
super().__init__(in_doc, path_or_stream)
|
super().__init__(in_doc, path_or_stream)
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ from docling_core.types.doc import (
|
|||||||
TableCell,
|
TableCell,
|
||||||
TableData,
|
TableData,
|
||||||
)
|
)
|
||||||
from PIL import Image
|
from PIL import Image, UnidentifiedImageError
|
||||||
from pptx import Presentation
|
from pptx import Presentation
|
||||||
from pptx.enum.shapes import MSO_SHAPE_TYPE, PP_PLACEHOLDER
|
from pptx.enum.shapes import MSO_SHAPE_TYPE, PP_PLACEHOLDER
|
||||||
|
|
||||||
@ -120,6 +120,7 @@ class MsPowerpointDocumentBackend(DeclarativeDocumentBackend, PaginatedDocumentB
|
|||||||
bullet_type = "None"
|
bullet_type = "None"
|
||||||
list_text = ""
|
list_text = ""
|
||||||
list_label = GroupLabel.LIST
|
list_label = GroupLabel.LIST
|
||||||
|
doc_label = DocItemLabel.LIST_ITEM
|
||||||
prov = self.generate_prov(shape, slide_ind, shape.text.strip())
|
prov = self.generate_prov(shape, slide_ind, shape.text.strip())
|
||||||
|
|
||||||
# Identify if shape contains lists
|
# Identify if shape contains lists
|
||||||
@ -276,16 +277,19 @@ class MsPowerpointDocumentBackend(DeclarativeDocumentBackend, PaginatedDocumentB
|
|||||||
im_dpi, _ = image.dpi
|
im_dpi, _ = image.dpi
|
||||||
|
|
||||||
# Open it with PIL
|
# Open it with PIL
|
||||||
pil_image = Image.open(BytesIO(image_bytes))
|
try:
|
||||||
|
pil_image = Image.open(BytesIO(image_bytes))
|
||||||
|
|
||||||
# shape has picture
|
# shape has picture
|
||||||
prov = self.generate_prov(shape, slide_ind, "")
|
prov = self.generate_prov(shape, slide_ind, "")
|
||||||
doc.add_picture(
|
doc.add_picture(
|
||||||
parent=parent_slide,
|
parent=parent_slide,
|
||||||
image=ImageRef.from_pil(image=pil_image, dpi=im_dpi),
|
image=ImageRef.from_pil(image=pil_image, dpi=im_dpi),
|
||||||
caption=None,
|
caption=None,
|
||||||
prov=prov,
|
prov=prov,
|
||||||
)
|
)
|
||||||
|
except (UnidentifiedImageError, OSError) as e:
|
||||||
|
_log.warning(f"Warning: image cannot be loaded by Pillow: {e}")
|
||||||
return
|
return
|
||||||
|
|
||||||
def handle_tables(self, shape, parent_slide, slide_ind, doc):
|
def handle_tables(self, shape, parent_slide, slide_ind, doc):
|
||||||
|
@ -26,7 +26,6 @@ _log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class MsWordDocumentBackend(DeclarativeDocumentBackend):
|
class MsWordDocumentBackend(DeclarativeDocumentBackend):
|
||||||
|
|
||||||
def __init__(self, in_doc: "InputDocument", path_or_stream: Union[BytesIO, Path]):
|
def __init__(self, in_doc: "InputDocument", path_or_stream: Union[BytesIO, Path]):
|
||||||
super().__init__(in_doc, path_or_stream)
|
super().__init__(in_doc, path_or_stream)
|
||||||
self.XML_KEY = (
|
self.XML_KEY = (
|
||||||
|
@ -12,7 +12,6 @@ from docling.datamodel.document import InputDocument
|
|||||||
|
|
||||||
|
|
||||||
class PdfPageBackend(ABC):
|
class PdfPageBackend(ABC):
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_text_in_rect(self, bbox: BoundingBox) -> str:
|
def get_text_in_rect(self, bbox: BoundingBox) -> str:
|
||||||
pass
|
pass
|
||||||
@ -45,7 +44,6 @@ class PdfPageBackend(ABC):
|
|||||||
|
|
||||||
|
|
||||||
class PdfDocumentBackend(PaginatedDocumentBackend):
|
class PdfDocumentBackend(PaginatedDocumentBackend):
|
||||||
|
|
||||||
def __init__(self, in_doc: InputDocument, path_or_stream: Union[BytesIO, Path]):
|
def __init__(self, in_doc: InputDocument, path_or_stream: Union[BytesIO, Path]):
|
||||||
super().__init__(in_doc, path_or_stream)
|
super().__init__(in_doc, path_or_stream)
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ class PyPdfiumPageBackend(PdfPageBackend):
|
|||||||
return self.valid
|
return self.valid
|
||||||
|
|
||||||
def get_bitmap_rects(self, scale: float = 1) -> Iterable[BoundingBox]:
|
def get_bitmap_rects(self, scale: float = 1) -> Iterable[BoundingBox]:
|
||||||
AREA_THRESHOLD = 32 * 32
|
AREA_THRESHOLD = 0 # 32 * 32
|
||||||
for obj in self._ppage.get_objects(filter=[pdfium_c.FPDF_PAGEOBJ_IMAGE]):
|
for obj in self._ppage.get_objects(filter=[pdfium_c.FPDF_PAGEOBJ_IMAGE]):
|
||||||
pos = obj.get_pos()
|
pos = obj.get_pos()
|
||||||
cropbox = BoundingBox.from_tuple(
|
cropbox = BoundingBox.from_tuple(
|
||||||
@ -210,7 +210,7 @@ class PyPdfiumPageBackend(PdfPageBackend):
|
|||||||
l=0, r=0, t=0, b=0, coord_origin=CoordOrigin.BOTTOMLEFT
|
l=0, r=0, t=0, b=0, coord_origin=CoordOrigin.BOTTOMLEFT
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
padbox = cropbox.to_bottom_left_origin(page_size.height)
|
padbox = cropbox.to_bottom_left_origin(page_size.height).model_copy()
|
||||||
padbox.r = page_size.width - padbox.r
|
padbox.r = page_size.width - padbox.r
|
||||||
padbox.t = page_size.height - padbox.t
|
padbox.t = page_size.height - padbox.t
|
||||||
|
|
||||||
|
@ -164,6 +164,11 @@ def convert(
|
|||||||
to_formats: List[OutputFormat] = typer.Option(
|
to_formats: List[OutputFormat] = typer.Option(
|
||||||
None, "--to", help="Specify output formats. Defaults to Markdown."
|
None, "--to", help="Specify output formats. Defaults to Markdown."
|
||||||
),
|
),
|
||||||
|
headers: str = typer.Option(
|
||||||
|
None,
|
||||||
|
"--headers",
|
||||||
|
help="Specify http request headers used when fetching url input sources in the form of a JSON string",
|
||||||
|
),
|
||||||
image_export_mode: Annotated[
|
image_export_mode: Annotated[
|
||||||
ImageRefMode,
|
ImageRefMode,
|
||||||
typer.Option(
|
typer.Option(
|
||||||
@ -279,12 +284,19 @@ def convert(
|
|||||||
if from_formats is None:
|
if from_formats is None:
|
||||||
from_formats = [e for e in InputFormat]
|
from_formats = [e for e in InputFormat]
|
||||||
|
|
||||||
|
parsed_headers: Optional[Dict[str, str]] = None
|
||||||
|
if headers is not None:
|
||||||
|
headers_t = TypeAdapter(Dict[str, str])
|
||||||
|
parsed_headers = headers_t.validate_json(headers)
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
input_doc_paths: List[Path] = []
|
input_doc_paths: List[Path] = []
|
||||||
for src in input_sources:
|
for src in input_sources:
|
||||||
try:
|
try:
|
||||||
# check if we can fetch some remote url
|
# check if we can fetch some remote url
|
||||||
source = resolve_source_to_path(source=src, workdir=Path(tempdir))
|
source = resolve_source_to_path(
|
||||||
|
source=src, headers=parsed_headers, workdir=Path(tempdir)
|
||||||
|
)
|
||||||
input_doc_paths.append(source)
|
input_doc_paths.append(source)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
err_console.print(
|
err_console.print(
|
||||||
@ -390,7 +402,7 @@ def convert(
|
|||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
conv_results = doc_converter.convert_all(
|
conv_results = doc_converter.convert_all(
|
||||||
input_doc_paths, raises_on_error=abort_on_error
|
input_doc_paths, headers=parsed_headers, raises_on_error=abort_on_error
|
||||||
)
|
)
|
||||||
|
|
||||||
output.mkdir(parents=True, exist_ok=True)
|
output.mkdir(parents=True, exist_ok=True)
|
||||||
|
@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Union
|
|||||||
from docling_core.types.doc import (
|
from docling_core.types.doc import (
|
||||||
BoundingBox,
|
BoundingBox,
|
||||||
DocItemLabel,
|
DocItemLabel,
|
||||||
|
NodeItem,
|
||||||
PictureDataType,
|
PictureDataType,
|
||||||
Size,
|
Size,
|
||||||
TableCell,
|
TableCell,
|
||||||
@ -201,6 +202,13 @@ class AssembledUnit(BaseModel):
|
|||||||
headers: List[PageElement] = []
|
headers: List[PageElement] = []
|
||||||
|
|
||||||
|
|
||||||
|
class ItemAndImageEnrichmentElement(BaseModel):
|
||||||
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||||
|
|
||||||
|
item: NodeItem
|
||||||
|
image: Image
|
||||||
|
|
||||||
|
|
||||||
class Page(BaseModel):
|
class Page(BaseModel):
|
||||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||||
|
|
||||||
@ -219,12 +227,28 @@ class Page(BaseModel):
|
|||||||
{}
|
{}
|
||||||
) # Cache of images in different scales. By default it is cleared during assembling.
|
) # Cache of images in different scales. By default it is cleared during assembling.
|
||||||
|
|
||||||
def get_image(self, scale: float = 1.0) -> Optional[Image]:
|
def get_image(
|
||||||
|
self, scale: float = 1.0, cropbox: Optional[BoundingBox] = None
|
||||||
|
) -> Optional[Image]:
|
||||||
if self._backend is None:
|
if self._backend is None:
|
||||||
return self._image_cache.get(scale, None)
|
return self._image_cache.get(scale, None)
|
||||||
|
|
||||||
if not scale in self._image_cache:
|
if not scale in self._image_cache:
|
||||||
self._image_cache[scale] = self._backend.get_page_image(scale=scale)
|
if cropbox is None:
|
||||||
return self._image_cache[scale]
|
self._image_cache[scale] = self._backend.get_page_image(scale=scale)
|
||||||
|
else:
|
||||||
|
return self._backend.get_page_image(scale=scale, cropbox=cropbox)
|
||||||
|
|
||||||
|
if cropbox is None:
|
||||||
|
return self._image_cache[scale]
|
||||||
|
else:
|
||||||
|
page_im = self._image_cache[scale]
|
||||||
|
assert self.size is not None
|
||||||
|
return page_im.crop(
|
||||||
|
cropbox.to_top_left_origin(page_height=self.size.height)
|
||||||
|
.scaled(scale=scale)
|
||||||
|
.as_tuple()
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def image(self) -> Optional[Image]:
|
def image(self) -> Optional[Image]:
|
||||||
|
@ -227,13 +227,18 @@ class _DummyBackend(AbstractDocumentBackend):
|
|||||||
class _DocumentConversionInput(BaseModel):
|
class _DocumentConversionInput(BaseModel):
|
||||||
|
|
||||||
path_or_stream_iterator: Iterable[Union[Path, str, DocumentStream]]
|
path_or_stream_iterator: Iterable[Union[Path, str, DocumentStream]]
|
||||||
|
headers: Optional[Dict[str, str]] = None
|
||||||
limits: Optional[DocumentLimits] = DocumentLimits()
|
limits: Optional[DocumentLimits] = DocumentLimits()
|
||||||
|
|
||||||
def docs(
|
def docs(
|
||||||
self, format_options: Dict[InputFormat, "FormatOption"]
|
self, format_options: Dict[InputFormat, "FormatOption"]
|
||||||
) -> Iterable[InputDocument]:
|
) -> Iterable[InputDocument]:
|
||||||
for item in self.path_or_stream_iterator:
|
for item in self.path_or_stream_iterator:
|
||||||
obj = resolve_source_to_stream(item) if isinstance(item, str) else item
|
obj = (
|
||||||
|
resolve_source_to_stream(item, self.headers)
|
||||||
|
if isinstance(item, str)
|
||||||
|
else item
|
||||||
|
)
|
||||||
format = self._guess_format(obj)
|
format = self._guess_format(obj)
|
||||||
backend: Type[AbstractDocumentBackend]
|
backend: Type[AbstractDocumentBackend]
|
||||||
if format not in format_options.keys():
|
if format not in format_options.keys():
|
||||||
|
@ -1,17 +1,11 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import warnings
|
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Annotated, Any, Dict, List, Literal, Optional, Tuple, Type, Union
|
from typing import Any, List, Literal, Optional, Union
|
||||||
|
|
||||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
from pydantic import BaseModel, ConfigDict, Field, model_validator
|
||||||
from pydantic_settings import (
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
BaseSettings,
|
|
||||||
PydanticBaseSettingsSource,
|
|
||||||
SettingsConfigDict,
|
|
||||||
)
|
|
||||||
from typing_extensions import deprecated
|
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -139,7 +133,7 @@ class EasyOcrOptions(OcrOptions):
|
|||||||
|
|
||||||
use_gpu: Optional[bool] = None
|
use_gpu: Optional[bool] = None
|
||||||
|
|
||||||
confidence_threshold: float = 0.65
|
confidence_threshold: float = 0.5
|
||||||
|
|
||||||
model_storage_directory: Optional[str] = None
|
model_storage_directory: Optional[str] = None
|
||||||
recog_network: Optional[str] = "standard"
|
recog_network: Optional[str] = "standard"
|
||||||
@ -225,6 +219,8 @@ class PdfPipelineOptions(PipelineOptions):
|
|||||||
artifacts_path: Optional[Union[Path, str]] = None
|
artifacts_path: Optional[Union[Path, str]] = None
|
||||||
do_table_structure: bool = True # True: perform table structure extraction
|
do_table_structure: bool = True # True: perform table structure extraction
|
||||||
do_ocr: bool = True # True: perform OCR, replace programmatic PDF text
|
do_ocr: bool = True # True: perform OCR, replace programmatic PDF text
|
||||||
|
do_code_enrichment: bool = False # True: perform code OCR
|
||||||
|
do_formula_enrichment: bool = False # True: perform formula OCR, return Latex code
|
||||||
|
|
||||||
table_structure_options: TableStructureOptions = TableStructureOptions()
|
table_structure_options: TableStructureOptions = TableStructureOptions()
|
||||||
ocr_options: Union[
|
ocr_options: Union[
|
||||||
|
@ -176,6 +176,7 @@ class DocumentConverter:
|
|||||||
def convert(
|
def convert(
|
||||||
self,
|
self,
|
||||||
source: Union[Path, str, DocumentStream], # TODO review naming
|
source: Union[Path, str, DocumentStream], # TODO review naming
|
||||||
|
headers: Optional[Dict[str, str]] = None,
|
||||||
raises_on_error: bool = True,
|
raises_on_error: bool = True,
|
||||||
max_num_pages: int = sys.maxsize,
|
max_num_pages: int = sys.maxsize,
|
||||||
max_file_size: int = sys.maxsize,
|
max_file_size: int = sys.maxsize,
|
||||||
@ -185,6 +186,7 @@ class DocumentConverter:
|
|||||||
raises_on_error=raises_on_error,
|
raises_on_error=raises_on_error,
|
||||||
max_num_pages=max_num_pages,
|
max_num_pages=max_num_pages,
|
||||||
max_file_size=max_file_size,
|
max_file_size=max_file_size,
|
||||||
|
headers=headers,
|
||||||
)
|
)
|
||||||
return next(all_res)
|
return next(all_res)
|
||||||
|
|
||||||
@ -192,6 +194,7 @@ class DocumentConverter:
|
|||||||
def convert_all(
|
def convert_all(
|
||||||
self,
|
self,
|
||||||
source: Iterable[Union[Path, str, DocumentStream]], # TODO review naming
|
source: Iterable[Union[Path, str, DocumentStream]], # TODO review naming
|
||||||
|
headers: Optional[Dict[str, str]] = None,
|
||||||
raises_on_error: bool = True, # True: raises on first conversion error; False: does not raise on conv error
|
raises_on_error: bool = True, # True: raises on first conversion error; False: does not raise on conv error
|
||||||
max_num_pages: int = sys.maxsize,
|
max_num_pages: int = sys.maxsize,
|
||||||
max_file_size: int = sys.maxsize,
|
max_file_size: int = sys.maxsize,
|
||||||
@ -201,8 +204,7 @@ class DocumentConverter:
|
|||||||
max_file_size=max_file_size,
|
max_file_size=max_file_size,
|
||||||
)
|
)
|
||||||
conv_input = _DocumentConversionInput(
|
conv_input = _DocumentConversionInput(
|
||||||
path_or_stream_iterator=source,
|
path_or_stream_iterator=source, limits=limits, headers=headers
|
||||||
limits=limits,
|
|
||||||
)
|
)
|
||||||
conv_res_iter = self._convert(conv_input, raises_on_error=raises_on_error)
|
conv_res_iter = self._convert(conv_input, raises_on_error=raises_on_error)
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Any, Iterable
|
from typing import Any, Generic, Iterable, Optional
|
||||||
|
|
||||||
from docling_core.types.doc import DoclingDocument, NodeItem
|
from docling_core.types.doc import BoundingBox, DoclingDocument, NodeItem, TextItem
|
||||||
|
from typing_extensions import TypeVar
|
||||||
|
|
||||||
from docling.datamodel.base_models import Page
|
from docling.datamodel.base_models import ItemAndImageEnrichmentElement, Page
|
||||||
from docling.datamodel.document import ConversionResult
|
from docling.datamodel.document import ConversionResult
|
||||||
|
|
||||||
|
|
||||||
@ -15,14 +16,69 @@ class BasePageModel(ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class BaseEnrichmentModel(ABC):
|
EnrichElementT = TypeVar("EnrichElementT", default=NodeItem)
|
||||||
|
|
||||||
|
|
||||||
|
class GenericEnrichmentModel(ABC, Generic[EnrichElementT]):
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def is_processable(self, doc: DoclingDocument, element: NodeItem) -> bool:
|
def is_processable(self, doc: DoclingDocument, element: NodeItem) -> bool:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def __call__(
|
def prepare_element(
|
||||||
self, doc: DoclingDocument, element_batch: Iterable[NodeItem]
|
self, conv_res: ConversionResult, element: NodeItem
|
||||||
) -> Iterable[Any]:
|
) -> Optional[EnrichElementT]:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __call__(
|
||||||
|
self, doc: DoclingDocument, element_batch: Iterable[EnrichElementT]
|
||||||
|
) -> Iterable[NodeItem]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BaseEnrichmentModel(GenericEnrichmentModel[NodeItem]):
|
||||||
|
|
||||||
|
def prepare_element(
|
||||||
|
self, conv_res: ConversionResult, element: NodeItem
|
||||||
|
) -> Optional[NodeItem]:
|
||||||
|
if self.is_processable(doc=conv_res.document, element=element):
|
||||||
|
return element
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class BaseItemAndImageEnrichmentModel(
|
||||||
|
GenericEnrichmentModel[ItemAndImageEnrichmentElement]
|
||||||
|
):
|
||||||
|
|
||||||
|
images_scale: float
|
||||||
|
expansion_factor: float = 0.0
|
||||||
|
|
||||||
|
def prepare_element(
|
||||||
|
self, conv_res: ConversionResult, element: NodeItem
|
||||||
|
) -> Optional[ItemAndImageEnrichmentElement]:
|
||||||
|
if not self.is_processable(doc=conv_res.document, element=element):
|
||||||
|
return None
|
||||||
|
|
||||||
|
assert isinstance(element, TextItem)
|
||||||
|
element_prov = element.prov[0]
|
||||||
|
|
||||||
|
bbox = element_prov.bbox
|
||||||
|
width = bbox.r - bbox.l
|
||||||
|
height = bbox.t - bbox.b
|
||||||
|
|
||||||
|
# TODO: move to a utility in the BoundingBox class
|
||||||
|
expanded_bbox = BoundingBox(
|
||||||
|
l=bbox.l - width * self.expansion_factor,
|
||||||
|
t=bbox.t + height * self.expansion_factor,
|
||||||
|
r=bbox.r + width * self.expansion_factor,
|
||||||
|
b=bbox.b - height * self.expansion_factor,
|
||||||
|
coord_origin=bbox.coord_origin,
|
||||||
|
)
|
||||||
|
|
||||||
|
page_ix = element_prov.page_no - 1
|
||||||
|
cropped_image = conv_res.pages[page_ix].get_image(
|
||||||
|
scale=self.images_scale, cropbox=expanded_bbox
|
||||||
|
)
|
||||||
|
return ItemAndImageEnrichmentElement(item=element, image=cropped_image)
|
||||||
|
@ -8,7 +8,7 @@ import numpy as np
|
|||||||
from docling_core.types.doc import BoundingBox, CoordOrigin
|
from docling_core.types.doc import BoundingBox, CoordOrigin
|
||||||
from PIL import Image, ImageDraw
|
from PIL import Image, ImageDraw
|
||||||
from rtree import index
|
from rtree import index
|
||||||
from scipy.ndimage import find_objects, label
|
from scipy.ndimage import binary_dilation, find_objects, label
|
||||||
|
|
||||||
from docling.datamodel.base_models import Cell, OcrCell, Page
|
from docling.datamodel.base_models import Cell, OcrCell, Page
|
||||||
from docling.datamodel.document import ConversionResult
|
from docling.datamodel.document import ConversionResult
|
||||||
@ -43,6 +43,12 @@ class BaseOcrModel(BasePageModel):
|
|||||||
|
|
||||||
np_image = np.array(image)
|
np_image = np.array(image)
|
||||||
|
|
||||||
|
# Dilate the image by 10 pixels to merge nearby bitmap rectangles
|
||||||
|
structure = np.ones(
|
||||||
|
(20, 20)
|
||||||
|
) # Create a 20x20 structure element (10 pixels in all directions)
|
||||||
|
np_image = binary_dilation(np_image > 0, structure=structure)
|
||||||
|
|
||||||
# Find the connected components
|
# Find the connected components
|
||||||
labeled_image, num_features = label(
|
labeled_image, num_features = label(
|
||||||
np_image > 0
|
np_image > 0
|
||||||
@ -72,7 +78,7 @@ class BaseOcrModel(BasePageModel):
|
|||||||
bitmap_rects = []
|
bitmap_rects = []
|
||||||
coverage, ocr_rects = find_ocr_rects(page.size, bitmap_rects)
|
coverage, ocr_rects = find_ocr_rects(page.size, bitmap_rects)
|
||||||
|
|
||||||
# return full-page rectangle if sufficiently covered with bitmaps
|
# return full-page rectangle if page is dominantly covered with bitmaps
|
||||||
if self.options.force_full_page_ocr or coverage > max(
|
if self.options.force_full_page_ocr or coverage > max(
|
||||||
BITMAP_COVERAGE_TRESHOLD, self.options.bitmap_area_threshold
|
BITMAP_COVERAGE_TRESHOLD, self.options.bitmap_area_threshold
|
||||||
):
|
):
|
||||||
@ -85,17 +91,11 @@ class BaseOcrModel(BasePageModel):
|
|||||||
coord_origin=CoordOrigin.TOPLEFT,
|
coord_origin=CoordOrigin.TOPLEFT,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
# return individual rectangles if the bitmap coverage is smaller
|
# return individual rectangles if the bitmap coverage is above the threshold
|
||||||
else: # coverage <= BITMAP_COVERAGE_TRESHOLD:
|
elif coverage > self.options.bitmap_area_threshold:
|
||||||
|
|
||||||
# skip OCR if the bitmap area on the page is smaller than the options threshold
|
|
||||||
ocr_rects = [
|
|
||||||
rect
|
|
||||||
for rect in ocr_rects
|
|
||||||
if rect.area() / (page.size.width * page.size.height)
|
|
||||||
> self.options.bitmap_area_threshold
|
|
||||||
]
|
|
||||||
return ocr_rects
|
return ocr_rects
|
||||||
|
else: # overall coverage of bitmaps is too low, drop all bitmap rectangles.
|
||||||
|
return []
|
||||||
|
|
||||||
# Filters OCR cells by dropping any OCR cell that intersects with an existing programmatic cell.
|
# Filters OCR cells by dropping any OCR cell that intersects with an existing programmatic cell.
|
||||||
def _filter_ocr_cells(self, ocr_cells, programmatic_cells):
|
def _filter_ocr_cells(self, ocr_cells, programmatic_cells):
|
||||||
@ -138,18 +138,34 @@ class BaseOcrModel(BasePageModel):
|
|||||||
|
|
||||||
def draw_ocr_rects_and_cells(self, conv_res, page, ocr_rects, show: bool = False):
|
def draw_ocr_rects_and_cells(self, conv_res, page, ocr_rects, show: bool = False):
|
||||||
image = copy.deepcopy(page.image)
|
image = copy.deepcopy(page.image)
|
||||||
|
scale_x = image.width / page.size.width
|
||||||
|
scale_y = image.height / page.size.height
|
||||||
|
|
||||||
draw = ImageDraw.Draw(image, "RGBA")
|
draw = ImageDraw.Draw(image, "RGBA")
|
||||||
|
|
||||||
# Draw OCR rectangles as yellow filled rect
|
# Draw OCR rectangles as yellow filled rect
|
||||||
for rect in ocr_rects:
|
for rect in ocr_rects:
|
||||||
x0, y0, x1, y1 = rect.as_tuple()
|
x0, y0, x1, y1 = rect.as_tuple()
|
||||||
|
y0 *= scale_x
|
||||||
|
y1 *= scale_y
|
||||||
|
x0 *= scale_x
|
||||||
|
x1 *= scale_x
|
||||||
|
|
||||||
shade_color = (255, 255, 0, 40) # transparent yellow
|
shade_color = (255, 255, 0, 40) # transparent yellow
|
||||||
draw.rectangle([(x0, y0), (x1, y1)], fill=shade_color, outline=None)
|
draw.rectangle([(x0, y0), (x1, y1)], fill=shade_color, outline=None)
|
||||||
|
|
||||||
# Draw OCR and programmatic cells
|
# Draw OCR and programmatic cells
|
||||||
for tc in page.cells:
|
for tc in page.cells:
|
||||||
x0, y0, x1, y1 = tc.bbox.as_tuple()
|
x0, y0, x1, y1 = tc.bbox.as_tuple()
|
||||||
color = "red"
|
y0 *= scale_x
|
||||||
|
y1 *= scale_y
|
||||||
|
x0 *= scale_x
|
||||||
|
x1 *= scale_x
|
||||||
|
|
||||||
|
if y1 <= y0:
|
||||||
|
y1, y0 = y0, y1
|
||||||
|
|
||||||
|
color = "gray"
|
||||||
if isinstance(tc, OcrCell):
|
if isinstance(tc, OcrCell):
|
||||||
color = "magenta"
|
color = "magenta"
|
||||||
draw.rectangle([(x0, y0), (x1, y1)], outline=color)
|
draw.rectangle([(x0, y0), (x1, y1)], outline=color)
|
||||||
|
245
docling/models/code_formula_model.py
Normal file
245
docling/models/code_formula_model.py
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
import re
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Iterable, List, Literal, Optional, Tuple, Union
|
||||||
|
|
||||||
|
from docling_core.types.doc import (
|
||||||
|
CodeItem,
|
||||||
|
DocItemLabel,
|
||||||
|
DoclingDocument,
|
||||||
|
NodeItem,
|
||||||
|
TextItem,
|
||||||
|
)
|
||||||
|
from docling_core.types.doc.labels import CodeLanguageLabel
|
||||||
|
from PIL import Image
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from docling.datamodel.base_models import ItemAndImageEnrichmentElement
|
||||||
|
from docling.datamodel.pipeline_options import AcceleratorOptions
|
||||||
|
from docling.models.base_model import BaseItemAndImageEnrichmentModel
|
||||||
|
from docling.utils.accelerator_utils import decide_device
|
||||||
|
|
||||||
|
|
||||||
|
class CodeFormulaModelOptions(BaseModel):
|
||||||
|
"""
|
||||||
|
Configuration options for the CodeFormulaModel.
|
||||||
|
|
||||||
|
Attributes
|
||||||
|
----------
|
||||||
|
kind : str
|
||||||
|
Type of the model. Fixed value "code_formula".
|
||||||
|
do_code_enrichment : bool
|
||||||
|
True if code enrichment is enabled, False otherwise.
|
||||||
|
do_formula_enrichment : bool
|
||||||
|
True if formula enrichment is enabled, False otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
|
kind: Literal["code_formula"] = "code_formula"
|
||||||
|
do_code_enrichment: bool = True
|
||||||
|
do_formula_enrichment: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
class CodeFormulaModel(BaseItemAndImageEnrichmentModel):
|
||||||
|
"""
|
||||||
|
Model for processing and enriching documents with code and formula predictions.
|
||||||
|
|
||||||
|
Attributes
|
||||||
|
----------
|
||||||
|
enabled : bool
|
||||||
|
True if the model is enabled, False otherwise.
|
||||||
|
options : CodeFormulaModelOptions
|
||||||
|
Configuration options for the CodeFormulaModel.
|
||||||
|
code_formula_model : CodeFormulaPredictor
|
||||||
|
The predictor model for code and formula processing.
|
||||||
|
|
||||||
|
Methods
|
||||||
|
-------
|
||||||
|
__init__(self, enabled, artifacts_path, accelerator_options, code_formula_options)
|
||||||
|
Initializes the CodeFormulaModel with the given configuration options.
|
||||||
|
is_processable(self, doc, element)
|
||||||
|
Determines if a given element in a document can be processed by the model.
|
||||||
|
__call__(self, doc, element_batch)
|
||||||
|
Processes the given batch of elements and enriches them with predictions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
images_scale = 1.66 # = 120 dpi, aligned with training data resolution
|
||||||
|
expansion_factor = 0.03
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
enabled: bool,
|
||||||
|
artifacts_path: Optional[Union[Path, str]],
|
||||||
|
options: CodeFormulaModelOptions,
|
||||||
|
accelerator_options: AcceleratorOptions,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Initializes the CodeFormulaModel with the given configuration.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
enabled : bool
|
||||||
|
True if the model is enabled, False otherwise.
|
||||||
|
artifacts_path : Path
|
||||||
|
Path to the directory containing the model artifacts.
|
||||||
|
options : CodeFormulaModelOptions
|
||||||
|
Configuration options for the model.
|
||||||
|
accelerator_options : AcceleratorOptions
|
||||||
|
Options specifying the device and number of threads for acceleration.
|
||||||
|
"""
|
||||||
|
self.enabled = enabled
|
||||||
|
self.options = options
|
||||||
|
|
||||||
|
if self.enabled:
|
||||||
|
device = decide_device(accelerator_options.device)
|
||||||
|
|
||||||
|
from docling_ibm_models.code_formula_model.code_formula_predictor import (
|
||||||
|
CodeFormulaPredictor,
|
||||||
|
)
|
||||||
|
|
||||||
|
if artifacts_path is None:
|
||||||
|
artifacts_path = self.download_models_hf()
|
||||||
|
else:
|
||||||
|
artifacts_path = Path(artifacts_path)
|
||||||
|
|
||||||
|
self.code_formula_model = CodeFormulaPredictor(
|
||||||
|
artifacts_path=artifacts_path,
|
||||||
|
device=device,
|
||||||
|
num_threads=accelerator_options.num_threads,
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def download_models_hf(
|
||||||
|
local_dir: Optional[Path] = None, force: bool = False
|
||||||
|
) -> Path:
|
||||||
|
from huggingface_hub import snapshot_download
|
||||||
|
from huggingface_hub.utils import disable_progress_bars
|
||||||
|
|
||||||
|
disable_progress_bars()
|
||||||
|
download_path = snapshot_download(
|
||||||
|
repo_id="ds4sd/CodeFormula",
|
||||||
|
force_download=force,
|
||||||
|
local_dir=local_dir,
|
||||||
|
revision="v1.0.0",
|
||||||
|
)
|
||||||
|
|
||||||
|
return Path(download_path)
|
||||||
|
|
||||||
|
def is_processable(self, doc: DoclingDocument, element: NodeItem) -> bool:
|
||||||
|
"""
|
||||||
|
Determines if a given element in a document can be processed by the model.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
doc : DoclingDocument
|
||||||
|
The document being processed.
|
||||||
|
element : NodeItem
|
||||||
|
The element within the document to check.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
bool
|
||||||
|
True if the element can be processed, False otherwise.
|
||||||
|
"""
|
||||||
|
return self.enabled and (
|
||||||
|
(isinstance(element, CodeItem) and self.options.do_code_enrichment)
|
||||||
|
or (
|
||||||
|
isinstance(element, TextItem)
|
||||||
|
and element.label == DocItemLabel.FORMULA
|
||||||
|
and self.options.do_formula_enrichment
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _extract_code_language(self, input_string: str) -> Tuple[str, Optional[str]]:
|
||||||
|
"""Extracts a programming language from the beginning of a string.
|
||||||
|
|
||||||
|
This function checks if the input string starts with a pattern of the form
|
||||||
|
``<_some_language_>``. If it does, it extracts the language string and returns
|
||||||
|
a tuple of (remainder, language). Otherwise, it returns the original string
|
||||||
|
and `None`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input_string (str): The input string, which may start with ``<_language_>``.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple[str, Optional[str]]:
|
||||||
|
A tuple where:
|
||||||
|
- The first element is either:
|
||||||
|
- The remainder of the string (everything after ``<_language_>``),
|
||||||
|
if a match is found; or
|
||||||
|
- The original string, if no match is found.
|
||||||
|
- The second element is the extracted language if a match is found;
|
||||||
|
otherwise, `None`.
|
||||||
|
"""
|
||||||
|
pattern = r"^<_([^>]+)_>\s*(.*)"
|
||||||
|
match = re.match(pattern, input_string, flags=re.DOTALL)
|
||||||
|
if match:
|
||||||
|
language = str(match.group(1)) # the captured programming language
|
||||||
|
remainder = str(match.group(2)) # everything after the <_language_>
|
||||||
|
return remainder, language
|
||||||
|
else:
|
||||||
|
return input_string, None
|
||||||
|
|
||||||
|
def _get_code_language_enum(self, value: Optional[str]) -> CodeLanguageLabel:
|
||||||
|
"""
|
||||||
|
Converts a string to a corresponding `CodeLanguageLabel` enum member.
|
||||||
|
|
||||||
|
If the provided string does not match any value in `CodeLanguageLabel`,
|
||||||
|
it defaults to `CodeLanguageLabel.UNKNOWN`.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value (Optional[str]): The string representation of the code language or None.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
CodeLanguageLabel: The corresponding enum member if the value is valid,
|
||||||
|
otherwise `CodeLanguageLabel.UNKNOWN`.
|
||||||
|
"""
|
||||||
|
if not isinstance(value, str):
|
||||||
|
return CodeLanguageLabel.UNKNOWN
|
||||||
|
|
||||||
|
try:
|
||||||
|
return CodeLanguageLabel(value)
|
||||||
|
except ValueError:
|
||||||
|
return CodeLanguageLabel.UNKNOWN
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
doc: DoclingDocument,
|
||||||
|
element_batch: Iterable[ItemAndImageEnrichmentElement],
|
||||||
|
) -> Iterable[NodeItem]:
|
||||||
|
"""
|
||||||
|
Processes the given batch of elements and enriches them with predictions.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
doc : DoclingDocument
|
||||||
|
The document being processed.
|
||||||
|
element_batch : Iterable[ItemAndImageEnrichmentElement]
|
||||||
|
A batch of elements to be processed.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
Iterable[Any]
|
||||||
|
An iterable of enriched elements.
|
||||||
|
"""
|
||||||
|
if not self.enabled:
|
||||||
|
for element in element_batch:
|
||||||
|
yield element.item
|
||||||
|
return
|
||||||
|
|
||||||
|
labels: List[str] = []
|
||||||
|
images: List[Image.Image] = []
|
||||||
|
elements: List[TextItem] = []
|
||||||
|
for el in element_batch:
|
||||||
|
assert isinstance(el.item, TextItem)
|
||||||
|
elements.append(el.item)
|
||||||
|
labels.append(el.item.label)
|
||||||
|
images.append(el.image)
|
||||||
|
|
||||||
|
outputs = self.code_formula_model.predict(images, labels)
|
||||||
|
|
||||||
|
for item, output in zip(elements, outputs):
|
||||||
|
if isinstance(item, CodeItem):
|
||||||
|
output, code_language = self._extract_code_language(output)
|
||||||
|
item.code_language = self._get_code_language_enum(code_language)
|
||||||
|
item.text = output
|
||||||
|
|
||||||
|
yield item
|
@ -1,28 +1,21 @@
|
|||||||
import copy
|
import copy
|
||||||
import logging
|
import logging
|
||||||
import random
|
|
||||||
import time
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Iterable, List
|
from typing import Iterable
|
||||||
|
|
||||||
from docling_core.types.doc import CoordOrigin, DocItemLabel
|
from docling_core.types.doc import DocItemLabel
|
||||||
from docling_ibm_models.layoutmodel.layout_predictor import LayoutPredictor
|
from docling_ibm_models.layoutmodel.layout_predictor import LayoutPredictor
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image
|
||||||
|
|
||||||
from docling.datamodel.base_models import (
|
from docling.datamodel.base_models import BoundingBox, Cluster, LayoutPrediction, Page
|
||||||
BoundingBox,
|
|
||||||
Cell,
|
|
||||||
Cluster,
|
|
||||||
LayoutPrediction,
|
|
||||||
Page,
|
|
||||||
)
|
|
||||||
from docling.datamodel.document import ConversionResult
|
from docling.datamodel.document import ConversionResult
|
||||||
from docling.datamodel.pipeline_options import AcceleratorDevice, AcceleratorOptions
|
from docling.datamodel.pipeline_options import AcceleratorOptions
|
||||||
from docling.datamodel.settings import settings
|
from docling.datamodel.settings import settings
|
||||||
from docling.models.base_model import BasePageModel
|
from docling.models.base_model import BasePageModel
|
||||||
from docling.utils.accelerator_utils import decide_device
|
from docling.utils.accelerator_utils import decide_device
|
||||||
from docling.utils.layout_postprocessor import LayoutPostprocessor
|
from docling.utils.layout_postprocessor import LayoutPostprocessor
|
||||||
from docling.utils.profiling import TimeRecorder
|
from docling.utils.profiling import TimeRecorder
|
||||||
|
from docling.utils.visualization import draw_clusters
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -40,7 +33,7 @@ class LayoutModel(BasePageModel):
|
|||||||
DocItemLabel.PAGE_FOOTER,
|
DocItemLabel.PAGE_FOOTER,
|
||||||
DocItemLabel.CODE,
|
DocItemLabel.CODE,
|
||||||
DocItemLabel.LIST_ITEM,
|
DocItemLabel.LIST_ITEM,
|
||||||
# "Formula",
|
DocItemLabel.FORMULA,
|
||||||
]
|
]
|
||||||
PAGE_HEADER_LABELS = [DocItemLabel.PAGE_HEADER, DocItemLabel.PAGE_FOOTER]
|
PAGE_HEADER_LABELS = [DocItemLabel.PAGE_HEADER, DocItemLabel.PAGE_FOOTER]
|
||||||
|
|
||||||
@ -67,29 +60,9 @@ class LayoutModel(BasePageModel):
|
|||||||
- Right: Clusters including FORM, KEY_VALUE_REGION, and PICTURE.
|
- Right: Clusters including FORM, KEY_VALUE_REGION, and PICTURE.
|
||||||
Includes label names and confidence scores for each cluster.
|
Includes label names and confidence scores for each cluster.
|
||||||
"""
|
"""
|
||||||
label_to_color = {
|
scale_x = page.image.width / page.size.width
|
||||||
DocItemLabel.TEXT: (255, 255, 153), # Light Yellow
|
scale_y = page.image.height / page.size.height
|
||||||
DocItemLabel.CAPTION: (255, 204, 153), # Light Orange
|
|
||||||
DocItemLabel.LIST_ITEM: (153, 153, 255), # Light Purple
|
|
||||||
DocItemLabel.FORMULA: (192, 192, 192), # Gray
|
|
||||||
DocItemLabel.TABLE: (255, 204, 204), # Light Pink
|
|
||||||
DocItemLabel.PICTURE: (255, 204, 164), # Light Beige
|
|
||||||
DocItemLabel.SECTION_HEADER: (255, 153, 153), # Light Red
|
|
||||||
DocItemLabel.PAGE_HEADER: (204, 255, 204), # Light Green
|
|
||||||
DocItemLabel.PAGE_FOOTER: (
|
|
||||||
204,
|
|
||||||
255,
|
|
||||||
204,
|
|
||||||
), # Light Green (same as Page-Header)
|
|
||||||
DocItemLabel.TITLE: (255, 153, 153), # Light Red (same as Section-Header)
|
|
||||||
DocItemLabel.FOOTNOTE: (200, 200, 255), # Light Blue
|
|
||||||
DocItemLabel.DOCUMENT_INDEX: (220, 220, 220), # Light Gray
|
|
||||||
DocItemLabel.CODE: (125, 125, 125), # Gray
|
|
||||||
DocItemLabel.CHECKBOX_SELECTED: (255, 182, 193), # Pale Green
|
|
||||||
DocItemLabel.CHECKBOX_UNSELECTED: (255, 182, 193), # Light Pink
|
|
||||||
DocItemLabel.FORM: (200, 255, 255), # Light Cyan
|
|
||||||
DocItemLabel.KEY_VALUE_REGION: (183, 65, 14), # Rusty orange
|
|
||||||
}
|
|
||||||
# Filter clusters for left and right images
|
# Filter clusters for left and right images
|
||||||
exclude_labels = {
|
exclude_labels = {
|
||||||
DocItemLabel.FORM,
|
DocItemLabel.FORM,
|
||||||
@ -102,65 +75,9 @@ class LayoutModel(BasePageModel):
|
|||||||
left_image = copy.deepcopy(page.image)
|
left_image = copy.deepcopy(page.image)
|
||||||
right_image = copy.deepcopy(page.image)
|
right_image = copy.deepcopy(page.image)
|
||||||
|
|
||||||
# Function to draw clusters on an image
|
|
||||||
def draw_clusters(image, clusters):
|
|
||||||
draw = ImageDraw.Draw(image, "RGBA")
|
|
||||||
# Create a smaller font for the labels
|
|
||||||
try:
|
|
||||||
font = ImageFont.truetype("arial.ttf", 12)
|
|
||||||
except OSError:
|
|
||||||
# Fallback to default font if arial is not available
|
|
||||||
font = ImageFont.load_default()
|
|
||||||
for c_tl in clusters:
|
|
||||||
all_clusters = [c_tl, *c_tl.children]
|
|
||||||
for c in all_clusters:
|
|
||||||
# Draw cells first (underneath)
|
|
||||||
cell_color = (0, 0, 0, 40) # Transparent black for cells
|
|
||||||
for tc in c.cells:
|
|
||||||
cx0, cy0, cx1, cy1 = tc.bbox.as_tuple()
|
|
||||||
draw.rectangle(
|
|
||||||
[(cx0, cy0), (cx1, cy1)],
|
|
||||||
outline=None,
|
|
||||||
fill=cell_color,
|
|
||||||
)
|
|
||||||
# Draw cluster rectangle
|
|
||||||
x0, y0, x1, y1 = c.bbox.as_tuple()
|
|
||||||
cluster_fill_color = (*list(label_to_color.get(c.label)), 70)
|
|
||||||
cluster_outline_color = (*list(label_to_color.get(c.label)), 255)
|
|
||||||
draw.rectangle(
|
|
||||||
[(x0, y0), (x1, y1)],
|
|
||||||
outline=cluster_outline_color,
|
|
||||||
fill=cluster_fill_color,
|
|
||||||
)
|
|
||||||
# Add label name and confidence
|
|
||||||
label_text = f"{c.label.name} ({c.confidence:.2f})"
|
|
||||||
# Create semi-transparent background for text
|
|
||||||
text_bbox = draw.textbbox((x0, y0), label_text, font=font)
|
|
||||||
text_bg_padding = 2
|
|
||||||
draw.rectangle(
|
|
||||||
[
|
|
||||||
(
|
|
||||||
text_bbox[0] - text_bg_padding,
|
|
||||||
text_bbox[1] - text_bg_padding,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
text_bbox[2] + text_bg_padding,
|
|
||||||
text_bbox[3] + text_bg_padding,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
fill=(255, 255, 255, 180), # Semi-transparent white
|
|
||||||
)
|
|
||||||
# Draw text
|
|
||||||
draw.text(
|
|
||||||
(x0, y0),
|
|
||||||
label_text,
|
|
||||||
fill=(0, 0, 0, 255), # Solid black
|
|
||||||
font=font,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Draw clusters on both images
|
# Draw clusters on both images
|
||||||
draw_clusters(left_image, left_clusters)
|
draw_clusters(left_image, left_clusters, scale_x, scale_y)
|
||||||
draw_clusters(right_image, right_clusters)
|
draw_clusters(right_image, right_clusters, scale_x, scale_y)
|
||||||
# Combine the images side by side
|
# Combine the images side by side
|
||||||
combined_width = left_image.width * 2
|
combined_width = left_image.width * 2
|
||||||
combined_height = left_image.height
|
combined_height = left_image.height
|
||||||
|
@ -22,7 +22,7 @@ _log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class PageAssembleOptions(BaseModel):
|
class PageAssembleOptions(BaseModel):
|
||||||
keep_images: bool = False
|
pass
|
||||||
|
|
||||||
|
|
||||||
class PageAssembleModel(BasePageModel):
|
class PageAssembleModel(BasePageModel):
|
||||||
@ -135,31 +135,6 @@ class PageAssembleModel(BasePageModel):
|
|||||||
)
|
)
|
||||||
elements.append(fig)
|
elements.append(fig)
|
||||||
body.append(fig)
|
body.append(fig)
|
||||||
elif cluster.label == LayoutModel.FORMULA_LABEL:
|
|
||||||
equation = None
|
|
||||||
if page.predictions.equations_prediction:
|
|
||||||
equation = page.predictions.equations_prediction.equation_map.get(
|
|
||||||
cluster.id, None
|
|
||||||
)
|
|
||||||
if (
|
|
||||||
not equation
|
|
||||||
): # fallback: add empty formula, if it isn't present
|
|
||||||
text = self.sanitize_text(
|
|
||||||
[
|
|
||||||
cell.text.replace("\x02", "-").strip()
|
|
||||||
for cell in cluster.cells
|
|
||||||
if len(cell.text.strip()) > 0
|
|
||||||
]
|
|
||||||
)
|
|
||||||
equation = TextElement(
|
|
||||||
label=cluster.label,
|
|
||||||
id=cluster.id,
|
|
||||||
cluster=cluster,
|
|
||||||
page_no=page.page_no,
|
|
||||||
text=text,
|
|
||||||
)
|
|
||||||
elements.append(equation)
|
|
||||||
body.append(equation)
|
|
||||||
elif cluster.label in LayoutModel.CONTAINER_LABELS:
|
elif cluster.label in LayoutModel.CONTAINER_LABELS:
|
||||||
container_el = ContainerElement(
|
container_el = ContainerElement(
|
||||||
label=cluster.label,
|
label=cluster.label,
|
||||||
@ -174,11 +149,4 @@ class PageAssembleModel(BasePageModel):
|
|||||||
elements=elements, headers=headers, body=body
|
elements=elements, headers=headers, body=body
|
||||||
)
|
)
|
||||||
|
|
||||||
# Remove page images (can be disabled)
|
|
||||||
if not self.options.keep_images:
|
|
||||||
page._image_cache = {}
|
|
||||||
|
|
||||||
# Unload backend
|
|
||||||
page._backend.unload()
|
|
||||||
|
|
||||||
yield page
|
yield page
|
||||||
|
@ -66,23 +66,43 @@ class TableStructureModel(BasePageModel):
|
|||||||
show: bool = False,
|
show: bool = False,
|
||||||
):
|
):
|
||||||
assert page._backend is not None
|
assert page._backend is not None
|
||||||
|
assert page.size is not None
|
||||||
|
|
||||||
image = (
|
image = (
|
||||||
page._backend.get_page_image()
|
page._backend.get_page_image()
|
||||||
) # make new image to avoid drawing on the saved ones
|
) # make new image to avoid drawing on the saved ones
|
||||||
|
|
||||||
|
scale_x = image.width / page.size.width
|
||||||
|
scale_y = image.height / page.size.height
|
||||||
|
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
|
|
||||||
for table_element in tbl_list:
|
for table_element in tbl_list:
|
||||||
x0, y0, x1, y1 = table_element.cluster.bbox.as_tuple()
|
x0, y0, x1, y1 = table_element.cluster.bbox.as_tuple()
|
||||||
|
y0 *= scale_x
|
||||||
|
y1 *= scale_y
|
||||||
|
x0 *= scale_x
|
||||||
|
x1 *= scale_x
|
||||||
|
|
||||||
draw.rectangle([(x0, y0), (x1, y1)], outline="red")
|
draw.rectangle([(x0, y0), (x1, y1)], outline="red")
|
||||||
|
|
||||||
for cell in table_element.cluster.cells:
|
for cell in table_element.cluster.cells:
|
||||||
x0, y0, x1, y1 = cell.bbox.as_tuple()
|
x0, y0, x1, y1 = cell.bbox.as_tuple()
|
||||||
|
x0 *= scale_x
|
||||||
|
x1 *= scale_x
|
||||||
|
y0 *= scale_x
|
||||||
|
y1 *= scale_y
|
||||||
|
|
||||||
draw.rectangle([(x0, y0), (x1, y1)], outline="green")
|
draw.rectangle([(x0, y0), (x1, y1)], outline="green")
|
||||||
|
|
||||||
for tc in table_element.table_cells:
|
for tc in table_element.table_cells:
|
||||||
if tc.bbox is not None:
|
if tc.bbox is not None:
|
||||||
x0, y0, x1, y1 = tc.bbox.as_tuple()
|
x0, y0, x1, y1 = tc.bbox.as_tuple()
|
||||||
|
x0 *= scale_x
|
||||||
|
x1 *= scale_x
|
||||||
|
y0 *= scale_x
|
||||||
|
y1 *= scale_y
|
||||||
|
|
||||||
if tc.column_header:
|
if tc.column_header:
|
||||||
width = 3
|
width = 3
|
||||||
else:
|
else:
|
||||||
|
@ -20,7 +20,6 @@ _log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class TesseractOcrCliModel(BaseOcrModel):
|
class TesseractOcrCliModel(BaseOcrModel):
|
||||||
|
|
||||||
def __init__(self, enabled: bool, options: TesseractCliOcrOptions):
|
def __init__(self, enabled: bool, options: TesseractCliOcrOptions):
|
||||||
super().__init__(enabled=enabled, options=options)
|
super().__init__(enabled=enabled, options=options)
|
||||||
self.options: TesseractCliOcrOptions
|
self.options: TesseractCliOcrOptions
|
||||||
|
@ -54,43 +54,56 @@ class TesseractOcrModel(BaseOcrModel):
|
|||||||
# Initialize the tesseractAPI
|
# Initialize the tesseractAPI
|
||||||
_log.debug("Initializing TesserOCR: %s", tesseract_version)
|
_log.debug("Initializing TesserOCR: %s", tesseract_version)
|
||||||
lang = "+".join(self.options.lang)
|
lang = "+".join(self.options.lang)
|
||||||
|
|
||||||
|
self.script_readers: dict[str, tesserocr.PyTessBaseAPI] = {}
|
||||||
|
|
||||||
|
if any([l.startswith("script/") for l in tesserocr_languages]):
|
||||||
|
self.script_prefix = "script/"
|
||||||
|
else:
|
||||||
|
self.script_prefix = ""
|
||||||
|
|
||||||
|
tesserocr_kwargs = {
|
||||||
|
"psm": tesserocr.PSM.AUTO,
|
||||||
|
"init": True,
|
||||||
|
"oem": tesserocr.OEM.DEFAULT,
|
||||||
|
}
|
||||||
|
|
||||||
if self.options.path is not None:
|
if self.options.path is not None:
|
||||||
|
tesserocr_kwargs["path"] = self.options.path
|
||||||
|
|
||||||
|
if lang == "auto":
|
||||||
self.reader = tesserocr.PyTessBaseAPI(
|
self.reader = tesserocr.PyTessBaseAPI(
|
||||||
path=self.options.path,
|
**{"lang": "osd", "psm": tesserocr.PSM.OSD_ONLY} | tesserocr_kwargs
|
||||||
lang=lang,
|
|
||||||
psm=tesserocr.PSM.AUTO,
|
|
||||||
init=True,
|
|
||||||
oem=tesserocr.OEM.DEFAULT,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.reader = tesserocr.PyTessBaseAPI(
|
self.reader = tesserocr.PyTessBaseAPI(
|
||||||
lang=lang,
|
**{"lang": lang} | tesserocr_kwargs,
|
||||||
psm=tesserocr.PSM.AUTO,
|
|
||||||
init=True,
|
|
||||||
oem=tesserocr.OEM.DEFAULT,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.reader_RIL = tesserocr.RIL
|
self.reader_RIL = tesserocr.RIL
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
if self.reader is not None:
|
if self.reader is not None:
|
||||||
# Finalize the tesseractAPI
|
# Finalize the tesseractAPI
|
||||||
self.reader.End()
|
self.reader.End()
|
||||||
|
for script in self.script_readers:
|
||||||
|
self.script_readers[script].End()
|
||||||
|
|
||||||
def __call__(
|
def __call__(
|
||||||
self, conv_res: ConversionResult, page_batch: Iterable[Page]
|
self, conv_res: ConversionResult, page_batch: Iterable[Page]
|
||||||
) -> Iterable[Page]:
|
) -> Iterable[Page]:
|
||||||
|
|
||||||
if not self.enabled:
|
if not self.enabled:
|
||||||
yield from page_batch
|
yield from page_batch
|
||||||
return
|
return
|
||||||
|
|
||||||
|
import tesserocr
|
||||||
|
|
||||||
for page in page_batch:
|
for page in page_batch:
|
||||||
assert page._backend is not None
|
assert page._backend is not None
|
||||||
if not page._backend.is_valid():
|
if not page._backend.is_valid():
|
||||||
yield page
|
yield page
|
||||||
else:
|
else:
|
||||||
with TimeRecorder(conv_res, "ocr"):
|
with TimeRecorder(conv_res, "ocr"):
|
||||||
|
|
||||||
assert self.reader is not None
|
assert self.reader is not None
|
||||||
|
|
||||||
ocr_rects = self.get_ocr_rects(page)
|
ocr_rects = self.get_ocr_rects(page)
|
||||||
@ -106,20 +119,55 @@ class TesseractOcrModel(BaseOcrModel):
|
|||||||
|
|
||||||
# Retrieve text snippets with their bounding boxes
|
# Retrieve text snippets with their bounding boxes
|
||||||
self.reader.SetImage(high_res_image)
|
self.reader.SetImage(high_res_image)
|
||||||
boxes = self.reader.GetComponentImages(
|
|
||||||
|
if self.options.lang == ["auto"]:
|
||||||
|
osd = self.reader.DetectOrientationScript()
|
||||||
|
|
||||||
|
# No text, probably
|
||||||
|
if osd is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
script = osd["script_name"]
|
||||||
|
|
||||||
|
if script == "Katakana" or script == "Hiragana":
|
||||||
|
script = "Japanese"
|
||||||
|
elif script == "Han":
|
||||||
|
script = "HanS"
|
||||||
|
elif script == "Korean":
|
||||||
|
script = "Hangul"
|
||||||
|
|
||||||
|
_log.debug(
|
||||||
|
f'Using model for the detected script "{script}"'
|
||||||
|
)
|
||||||
|
|
||||||
|
if script not in self.script_readers:
|
||||||
|
self.script_readers[script] = tesserocr.PyTessBaseAPI(
|
||||||
|
path=self.reader.GetDatapath(),
|
||||||
|
lang=f"{self.script_prefix}{script}",
|
||||||
|
psm=tesserocr.PSM.AUTO,
|
||||||
|
init=True,
|
||||||
|
oem=tesserocr.OEM.DEFAULT,
|
||||||
|
)
|
||||||
|
|
||||||
|
local_reader = self.script_readers[script]
|
||||||
|
local_reader.SetImage(high_res_image)
|
||||||
|
else:
|
||||||
|
local_reader = self.reader
|
||||||
|
|
||||||
|
boxes = local_reader.GetComponentImages(
|
||||||
self.reader_RIL.TEXTLINE, True
|
self.reader_RIL.TEXTLINE, True
|
||||||
)
|
)
|
||||||
|
|
||||||
cells = []
|
cells = []
|
||||||
for ix, (im, box, _, _) in enumerate(boxes):
|
for ix, (im, box, _, _) in enumerate(boxes):
|
||||||
# Set the area of interest. Tesseract uses Bottom-Left for the origin
|
# Set the area of interest. Tesseract uses Bottom-Left for the origin
|
||||||
self.reader.SetRectangle(
|
local_reader.SetRectangle(
|
||||||
box["x"], box["y"], box["w"], box["h"]
|
box["x"], box["y"], box["w"], box["h"]
|
||||||
)
|
)
|
||||||
|
|
||||||
# Extract text within the bounding box
|
# Extract text within the bounding box
|
||||||
text = self.reader.GetUTF8Text().strip()
|
text = local_reader.GetUTF8Text().strip()
|
||||||
confidence = self.reader.MeanTextConf()
|
confidence = local_reader.MeanTextConf()
|
||||||
left = box["x"] / self.scale
|
left = box["x"] / self.scale
|
||||||
bottom = box["y"] / self.scale
|
bottom = box["y"] / self.scale
|
||||||
right = (box["x"] + box["w"]) / self.scale
|
right = (box["x"] + box["w"]) / self.scale
|
||||||
|
@ -3,7 +3,7 @@ import logging
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Callable, Iterable, List
|
from typing import Any, Callable, Iterable, List
|
||||||
|
|
||||||
from docling_core.types.doc import DoclingDocument, NodeItem
|
from docling_core.types.doc import DoclingDocument, NodeItem
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ from docling.datamodel.base_models import (
|
|||||||
from docling.datamodel.document import ConversionResult, InputDocument
|
from docling.datamodel.document import ConversionResult, InputDocument
|
||||||
from docling.datamodel.pipeline_options import PipelineOptions
|
from docling.datamodel.pipeline_options import PipelineOptions
|
||||||
from docling.datamodel.settings import settings
|
from docling.datamodel.settings import settings
|
||||||
from docling.models.base_model import BaseEnrichmentModel
|
from docling.models.base_model import GenericEnrichmentModel
|
||||||
from docling.utils.profiling import ProfilingScope, TimeRecorder
|
from docling.utils.profiling import ProfilingScope, TimeRecorder
|
||||||
from docling.utils.utils import chunkify
|
from docling.utils.utils import chunkify
|
||||||
|
|
||||||
@ -28,8 +28,9 @@ _log = logging.getLogger(__name__)
|
|||||||
class BasePipeline(ABC):
|
class BasePipeline(ABC):
|
||||||
def __init__(self, pipeline_options: PipelineOptions):
|
def __init__(self, pipeline_options: PipelineOptions):
|
||||||
self.pipeline_options = pipeline_options
|
self.pipeline_options = pipeline_options
|
||||||
|
self.keep_images = False
|
||||||
self.build_pipe: List[Callable] = []
|
self.build_pipe: List[Callable] = []
|
||||||
self.enrichment_pipe: List[BaseEnrichmentModel] = []
|
self.enrichment_pipe: List[GenericEnrichmentModel[Any]] = []
|
||||||
|
|
||||||
def execute(self, in_doc: InputDocument, raises_on_error: bool) -> ConversionResult:
|
def execute(self, in_doc: InputDocument, raises_on_error: bool) -> ConversionResult:
|
||||||
conv_res = ConversionResult(input=in_doc)
|
conv_res = ConversionResult(input=in_doc)
|
||||||
@ -40,7 +41,7 @@ class BasePipeline(ABC):
|
|||||||
conv_res, "pipeline_total", scope=ProfilingScope.DOCUMENT
|
conv_res, "pipeline_total", scope=ProfilingScope.DOCUMENT
|
||||||
):
|
):
|
||||||
# These steps are building and assembling the structure of the
|
# These steps are building and assembling the structure of the
|
||||||
# output DoclingDocument
|
# output DoclingDocument.
|
||||||
conv_res = self._build_document(conv_res)
|
conv_res = self._build_document(conv_res)
|
||||||
conv_res = self._assemble_document(conv_res)
|
conv_res = self._assemble_document(conv_res)
|
||||||
# From this stage, all operations should rely only on conv_res.output
|
# From this stage, all operations should rely only on conv_res.output
|
||||||
@ -50,6 +51,8 @@ class BasePipeline(ABC):
|
|||||||
conv_res.status = ConversionStatus.FAILURE
|
conv_res.status = ConversionStatus.FAILURE
|
||||||
if raises_on_error:
|
if raises_on_error:
|
||||||
raise e
|
raise e
|
||||||
|
finally:
|
||||||
|
self._unload(conv_res)
|
||||||
|
|
||||||
return conv_res
|
return conv_res
|
||||||
|
|
||||||
@ -62,21 +65,22 @@ class BasePipeline(ABC):
|
|||||||
|
|
||||||
def _enrich_document(self, conv_res: ConversionResult) -> ConversionResult:
|
def _enrich_document(self, conv_res: ConversionResult) -> ConversionResult:
|
||||||
|
|
||||||
def _filter_elements(
|
def _prepare_elements(
|
||||||
doc: DoclingDocument, model: BaseEnrichmentModel
|
conv_res: ConversionResult, model: GenericEnrichmentModel[Any]
|
||||||
) -> Iterable[NodeItem]:
|
) -> Iterable[NodeItem]:
|
||||||
for element, _level in doc.iterate_items():
|
for doc_element, _level in conv_res.document.iterate_items():
|
||||||
if model.is_processable(doc=doc, element=element):
|
prepared_element = model.prepare_element(
|
||||||
yield element
|
conv_res=conv_res, element=doc_element
|
||||||
|
)
|
||||||
|
if prepared_element is not None:
|
||||||
|
yield prepared_element
|
||||||
|
|
||||||
with TimeRecorder(conv_res, "doc_enrich", scope=ProfilingScope.DOCUMENT):
|
with TimeRecorder(conv_res, "doc_enrich", scope=ProfilingScope.DOCUMENT):
|
||||||
for model in self.enrichment_pipe:
|
for model in self.enrichment_pipe:
|
||||||
for element_batch in chunkify(
|
for element_batch in chunkify(
|
||||||
_filter_elements(conv_res.document, model),
|
_prepare_elements(conv_res, model),
|
||||||
settings.perf.elements_batch_size,
|
settings.perf.elements_batch_size,
|
||||||
):
|
):
|
||||||
# TODO: currently we assume the element itself is modified, because
|
|
||||||
# we don't have an interface to save the element back to the document
|
|
||||||
for element in model(
|
for element in model(
|
||||||
doc=conv_res.document, element_batch=element_batch
|
doc=conv_res.document, element_batch=element_batch
|
||||||
): # Must exhaust!
|
): # Must exhaust!
|
||||||
@ -88,6 +92,9 @@ class BasePipeline(ABC):
|
|||||||
def _determine_status(self, conv_res: ConversionResult) -> ConversionStatus:
|
def _determine_status(self, conv_res: ConversionResult) -> ConversionStatus:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def _unload(self, conv_res: ConversionResult):
|
||||||
|
pass
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_default_options(cls) -> PipelineOptions:
|
def get_default_options(cls) -> PipelineOptions:
|
||||||
@ -107,6 +114,10 @@ class BasePipeline(ABC):
|
|||||||
|
|
||||||
class PaginatedPipeline(BasePipeline): # TODO this is a bad name.
|
class PaginatedPipeline(BasePipeline): # TODO this is a bad name.
|
||||||
|
|
||||||
|
def __init__(self, pipeline_options: PipelineOptions):
|
||||||
|
super().__init__(pipeline_options)
|
||||||
|
self.keep_backend = False
|
||||||
|
|
||||||
def _apply_on_pages(
|
def _apply_on_pages(
|
||||||
self, conv_res: ConversionResult, page_batch: Iterable[Page]
|
self, conv_res: ConversionResult, page_batch: Iterable[Page]
|
||||||
) -> Iterable[Page]:
|
) -> Iterable[Page]:
|
||||||
@ -148,7 +159,14 @@ class PaginatedPipeline(BasePipeline): # TODO this is a bad name.
|
|||||||
pipeline_pages = self._apply_on_pages(conv_res, init_pages)
|
pipeline_pages = self._apply_on_pages(conv_res, init_pages)
|
||||||
|
|
||||||
for p in pipeline_pages: # Must exhaust!
|
for p in pipeline_pages: # Must exhaust!
|
||||||
pass
|
|
||||||
|
# Cleanup cached images
|
||||||
|
if not self.keep_images:
|
||||||
|
p._image_cache = {}
|
||||||
|
|
||||||
|
# Cleanup page backends
|
||||||
|
if not self.keep_backend and p._backend is not None:
|
||||||
|
p._backend.unload()
|
||||||
|
|
||||||
end_batch_time = time.monotonic()
|
end_batch_time = time.monotonic()
|
||||||
total_elapsed_time += end_batch_time - start_batch_time
|
total_elapsed_time += end_batch_time - start_batch_time
|
||||||
@ -177,10 +195,15 @@ class PaginatedPipeline(BasePipeline): # TODO this is a bad name.
|
|||||||
)
|
)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
finally:
|
return conv_res
|
||||||
# Always unload the PDF backend, even in case of failure
|
|
||||||
if conv_res.input._backend:
|
def _unload(self, conv_res: ConversionResult) -> ConversionResult:
|
||||||
conv_res.input._backend.unload()
|
for page in conv_res.pages:
|
||||||
|
if page._backend is not None:
|
||||||
|
page._backend.unload()
|
||||||
|
|
||||||
|
if conv_res.input._backend:
|
||||||
|
conv_res.input._backend.unload()
|
||||||
|
|
||||||
return conv_res
|
return conv_res
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ from docling.datamodel.pipeline_options import (
|
|||||||
TesseractOcrOptions,
|
TesseractOcrOptions,
|
||||||
)
|
)
|
||||||
from docling.models.base_ocr_model import BaseOcrModel
|
from docling.models.base_ocr_model import BaseOcrModel
|
||||||
|
from docling.models.code_formula_model import CodeFormulaModel, CodeFormulaModelOptions
|
||||||
from docling.models.ds_glm_model import GlmModel, GlmOptions
|
from docling.models.ds_glm_model import GlmModel, GlmOptions
|
||||||
from docling.models.easyocr_model import EasyOcrModel
|
from docling.models.easyocr_model import EasyOcrModel
|
||||||
from docling.models.layout_model import LayoutModel
|
from docling.models.layout_model import LayoutModel
|
||||||
@ -50,7 +51,7 @@ class StandardPdfPipeline(PaginatedPipeline):
|
|||||||
else:
|
else:
|
||||||
self.artifacts_path = Path(pipeline_options.artifacts_path)
|
self.artifacts_path = Path(pipeline_options.artifacts_path)
|
||||||
|
|
||||||
keep_images = (
|
self.keep_images = (
|
||||||
self.pipeline_options.generate_page_images
|
self.pipeline_options.generate_page_images
|
||||||
or self.pipeline_options.generate_picture_images
|
or self.pipeline_options.generate_picture_images
|
||||||
or self.pipeline_options.generate_table_images
|
or self.pipeline_options.generate_table_images
|
||||||
@ -87,13 +88,30 @@ class StandardPdfPipeline(PaginatedPipeline):
|
|||||||
accelerator_options=pipeline_options.accelerator_options,
|
accelerator_options=pipeline_options.accelerator_options,
|
||||||
),
|
),
|
||||||
# Page assemble
|
# Page assemble
|
||||||
PageAssembleModel(options=PageAssembleOptions(keep_images=keep_images)),
|
PageAssembleModel(options=PageAssembleOptions()),
|
||||||
]
|
]
|
||||||
|
|
||||||
self.enrichment_pipe = [
|
self.enrichment_pipe = [
|
||||||
# Other models working on `NodeItem` elements in the DoclingDocument
|
# Other models working on `NodeItem` elements in the DoclingDocument
|
||||||
|
# Code Formula Enrichment Model
|
||||||
|
CodeFormulaModel(
|
||||||
|
enabled=pipeline_options.do_code_enrichment
|
||||||
|
or pipeline_options.do_formula_enrichment,
|
||||||
|
artifacts_path=pipeline_options.artifacts_path,
|
||||||
|
options=CodeFormulaModelOptions(
|
||||||
|
do_code_enrichment=pipeline_options.do_code_enrichment,
|
||||||
|
do_formula_enrichment=pipeline_options.do_formula_enrichment,
|
||||||
|
),
|
||||||
|
accelerator_options=pipeline_options.accelerator_options,
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if (
|
||||||
|
self.pipeline_options.do_formula_enrichment
|
||||||
|
or self.pipeline_options.do_code_enrichment
|
||||||
|
):
|
||||||
|
self.keep_backend = True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def download_models_hf(
|
def download_models_hf(
|
||||||
local_dir: Optional[Path] = None, force: bool = False
|
local_dir: Optional[Path] = None, force: bool = False
|
||||||
|
@ -270,7 +270,6 @@ def to_docling_document(doc_glm, update_name_label=False) -> DoclingDocument:
|
|||||||
container_el = doc.add_group(label=group_label)
|
container_el = doc.add_group(label=group_label)
|
||||||
|
|
||||||
_add_child_elements(container_el, doc, obj, pelem)
|
_add_child_elements(container_el, doc, obj, pelem)
|
||||||
|
|
||||||
elif "text" in obj:
|
elif "text" in obj:
|
||||||
text = obj["text"][span_i:span_j]
|
text = obj["text"][span_i:span_j]
|
||||||
|
|
||||||
@ -304,6 +303,10 @@ def to_docling_document(doc_glm, update_name_label=False) -> DoclingDocument:
|
|||||||
current_list = None
|
current_list = None
|
||||||
|
|
||||||
doc.add_heading(text=text, prov=prov)
|
doc.add_heading(text=text, prov=prov)
|
||||||
|
elif label == DocItemLabel.CODE:
|
||||||
|
current_list = None
|
||||||
|
|
||||||
|
doc.add_code(text=text, prov=prov)
|
||||||
else:
|
else:
|
||||||
current_list = None
|
current_list = None
|
||||||
|
|
||||||
|
80
docling/utils/visualization.py
Normal file
80
docling/utils/visualization.py
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
from docling_core.types.doc import DocItemLabel
|
||||||
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
|
from PIL.ImageFont import FreeTypeFont
|
||||||
|
|
||||||
|
from docling.datamodel.base_models import Cluster
|
||||||
|
|
||||||
|
|
||||||
|
def draw_clusters(
|
||||||
|
image: Image.Image, clusters: list[Cluster], scale_x: float, scale_y: float
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Draw clusters on an image
|
||||||
|
"""
|
||||||
|
draw = ImageDraw.Draw(image, "RGBA")
|
||||||
|
# Create a smaller font for the labels
|
||||||
|
font: ImageFont.ImageFont | FreeTypeFont
|
||||||
|
try:
|
||||||
|
font = ImageFont.truetype("arial.ttf", 12)
|
||||||
|
except OSError:
|
||||||
|
# Fallback to default font if arial is not available
|
||||||
|
font = ImageFont.load_default()
|
||||||
|
for c_tl in clusters:
|
||||||
|
all_clusters = [c_tl, *c_tl.children]
|
||||||
|
for c in all_clusters:
|
||||||
|
# Draw cells first (underneath)
|
||||||
|
cell_color = (0, 0, 0, 40) # Transparent black for cells
|
||||||
|
for tc in c.cells:
|
||||||
|
cx0, cy0, cx1, cy1 = tc.bbox.as_tuple()
|
||||||
|
cx0 *= scale_x
|
||||||
|
cx1 *= scale_x
|
||||||
|
cy0 *= scale_x
|
||||||
|
cy1 *= scale_y
|
||||||
|
|
||||||
|
draw.rectangle(
|
||||||
|
[(cx0, cy0), (cx1, cy1)],
|
||||||
|
outline=None,
|
||||||
|
fill=cell_color,
|
||||||
|
)
|
||||||
|
# Draw cluster rectangle
|
||||||
|
x0, y0, x1, y1 = c.bbox.as_tuple()
|
||||||
|
x0 *= scale_x
|
||||||
|
x1 *= scale_x
|
||||||
|
y0 *= scale_x
|
||||||
|
y1 *= scale_y
|
||||||
|
|
||||||
|
cluster_fill_color = (*list(DocItemLabel.get_color(c.label)), 70)
|
||||||
|
cluster_outline_color = (
|
||||||
|
*list(DocItemLabel.get_color(c.label)),
|
||||||
|
255,
|
||||||
|
)
|
||||||
|
draw.rectangle(
|
||||||
|
[(x0, y0), (x1, y1)],
|
||||||
|
outline=cluster_outline_color,
|
||||||
|
fill=cluster_fill_color,
|
||||||
|
)
|
||||||
|
# Add label name and confidence
|
||||||
|
label_text = f"{c.label.name} ({c.confidence:.2f})"
|
||||||
|
# Create semi-transparent background for text
|
||||||
|
text_bbox = draw.textbbox((x0, y0), label_text, font=font)
|
||||||
|
text_bg_padding = 2
|
||||||
|
draw.rectangle(
|
||||||
|
[
|
||||||
|
(
|
||||||
|
text_bbox[0] - text_bg_padding,
|
||||||
|
text_bbox[1] - text_bg_padding,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
text_bbox[2] + text_bg_padding,
|
||||||
|
text_bbox[3] + text_bg_padding,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
fill=(255, 255, 255, 180), # Semi-transparent white
|
||||||
|
)
|
||||||
|
# Draw text
|
||||||
|
draw.text(
|
||||||
|
(x0, y0),
|
||||||
|
label_text,
|
||||||
|
fill=(0, 0, 0, 255), # Solid black
|
||||||
|
font=font,
|
||||||
|
)
|
@ -54,12 +54,12 @@ tokens), &
|
|||||||
chunks with same headings & captions) — users can opt out of this step via param
|
chunks with same headings & captions) — users can opt out of this step via param
|
||||||
`merge_peers` (by default `True`)
|
`merge_peers` (by default `True`)
|
||||||
|
|
||||||
👉 Example: see [here](../../examples/hybrid_chunking).
|
👉 Example: see [here](../examples/hybrid_chunking.ipynb).
|
||||||
|
|
||||||
## Hierarchical Chunker
|
## Hierarchical Chunker
|
||||||
|
|
||||||
The `HierarchicalChunker` implementation uses the document structure information from
|
The `HierarchicalChunker` implementation uses the document structure information from
|
||||||
the [`DoclingDocument`](../docling_document) to create one chunk for each individual
|
the [`DoclingDocument`](./docling_document.md) to create one chunk for each individual
|
||||||
detected document element, by default only merging together list items (can be opted out
|
detected document element, by default only merging together list items (can be opted out
|
||||||
via param `merge_list_items`). It also takes care of attaching all relevant document
|
via param `merge_list_items`). It also takes care of attaching all relevant document
|
||||||
metadata, including headers and captions.
|
metadata, including headers and captions.
|
||||||
|
@ -5,7 +5,11 @@ from pathlib import Path
|
|||||||
|
|
||||||
from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend
|
from docling.backend.pypdfium2_backend import PyPdfiumDocumentBackend
|
||||||
from docling.datamodel.base_models import InputFormat
|
from docling.datamodel.base_models import InputFormat
|
||||||
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
from docling.datamodel.pipeline_options import (
|
||||||
|
AcceleratorDevice,
|
||||||
|
AcceleratorOptions,
|
||||||
|
PdfPipelineOptions,
|
||||||
|
)
|
||||||
from docling.document_converter import DocumentConverter, PdfFormatOption
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
||||||
from docling.models.ocr_mac_model import OcrMacOptions
|
from docling.models.ocr_mac_model import OcrMacOptions
|
||||||
from docling.models.tesseract_ocr_cli_model import TesseractCliOcrOptions
|
from docling.models.tesseract_ocr_cli_model import TesseractCliOcrOptions
|
||||||
@ -76,7 +80,7 @@ def main():
|
|||||||
pipeline_options.table_structure_options.do_cell_matching = True
|
pipeline_options.table_structure_options.do_cell_matching = True
|
||||||
pipeline_options.ocr_options.lang = ["es"]
|
pipeline_options.ocr_options.lang = ["es"]
|
||||||
pipeline_options.accelerator_options = AcceleratorOptions(
|
pipeline_options.accelerator_options = AcceleratorOptions(
|
||||||
num_threads=4, device=Device.AUTO
|
num_threads=4, device=AcceleratorDevice.AUTO
|
||||||
)
|
)
|
||||||
|
|
||||||
doc_converter = DocumentConverter(
|
doc_converter = DocumentConverter(
|
||||||
|
88
docs/examples/develop_formula_understanding.py
Normal file
88
docs/examples/develop_formula_understanding.py
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
|
from docling_core.types.doc import DocItemLabel, DoclingDocument, NodeItem, TextItem
|
||||||
|
|
||||||
|
from docling.datamodel.base_models import InputFormat, ItemAndImageEnrichmentElement
|
||||||
|
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
||||||
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
||||||
|
from docling.models.base_model import BaseItemAndImageEnrichmentModel
|
||||||
|
from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleFormulaUnderstandingPipelineOptions(PdfPipelineOptions):
|
||||||
|
do_formula_understanding: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
# A new enrichment model using both the document element and its image as input
|
||||||
|
class ExampleFormulaUnderstandingEnrichmentModel(BaseItemAndImageEnrichmentModel):
|
||||||
|
images_scale = 2.6
|
||||||
|
|
||||||
|
def __init__(self, enabled: bool):
|
||||||
|
self.enabled = enabled
|
||||||
|
|
||||||
|
def is_processable(self, doc: DoclingDocument, element: NodeItem) -> bool:
|
||||||
|
return (
|
||||||
|
self.enabled
|
||||||
|
and isinstance(element, TextItem)
|
||||||
|
and element.label == DocItemLabel.FORMULA
|
||||||
|
)
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self,
|
||||||
|
doc: DoclingDocument,
|
||||||
|
element_batch: Iterable[ItemAndImageEnrichmentElement],
|
||||||
|
) -> Iterable[NodeItem]:
|
||||||
|
if not self.enabled:
|
||||||
|
return
|
||||||
|
|
||||||
|
for enrich_element in element_batch:
|
||||||
|
enrich_element.image.show()
|
||||||
|
|
||||||
|
yield enrich_element.item
|
||||||
|
|
||||||
|
|
||||||
|
# How the pipeline can be extended.
|
||||||
|
class ExampleFormulaUnderstandingPipeline(StandardPdfPipeline):
|
||||||
|
|
||||||
|
def __init__(self, pipeline_options: ExampleFormulaUnderstandingPipelineOptions):
|
||||||
|
super().__init__(pipeline_options)
|
||||||
|
self.pipeline_options: ExampleFormulaUnderstandingPipelineOptions
|
||||||
|
|
||||||
|
self.enrichment_pipe = [
|
||||||
|
ExampleFormulaUnderstandingEnrichmentModel(
|
||||||
|
enabled=self.pipeline_options.do_formula_understanding
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
if self.pipeline_options.do_formula_understanding:
|
||||||
|
self.keep_backend = True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_default_options(cls) -> ExampleFormulaUnderstandingPipelineOptions:
|
||||||
|
return ExampleFormulaUnderstandingPipelineOptions()
|
||||||
|
|
||||||
|
|
||||||
|
# Example main. In the final version, we simply have to set do_formula_understanding to true.
|
||||||
|
def main():
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
input_doc_path = Path("./tests/data/2203.01017v2.pdf")
|
||||||
|
|
||||||
|
pipeline_options = ExampleFormulaUnderstandingPipelineOptions()
|
||||||
|
pipeline_options.do_formula_understanding = True
|
||||||
|
|
||||||
|
doc_converter = DocumentConverter(
|
||||||
|
format_options={
|
||||||
|
InputFormat.PDF: PdfFormatOption(
|
||||||
|
pipeline_cls=ExampleFormulaUnderstandingPipeline,
|
||||||
|
pipeline_options=pipeline_options,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
result = doc_converter.convert(input_doc_path)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
@ -22,7 +22,6 @@ class ExamplePictureClassifierPipelineOptions(PdfPipelineOptions):
|
|||||||
|
|
||||||
|
|
||||||
class ExamplePictureClassifierEnrichmentModel(BaseEnrichmentModel):
|
class ExamplePictureClassifierEnrichmentModel(BaseEnrichmentModel):
|
||||||
|
|
||||||
def __init__(self, enabled: bool):
|
def __init__(self, enabled: bool):
|
||||||
self.enabled = enabled
|
self.enabled = enabled
|
||||||
|
|
||||||
@ -54,7 +53,6 @@ class ExamplePictureClassifierEnrichmentModel(BaseEnrichmentModel):
|
|||||||
|
|
||||||
|
|
||||||
class ExamplePictureClassifierPipeline(StandardPdfPipeline):
|
class ExamplePictureClassifierPipeline(StandardPdfPipeline):
|
||||||
|
|
||||||
def __init__(self, pipeline_options: ExamplePictureClassifierPipelineOptions):
|
def __init__(self, pipeline_options: ExamplePictureClassifierPipelineOptions):
|
||||||
super().__init__(pipeline_options)
|
super().__init__(pipeline_options)
|
||||||
self.pipeline_options: ExamplePictureClassifierPipeline
|
self.pipeline_options: ExamplePictureClassifierPipeline
|
||||||
|
@ -4,7 +4,30 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# Hybrid Chunking"
|
"# Hybrid chunking"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Overview"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Hybrid chunking applies tokenization-aware refinements on top of document-based hierarchical chunking.\n",
|
||||||
|
"\n",
|
||||||
|
"For more details, see [here](../../concepts/chunking#hybrid-chunker)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -21,7 +44,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install -qU 'docling-core[chunking]' sentence-transformers transformers lancedb"
|
"%pip install -qU docling transformers"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -48,16 +71,12 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Chunking"
|
"## Chunking\n",
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Notice how `tokenizer` and `embed_model` further below are single-sourced from `EMBED_MODEL_ID`.\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"This is important for making sure the chunker and the embedding model are using the same tokenizer."
|
"### Basic usage\n",
|
||||||
|
"\n",
|
||||||
|
"For a basic usage scenario, we can just instantiate a `HybridChunker`, which will use\n",
|
||||||
|
"the default parameters."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -65,20 +84,102 @@
|
|||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from docling.chunking import HybridChunker\n",
|
||||||
|
"\n",
|
||||||
|
"chunker = HybridChunker()\n",
|
||||||
|
"chunk_iter = chunker.chunk(dl_doc=doc)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Note that the text you would typically want to embed is the context-enriched one as\n",
|
||||||
|
"returned by the `serialize()` method:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"=== 0 ===\n",
|
||||||
|
"chunk.text:\n",
|
||||||
|
"'International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American multinational technology company headquartered in Armonk, New York and present in over 175 countries.\\nIt is a publicly traded company and one of the 30 companies in the Dow Jones Industrial Aver…'\n",
|
||||||
|
"chunker.serialize(chunk):\n",
|
||||||
|
"'IBM\\nInternational Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American multinational technology company headquartered in Armonk, New York and present in over 175 countries.\\nIt is a publicly traded company and one of the 30 companies in the Dow Jones Industrial …'\n",
|
||||||
|
"\n",
|
||||||
|
"=== 1 ===\n",
|
||||||
|
"chunk.text:\n",
|
||||||
|
"'IBM originated with several technological innovations developed and commercialized in the late 19th century. Julius E. Pitrap patented the computing scale in 1885;[17] Alexander Dey invented the dial recorder (1888);[18] Herman Hollerith patented the Electric Tabulating Machine (1889);[19] and Willa…'\n",
|
||||||
|
"chunker.serialize(chunk):\n",
|
||||||
|
"'IBM\\n1910s–1950s\\nIBM originated with several technological innovations developed and commercialized in the late 19th century. Julius E. Pitrap patented the computing scale in 1885;[17] Alexander Dey invented the dial recorder (1888);[18] Herman Hollerith patented the Electric Tabulating Machine (1889…'\n",
|
||||||
|
"\n",
|
||||||
|
"=== 2 ===\n",
|
||||||
|
"chunk.text:\n",
|
||||||
|
"'Collectively, the companies manufactured a wide array of machinery for sale and lease, ranging from commercial scales and industrial time recorders, meat and cheese slicers, to tabulators and punched cards. Thomas J. Watson, Sr., fired from the National Cash Register Company by John Henry Patterson,…'\n",
|
||||||
|
"chunker.serialize(chunk):\n",
|
||||||
|
"'IBM\\n1910s–1950s\\nCollectively, the companies manufactured a wide array of machinery for sale and lease, ranging from commercial scales and industrial time recorders, meat and cheese slicers, to tabulators and punched cards. Thomas J. Watson, Sr., fired from the National Cash Register Company by John …'\n",
|
||||||
|
"\n",
|
||||||
|
"=== 3 ===\n",
|
||||||
|
"chunk.text:\n",
|
||||||
|
"'In 1961, IBM developed the SABRE reservation system for American Airlines and introduced the highly successful Selectric typewriter.…'\n",
|
||||||
|
"chunker.serialize(chunk):\n",
|
||||||
|
"'IBM\\n1960s–1980s\\nIn 1961, IBM developed the SABRE reservation system for American Airlines and introduced the highly successful Selectric typewriter.…'\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for i, chunk in enumerate(chunk_iter):\n",
|
||||||
|
" print(f\"=== {i} ===\")\n",
|
||||||
|
" print(f\"chunk.text:\\n{repr(f'{chunk.text[:300]}…')}\")\n",
|
||||||
|
"\n",
|
||||||
|
" enriched_text = chunker.serialize(chunk=chunk)\n",
|
||||||
|
" print(f\"chunker.serialize(chunk):\\n{repr(f'{enriched_text[:300]}…')}\")\n",
|
||||||
|
"\n",
|
||||||
|
" print()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Advanced usage\n",
|
||||||
|
"\n",
|
||||||
|
"For more control on the chunking, we can parametrize through the `HybridChunker`\n",
|
||||||
|
"arguments illustrated below.\n",
|
||||||
|
"\n",
|
||||||
|
"Notice how `tokenizer` and `embed_model` further below are single-sourced from\n",
|
||||||
|
"`EMBED_MODEL_ID`.\n",
|
||||||
|
"This is important for making sure the chunker and the embedding model are using the same\n",
|
||||||
|
"tokenizer."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from transformers import AutoTokenizer\n",
|
"from transformers import AutoTokenizer\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from docling.chunking import HybridChunker\n",
|
"from docling.chunking import HybridChunker\n",
|
||||||
"\n",
|
"\n",
|
||||||
"EMBED_MODEL_ID = \"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
"EMBED_MODEL_ID = \"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
||||||
"MAX_TOKENS = 64\n",
|
"MAX_TOKENS = 64 # set to a small number for illustrative purposes\n",
|
||||||
"\n",
|
"\n",
|
||||||
"tokenizer = AutoTokenizer.from_pretrained(EMBED_MODEL_ID)\n",
|
"tokenizer = AutoTokenizer.from_pretrained(EMBED_MODEL_ID)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"chunker = HybridChunker(\n",
|
"chunker = HybridChunker(\n",
|
||||||
" tokenizer=tokenizer, # can also just pass model name instead of tokenizer instance\n",
|
" tokenizer=tokenizer, # instance or model name, defaults to \"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
||||||
" max_tokens=MAX_TOKENS, # optional, by default derived from `tokenizer`\n",
|
" max_tokens=MAX_TOKENS, # optional, by default derived from `tokenizer`\n",
|
||||||
" # merge_peers=True, # optional, defaults to True\n",
|
" merge_peers=True, # optional, defaults to True\n",
|
||||||
")\n",
|
")\n",
|
||||||
"chunk_iter = chunker.chunk(dl_doc=doc)\n",
|
"chunk_iter = chunker.chunk(dl_doc=doc)\n",
|
||||||
"chunks = list(chunk_iter)"
|
"chunks = list(chunk_iter)"
|
||||||
@ -88,7 +189,7 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Points to notice:\n",
|
"Points to notice looking at the output chunks below:\n",
|
||||||
"- Where possible, we fit the limit of 64 tokens for the metadata-enriched serialization form (see chunk 2)\n",
|
"- Where possible, we fit the limit of 64 tokens for the metadata-enriched serialization form (see chunk 2)\n",
|
||||||
"- Where neeeded, we stop before the limit, e.g. see cases of 63 as it would otherwise run into a comma (see chunk 6)\n",
|
"- Where neeeded, we stop before the limit, e.g. see cases of 63 as it would otherwise run into a comma (see chunk 6)\n",
|
||||||
"- Where possible, we merge undersized peer chunks (see chunk 0)\n",
|
"- Where possible, we merge undersized peer chunks (see chunk 0)\n",
|
||||||
@ -97,7 +198,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 6,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -245,174 +346,6 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" print()"
|
" print()"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"## Vector Retrieval"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 5,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "stderr",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
|
||||||
"To disable this warning, you can either:\n",
|
|
||||||
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
|
||||||
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"from sentence_transformers import SentenceTransformer\n",
|
|
||||||
"\n",
|
|
||||||
"embed_model = SentenceTransformer(EMBED_MODEL_ID)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 6,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/html": [
|
|
||||||
"<div>\n",
|
|
||||||
"<style scoped>\n",
|
|
||||||
" .dataframe tbody tr th:only-of-type {\n",
|
|
||||||
" vertical-align: middle;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe tbody tr th {\n",
|
|
||||||
" vertical-align: top;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe thead th {\n",
|
|
||||||
" text-align: right;\n",
|
|
||||||
" }\n",
|
|
||||||
"</style>\n",
|
|
||||||
"<table border=\"1\" class=\"dataframe\">\n",
|
|
||||||
" <thead>\n",
|
|
||||||
" <tr style=\"text-align: right;\">\n",
|
|
||||||
" <th></th>\n",
|
|
||||||
" <th>vector</th>\n",
|
|
||||||
" <th>text</th>\n",
|
|
||||||
" <th>headings</th>\n",
|
|
||||||
" <th>captions</th>\n",
|
|
||||||
" <th>_distance</th>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </thead>\n",
|
|
||||||
" <tbody>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>0</th>\n",
|
|
||||||
" <td>[-0.1269039, -0.01948185, -0.07718097, -0.1116...</td>\n",
|
|
||||||
" <td>language, and the UPC barcode. The company has...</td>\n",
|
|
||||||
" <td>[IBM]</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>1.164613</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>1</th>\n",
|
|
||||||
" <td>[-0.10198064, 0.0055981805, -0.05095279, -0.13...</td>\n",
|
|
||||||
" <td>IBM originated with several technological inno...</td>\n",
|
|
||||||
" <td>[IBM, 1910s–1950s]</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>1.245144</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>2</th>\n",
|
|
||||||
" <td>[-0.057121325, -0.034115084, -0.018113216, -0....</td>\n",
|
|
||||||
" <td>As one of the world's oldest and largest techn...</td>\n",
|
|
||||||
" <td>[IBM]</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>1.355586</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>3</th>\n",
|
|
||||||
" <td>[-0.04429054, -0.058111433, -0.009330196, -0.0...</td>\n",
|
|
||||||
" <td>IBM is the largest industrial research organiz...</td>\n",
|
|
||||||
" <td>[IBM]</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>1.398617</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>4</th>\n",
|
|
||||||
" <td>[-0.11920792, 0.053496413, -0.042391937, -0.03...</td>\n",
|
|
||||||
" <td>Awards.[16]</td>\n",
|
|
||||||
" <td>[IBM]</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>1.446295</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </tbody>\n",
|
|
||||||
"</table>\n",
|
|
||||||
"</div>"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
" vector \\\n",
|
|
||||||
"0 [-0.1269039, -0.01948185, -0.07718097, -0.1116... \n",
|
|
||||||
"1 [-0.10198064, 0.0055981805, -0.05095279, -0.13... \n",
|
|
||||||
"2 [-0.057121325, -0.034115084, -0.018113216, -0.... \n",
|
|
||||||
"3 [-0.04429054, -0.058111433, -0.009330196, -0.0... \n",
|
|
||||||
"4 [-0.11920792, 0.053496413, -0.042391937, -0.03... \n",
|
|
||||||
"\n",
|
|
||||||
" text headings \\\n",
|
|
||||||
"0 language, and the UPC barcode. The company has... [IBM] \n",
|
|
||||||
"1 IBM originated with several technological inno... [IBM, 1910s–1950s] \n",
|
|
||||||
"2 As one of the world's oldest and largest techn... [IBM] \n",
|
|
||||||
"3 IBM is the largest industrial research organiz... [IBM] \n",
|
|
||||||
"4 Awards.[16] [IBM] \n",
|
|
||||||
"\n",
|
|
||||||
" captions _distance \n",
|
|
||||||
"0 None 1.164613 \n",
|
|
||||||
"1 None 1.245144 \n",
|
|
||||||
"2 None 1.355586 \n",
|
|
||||||
"3 None 1.398617 \n",
|
|
||||||
"4 None 1.446295 "
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 6,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"from pathlib import Path\n",
|
|
||||||
"from tempfile import mkdtemp\n",
|
|
||||||
"\n",
|
|
||||||
"import lancedb\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def make_lancedb_index(db_uri, index_name, chunks, embedding_model):\n",
|
|
||||||
" db = lancedb.connect(db_uri)\n",
|
|
||||||
" data = []\n",
|
|
||||||
" for chunk in chunks:\n",
|
|
||||||
" embeddings = embedding_model.encode(chunker.serialize(chunk=chunk))\n",
|
|
||||||
" data_item = {\n",
|
|
||||||
" \"vector\": embeddings,\n",
|
|
||||||
" \"text\": chunk.text,\n",
|
|
||||||
" \"headings\": chunk.meta.headings,\n",
|
|
||||||
" \"captions\": chunk.meta.captions,\n",
|
|
||||||
" }\n",
|
|
||||||
" data.append(data_item)\n",
|
|
||||||
" tbl = db.create_table(index_name, data=data, exist_ok=True)\n",
|
|
||||||
" return tbl\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"db_uri = str(Path(mkdtemp()) / \"docling.db\")\n",
|
|
||||||
"index = make_lancedb_index(db_uri, doc.name, chunks, embed_model)\n",
|
|
||||||
"\n",
|
|
||||||
"sample_query = \"invent\"\n",
|
|
||||||
"sample_embedding = embed_model.encode(sample_query)\n",
|
|
||||||
"results = index.search(sample_embedding).limit(5)\n",
|
|
||||||
"\n",
|
|
||||||
"results.to_pandas()"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
893
docs/examples/rag_azuresearch.ipynb
Normal file
893
docs/examples/rag_azuresearch.ipynb
Normal file
@ -0,0 +1,893 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "Ag9kcX2B_atc"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"<a href=\"https://colab.research.google.com/github/DS4SD/docling/blob/main/docs/examples/rag_azuresearch.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# RAG with Azure AI Search"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"| Step | Tech | Execution |\n",
|
||||||
|
"| ------------------ | ------------------ | --------- |\n",
|
||||||
|
"| Embedding | Azure OpenAI | 🌐 Remote |\n",
|
||||||
|
"| Vector Store | Azure AI Search | 🌐 Remote |\n",
|
||||||
|
"| Gen AI | Azure OpenAI | 🌐 Remote |"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook demonstrates how to build a Retrieval-Augmented Generation (RAG) system using:\n",
|
||||||
|
"- [Docling](https://ds4sd.github.io/docling/) for document parsing and chunking\n",
|
||||||
|
"- [Azure AI Search](https://azure.microsoft.com/products/ai-services/ai-search/?msockid=0109678bea39665431e37323ebff6723) for vector indexing and retrieval\n",
|
||||||
|
"- [Azure OpenAI](https://azure.microsoft.com/products/ai-services/openai-service?msockid=0109678bea39665431e37323ebff6723) for embeddings and chat completion\n",
|
||||||
|
"\n",
|
||||||
|
"This sample demonstrates how to:\n",
|
||||||
|
"1. Parse a PDF with Docling.\n",
|
||||||
|
"2. Chunk the parsed text.\n",
|
||||||
|
"3. Use Azure OpenAI for embeddings.\n",
|
||||||
|
"4. Index and search in Azure AI Search.\n",
|
||||||
|
"5. Run a retrieval-augmented generation (RAG) query with Azure OpenAI GPT-4o.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# If running in a fresh environment (like Google Colab), uncomment and run this single command:\n",
|
||||||
|
"%pip install \"docling~=2.12\" azure-search-documents==11.5.2 azure-identity openai rich torch python-dotenv"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Part 0: Prerequisites\n",
|
||||||
|
" - **Azure AI Search** resource\n",
|
||||||
|
" - **Azure OpenAI** resource with a deployed embedding and chat completion model (e.g. `text-embedding-3-small` and `gpt-4o`) \n",
|
||||||
|
" - **Docling 2.12+** (installs `docling_core` automatically) Docling installed (Python 3.8+ environment)\n",
|
||||||
|
"\n",
|
||||||
|
"- A **GPU-enabled environment** is preferred for faster parsing. Docling 2.12 automatically detects GPU if present.\n",
|
||||||
|
" - If you only have CPU, parsing large PDFs can be slower. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"\n",
|
||||||
|
"from dotenv import load_dotenv\n",
|
||||||
|
"\n",
|
||||||
|
"load_dotenv()\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def _get_env(key, default=None):\n",
|
||||||
|
" try:\n",
|
||||||
|
" from google.colab import userdata\n",
|
||||||
|
"\n",
|
||||||
|
" try:\n",
|
||||||
|
" return userdata.get(key)\n",
|
||||||
|
" except userdata.SecretNotFoundError:\n",
|
||||||
|
" pass\n",
|
||||||
|
" except ImportError:\n",
|
||||||
|
" pass\n",
|
||||||
|
" return os.getenv(key, default)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"AZURE_SEARCH_ENDPOINT = _get_env(\"AZURE_SEARCH_ENDPOINT\")\n",
|
||||||
|
"AZURE_SEARCH_KEY = _get_env(\"AZURE_SEARCH_KEY\") # Ensure this is your Admin Key\n",
|
||||||
|
"AZURE_SEARCH_INDEX_NAME = _get_env(\"AZURE_SEARCH_INDEX_NAME\", \"docling-rag-sample\")\n",
|
||||||
|
"AZURE_OPENAI_ENDPOINT = _get_env(\"AZURE_OPENAI_ENDPOINT\")\n",
|
||||||
|
"AZURE_OPENAI_API_KEY = _get_env(\"AZURE_OPENAI_API_KEY\")\n",
|
||||||
|
"AZURE_OPENAI_API_VERSION = _get_env(\"AZURE_OPENAI_API_VERSION\", \"2024-10-21\")\n",
|
||||||
|
"AZURE_OPENAI_CHAT_MODEL = _get_env(\n",
|
||||||
|
" \"AZURE_OPENAI_CHAT_MODEL\"\n",
|
||||||
|
") # Using a deployed model named \"gpt-4o\"\n",
|
||||||
|
"AZURE_OPENAI_EMBEDDINGS = _get_env(\n",
|
||||||
|
" \"AZURE_OPENAI_EMBEDDINGS\", \"text-embedding-3-small\"\n",
|
||||||
|
") # Using a deployed model named \"text-embeddings-3-small\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Part 1: Parse the PDF with Docling\n",
|
||||||
|
"\n",
|
||||||
|
"We’ll parse the **Microsoft GraphRAG Research Paper** (~15 pages). Parsing should be relatively quick, even on CPU, but it will be faster on a GPU or MPS device if available.\n",
|
||||||
|
"\n",
|
||||||
|
"*(If you prefer a different document, simply provide a different URL or local file path.)*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"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\"><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">Parsing a ~</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">15</span><span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\">-page PDF. The process should be relatively quick, even on CPU...</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"\u001b[1;33mParsing a ~\u001b[0m\u001b[1;33m15\u001b[0m\u001b[1;33m-page PDF. The process should be relatively quick, even on CPU\u001b[0m\u001b[1;33m...\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">╭─────────────────────────────────────────── Docling Markdown Preview ────────────────────────────────────────────╮\n",
|
||||||
|
"│ ## From Local to Global: A Graph RAG Approach to Query-Focused Summarization │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Darren Edge 1† │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Ha Trinh 1† │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Newman Cheng 2 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Joshua Bradley 2 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Alex Chao 3 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Apurva Mody 3 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Steven Truitt 2 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ ## Jonathan Larson 1 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ 1 Microsoft Research 2 Microsoft Strategic Missions and Technologies 3 Microsoft Office of the CTO │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ { daedge,trinhha,newmancheng,joshbradley,achao,moapurva,steventruitt,jolarso } @microsoft.com │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ † These authors contributed equally to this work │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ ## Abstract │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ The use of retrieval-augmented gen... │\n",
|
||||||
|
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"╭─────────────────────────────────────────── Docling Markdown Preview ────────────────────────────────────────────╮\n",
|
||||||
|
"│ ## From Local to Global: A Graph RAG Approach to Query-Focused Summarization │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Darren Edge 1† │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Ha Trinh 1† │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Newman Cheng 2 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Joshua Bradley 2 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Alex Chao 3 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Apurva Mody 3 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ Steven Truitt 2 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ ## Jonathan Larson 1 │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ 1 Microsoft Research 2 Microsoft Strategic Missions and Technologies 3 Microsoft Office of the CTO │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ { daedge,trinhha,newmancheng,joshbradley,achao,moapurva,steventruitt,jolarso } @microsoft.com │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ † These authors contributed equally to this work │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ ## Abstract │\n",
|
||||||
|
"│ │\n",
|
||||||
|
"│ The use of retrieval-augmented gen... │\n",
|
||||||
|
"╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from rich.console import Console\n",
|
||||||
|
"from rich.panel import Panel\n",
|
||||||
|
"\n",
|
||||||
|
"from docling.document_converter import DocumentConverter\n",
|
||||||
|
"\n",
|
||||||
|
"console = Console()\n",
|
||||||
|
"\n",
|
||||||
|
"# This URL points to the Microsoft GraphRAG Research Paper (arXiv: 2404.16130), ~15 pages\n",
|
||||||
|
"source_url = \"https://arxiv.org/pdf/2404.16130\"\n",
|
||||||
|
"\n",
|
||||||
|
"console.print(\n",
|
||||||
|
" \"[bold yellow]Parsing a ~15-page PDF. The process should be relatively quick, even on CPU...[/bold yellow]\"\n",
|
||||||
|
")\n",
|
||||||
|
"converter = DocumentConverter()\n",
|
||||||
|
"result = converter.convert(source_url)\n",
|
||||||
|
"\n",
|
||||||
|
"# Optional: preview the parsed Markdown\n",
|
||||||
|
"md_preview = result.document.export_to_markdown()\n",
|
||||||
|
"console.print(Panel(md_preview[:500] + \"...\", title=\"Docling Markdown Preview\"))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Part 2: Hierarchical Chunking\n",
|
||||||
|
"We convert the `Document` into smaller chunks for embedding and indexing. The built-in `HierarchicalChunker` preserves structure. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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\">Total chunks from PDF: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">106</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"Total chunks from PDF: \u001b[1;36m106\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from docling.chunking import HierarchicalChunker\n",
|
||||||
|
"\n",
|
||||||
|
"chunker = HierarchicalChunker()\n",
|
||||||
|
"doc_chunks = list(chunker.chunk(result.document))\n",
|
||||||
|
"\n",
|
||||||
|
"all_chunks = []\n",
|
||||||
|
"for idx, c in enumerate(doc_chunks):\n",
|
||||||
|
" chunk_text = c.text\n",
|
||||||
|
" all_chunks.append((f\"chunk_{idx}\", chunk_text))\n",
|
||||||
|
"\n",
|
||||||
|
"console.print(f\"Total chunks from PDF: {len(all_chunks)}\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Part 3: Create Azure Search Index and Push Chunk Embeddings\n",
|
||||||
|
"We’ll define a vector index in Azure AI Search, then embed each chunk using Azure OpenAI and upload in batches."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 23,
|
||||||
|
"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\">Index <span style=\"color: #008000; text-decoration-color: #008000\">'docling-rag-sample-2'</span> created.\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"Index \u001b[32m'docling-rag-sample-2'\u001b[0m created.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from azure.core.credentials import AzureKeyCredential\n",
|
||||||
|
"from azure.search.documents.indexes import SearchIndexClient\n",
|
||||||
|
"from azure.search.documents.indexes.models import (\n",
|
||||||
|
" AzureOpenAIVectorizer,\n",
|
||||||
|
" AzureOpenAIVectorizerParameters,\n",
|
||||||
|
" HnswAlgorithmConfiguration,\n",
|
||||||
|
" SearchableField,\n",
|
||||||
|
" SearchField,\n",
|
||||||
|
" SearchFieldDataType,\n",
|
||||||
|
" SearchIndex,\n",
|
||||||
|
" SimpleField,\n",
|
||||||
|
" VectorSearch,\n",
|
||||||
|
" VectorSearchProfile,\n",
|
||||||
|
")\n",
|
||||||
|
"from rich.console import Console\n",
|
||||||
|
"\n",
|
||||||
|
"console = Console()\n",
|
||||||
|
"\n",
|
||||||
|
"VECTOR_DIM = 1536 # Adjust based on your chosen embeddings model\n",
|
||||||
|
"\n",
|
||||||
|
"index_client = SearchIndexClient(\n",
|
||||||
|
" AZURE_SEARCH_ENDPOINT, AzureKeyCredential(AZURE_SEARCH_KEY)\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def create_search_index(index_name: str):\n",
|
||||||
|
" # Define fields\n",
|
||||||
|
" fields = [\n",
|
||||||
|
" SimpleField(name=\"chunk_id\", type=SearchFieldDataType.String, key=True),\n",
|
||||||
|
" SearchableField(name=\"content\", type=SearchFieldDataType.String),\n",
|
||||||
|
" SearchField(\n",
|
||||||
|
" name=\"content_vector\",\n",
|
||||||
|
" type=SearchFieldDataType.Collection(SearchFieldDataType.Single),\n",
|
||||||
|
" searchable=True,\n",
|
||||||
|
" filterable=False,\n",
|
||||||
|
" sortable=False,\n",
|
||||||
|
" facetable=False,\n",
|
||||||
|
" vector_search_dimensions=VECTOR_DIM,\n",
|
||||||
|
" vector_search_profile_name=\"default\",\n",
|
||||||
|
" ),\n",
|
||||||
|
" ]\n",
|
||||||
|
" # Vector search config with an AzureOpenAIVectorizer\n",
|
||||||
|
" vector_search = VectorSearch(\n",
|
||||||
|
" algorithms=[HnswAlgorithmConfiguration(name=\"default\")],\n",
|
||||||
|
" profiles=[\n",
|
||||||
|
" VectorSearchProfile(\n",
|
||||||
|
" name=\"default\",\n",
|
||||||
|
" algorithm_configuration_name=\"default\",\n",
|
||||||
|
" vectorizer_name=\"default\",\n",
|
||||||
|
" )\n",
|
||||||
|
" ],\n",
|
||||||
|
" vectorizers=[\n",
|
||||||
|
" AzureOpenAIVectorizer(\n",
|
||||||
|
" vectorizer_name=\"default\",\n",
|
||||||
|
" parameters=AzureOpenAIVectorizerParameters(\n",
|
||||||
|
" resource_url=AZURE_OPENAI_ENDPOINT,\n",
|
||||||
|
" deployment_name=AZURE_OPENAI_EMBEDDINGS,\n",
|
||||||
|
" model_name=\"text-embedding-3-small\",\n",
|
||||||
|
" api_key=AZURE_OPENAI_API_KEY,\n",
|
||||||
|
" ),\n",
|
||||||
|
" )\n",
|
||||||
|
" ],\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
" # Create or update the index\n",
|
||||||
|
" new_index = SearchIndex(name=index_name, fields=fields, vector_search=vector_search)\n",
|
||||||
|
" try:\n",
|
||||||
|
" index_client.delete_index(index_name)\n",
|
||||||
|
" except:\n",
|
||||||
|
" pass\n",
|
||||||
|
"\n",
|
||||||
|
" index_client.create_or_update_index(new_index)\n",
|
||||||
|
" console.print(f\"Index '{index_name}' created.\")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"create_search_index(AZURE_SEARCH_INDEX_NAME)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Embed and Upsert to Azure AI Search\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 28,
|
||||||
|
"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\">Uploaded batch <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">0</span> -> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">50</span>; all_succeeded: <span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>, first_doc_status_code: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">201</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"Uploaded batch \u001b[1;36m0\u001b[0m -> \u001b[1;36m50\u001b[0m; all_succeeded: \u001b[3;92mTrue\u001b[0m, first_doc_status_code: \u001b[1;36m201\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Uploaded batch <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">50</span> -> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">100</span>; all_succeeded: <span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>, first_doc_status_code: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">201</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"Uploaded batch \u001b[1;36m50\u001b[0m -> \u001b[1;36m100\u001b[0m; all_succeeded: \u001b[3;92mTrue\u001b[0m, first_doc_status_code: \u001b[1;36m201\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Uploaded batch <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">100</span> -> <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">106</span>; all_succeeded: <span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>, first_doc_status_code: <span style=\"color: #008080; text-decoration-color: #008080; font-weight: bold\">201</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"Uploaded batch \u001b[1;36m100\u001b[0m -> \u001b[1;36m106\u001b[0m; all_succeeded: \u001b[3;92mTrue\u001b[0m, first_doc_status_code: \u001b[1;36m201\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/html": [
|
||||||
|
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">All chunks uploaded to Azure Search.\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"All chunks uploaded to Azure Search.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from azure.search.documents import SearchClient\n",
|
||||||
|
"from openai import AzureOpenAI\n",
|
||||||
|
"\n",
|
||||||
|
"search_client = SearchClient(\n",
|
||||||
|
" AZURE_SEARCH_ENDPOINT, AZURE_SEARCH_INDEX_NAME, AzureKeyCredential(AZURE_SEARCH_KEY)\n",
|
||||||
|
")\n",
|
||||||
|
"openai_client = AzureOpenAI(\n",
|
||||||
|
" api_key=AZURE_OPENAI_API_KEY,\n",
|
||||||
|
" api_version=AZURE_OPENAI_API_VERSION,\n",
|
||||||
|
" azure_endpoint=AZURE_OPENAI_ENDPOINT,\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def embed_text(text: str):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Helper to generate embeddings with Azure OpenAI.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" response = openai_client.embeddings.create(\n",
|
||||||
|
" input=text, model=AZURE_OPENAI_EMBEDDINGS\n",
|
||||||
|
" )\n",
|
||||||
|
" return response.data[0].embedding\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"upload_docs = []\n",
|
||||||
|
"for chunk_id, chunk_text in all_chunks:\n",
|
||||||
|
" embedding_vector = embed_text(chunk_text)\n",
|
||||||
|
" upload_docs.append(\n",
|
||||||
|
" {\n",
|
||||||
|
" \"chunk_id\": chunk_id,\n",
|
||||||
|
" \"content\": chunk_text,\n",
|
||||||
|
" \"content_vector\": embedding_vector,\n",
|
||||||
|
" }\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"BATCH_SIZE = 50\n",
|
||||||
|
"for i in range(0, len(upload_docs), BATCH_SIZE):\n",
|
||||||
|
" subset = upload_docs[i : i + BATCH_SIZE]\n",
|
||||||
|
" resp = search_client.upload_documents(documents=subset)\n",
|
||||||
|
"\n",
|
||||||
|
" all_succeeded = all(r.succeeded for r in resp)\n",
|
||||||
|
" console.print(\n",
|
||||||
|
" f\"Uploaded batch {i} -> {i+len(subset)}; all_succeeded: {all_succeeded}, \"\n",
|
||||||
|
" f\"first_doc_status_code: {resp[0].status_code}\"\n",
|
||||||
|
" )\n",
|
||||||
|
"\n",
|
||||||
|
"console.print(\"All chunks uploaded to Azure Search.\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Part 4: RAG Query with Azure OpenAI\n",
|
||||||
|
"Combine retrieval from Azure Search with Chat Completions (aka. grounding your LLM)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 29,
|
||||||
|
"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\"><span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">╭──────────────────────────────────────────────────</span> RAG Prompt <span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">───────────────────────────────────────────────────╮</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ You are an AI assistant helping answering questions about Microsoft GraphRAG. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Use ONLY the text below to answer the user's question. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ If the answer isn't in the text, say you don't know. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Context: │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Community summaries vs. source texts. When comparing community summaries to source texts using Graph RAG, │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ community summaries generally provided a small but consistent improvement in answer comprehensiveness and │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ diversity, except for root-level summaries. Intermediate-level summaries in the Podcast dataset and low-level │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ community summaries in the News dataset achieved comprehensiveness win rates of 57% and 64%, respectively. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Diversity win rates were 57% for Podcast intermediate-level summaries and 60% for News low-level community │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ summaries. Table 3 also illustrates the scalability advantages of Graph RAG compared to source text │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ summarization: for low-level community summaries ( C3 ), Graph RAG required 26-33% fewer context tokens, while │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ for root-level community summaries ( C0 ), it required over 97% fewer tokens. For a modest drop in performance │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ compared with other global methods, root-level Graph RAG offers a highly efficient method for the iterative │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ question answering that characterizes sensemaking activity, while retaining advantages in comprehensiveness │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ (72% win rate) and diversity (62% win rate) over na¨ıve RAG. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ We have presented a global approach to Graph RAG, combining knowledge graph generation, retrieval-augmented │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ generation (RAG), and query-focused summarization (QFS) to support human sensemaking over entire text corpora. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Initial evaluations show substantial improvements over a na¨ıve RAG baseline for both the comprehensiveness and │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ diversity of answers, as well as favorable comparisons to a global but graph-free approach using map-reduce │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ source text summarization. For situations requiring many global queries over the same dataset, summaries of │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ root-level communities in the entity-based graph index provide a data index that is both superior to na¨ıve RAG │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ and achieves competitive performance to other global methods at a fraction of the token cost. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Trade-offs of building a graph index . We consistently observed Graph RAG achieve the best headto-head results │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ against other methods, but in many cases the graph-free approach to global summarization of source texts │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ performed competitively. The real-world decision about whether to invest in building a graph index depends on │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ multiple factors, including the compute budget, expected number of lifetime queries per dataset, and value │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ obtained from other aspects of the graph index (including the generic community summaries and the use of other │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ graph-related RAG approaches). │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Future work . The graph index, rich text annotations, and hierarchical community structure supporting the │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ current Graph RAG approach offer many possibilities for refinement and adaptation. This includes RAG approaches │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ that operate in a more local manner, via embedding-based matching of user queries and graph annotations, as │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ well as the possibility of hybrid RAG schemes that combine embedding-based matching against community reports │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ before employing our map-reduce summarization mechanisms. This 'roll-up' operation could also be extended │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ across more levels of the community hierarchy, as well as implemented as a more exploratory 'drill down' │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ mechanism that follows the information scent contained in higher-level community summaries. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Advanced RAG systems include pre-retrieval, retrieval, post-retrieval strategies designed to overcome the │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ drawbacks of Na¨ıve RAG, while Modular RAG systems include patterns for iterative and dynamic cycles of │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ interleaved retrieval and generation (Gao et al., 2023). Our implementation of Graph RAG incorporates multiple │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ concepts related to other systems. For example, our community summaries are a kind of self-memory (Selfmem, │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Cheng et al., 2024) for generation-augmented retrieval (GAR, Mao et al., 2020) that facilitates future │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ generation cycles, while our parallel generation of community answers from these summaries is a kind of │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ iterative (Iter-RetGen, Shao et al., 2023) or federated (FeB4RAG, Wang et al., 2024) retrieval-generation │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ strategy. Other systems have also combined these concepts for multi-document summarization (CAiRE-COVID, Su et │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ al., 2020) and multi-hop question answering (ITRG, Feng et al., 2023; IR-CoT, Trivedi et al., 2022; DSP, │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Khattab et al., 2022). Our use of a hierarchical index and summarization also bears resemblance to further │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ approaches, such as generating a hierarchical index of text chunks by clustering the vectors of text embeddings │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ (RAPTOR, Sarthi et al., 2024) or generating a 'tree of clarifications' to answer multiple interpretations of │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ ambiguous questions (Kim et al., 2023). However, none of these iterative or hierarchical approaches use the │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ kind of self-generated graph index that enables Graph RAG. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ The use of retrieval-augmented generation (RAG) to retrieve relevant information from an external knowledge │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ source enables large language models (LLMs) to answer questions over private and/or previously unseen document │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ collections. However, RAG fails on global questions directed at an entire text corpus, such as 'What are the │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ main themes in the dataset?', since this is inherently a queryfocused summarization (QFS) task, rather than an │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ explicit retrieval task. Prior QFS methods, meanwhile, fail to scale to the quantities of text indexed by │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ typical RAGsystems. To combine the strengths of these contrasting methods, we propose a Graph RAG approach to │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ question answering over private text corpora that scales with both the generality of user questions and the │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ quantity of source text to be indexed. Our approach uses an LLM to build a graph-based text index in two │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ stages: first to derive an entity knowledge graph from the source documents, then to pregenerate community │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ summaries for all groups of closely-related entities. Given a question, each community summary is used to │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ generate a partial response, before all partial responses are again summarized in a final response to the user. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ For a class of global sensemaking questions over datasets in the 1 million token range, we show that Graph RAG │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ leads to substantial improvements over a na¨ıve RAG baseline for both the comprehensiveness and diversity of │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ generated answers. An open-source, Python-based implementation of both global and local Graph RAG approaches is │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ forthcoming at https://aka . ms/graphrag . │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Given the multi-stage nature of our Graph RAG mechanism, the multiple conditions we wanted to compare, and the │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ lack of gold standard answers to our activity-based sensemaking questions, we decided to adopt a head-to-head │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ comparison approach using an LLM evaluator. We selected three target metrics capturing qualities that are │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ desirable for sensemaking activities, as well as a control metric (directness) used as a indicator of validity. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Since directness is effectively in opposition to comprehensiveness and diversity, we would not expect any │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ method to win across all four metrics. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Figure 1: Graph RAG pipeline using an LLM-derived graph index of source document text. This index spans nodes │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ (e.g., entities), edges (e.g., relationships), and covariates (e.g., claims) that have been detected, │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ extracted, and summarized by LLM prompts tailored to the domain of the dataset. Community detection (e.g., │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Leiden, Traag et al., 2019) is used to partition the graph index into groups of elements (nodes, edges, │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ covariates) that the LLM can summarize in parallel at both indexing time and query time. The 'global answer' to │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ a given query is produced using a final round of query-focused summarization over all community summaries │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ reporting relevance to that query. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Retrieval-augmented generation (RAG, Lewis et al., 2020) is an established approach to answering user questions │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ over entire datasets, but it is designed for situations where these answers are contained locally within │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ regions of text whose retrieval provides sufficient grounding for the generation task. Instead, a more │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ appropriate task framing is query-focused summarization (QFS, Dang, 2006), and in particular, query-focused │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ abstractive summarization that generates natural language summaries and not just concatenated excerpts (Baumel │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ et al., 2018; Laskar et al., 2020; Yao et al., 2017) . In recent years, however, such distinctions between │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ summarization tasks that are abstractive versus extractive, generic versus query-focused, and single-document │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ versus multi-document, have become less relevant. While early applications of the transformer architecture │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ showed substantial improvements on the state-of-the-art for all such summarization tasks (Goodwin et al., 2020; │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Laskar et al., 2022; Liu and Lapata, 2019), these tasks are now trivialized by modern LLMs, including the GPT │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ (Achiam et al., 2023; Brown et al., 2020), Llama (Touvron et al., 2023), and Gemini (Anil et al., 2023) series, │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ all of which can use in-context learning to summarize any content provided in their context window. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ --- │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ community descriptions provide complete coverage of the underlying graph index and the input documents it │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ represents. Query-focused summarization of an entire corpus is then made possible using a map-reduce approach: │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ first using each community summary to answer the query independently and in parallel, then summarizing all │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ relevant partial answers into a final global answer. │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Question: What are the main advantages of using the Graph RAG approach for query-focused summarization compared │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ to traditional RAG methods? │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ Answer: │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #800000; text-decoration-color: #800000; font-weight: bold\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"\u001b[1;31m╭─\u001b[0m\u001b[1;31m─────────────────────────────────────────────────\u001b[0m RAG Prompt \u001b[1;31m──────────────────────────────────────────────────\u001b[0m\u001b[1;31m─╮\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mYou are an AI assistant helping answering questions about Microsoft GraphRAG.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mUse ONLY the text below to answer the user's question.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mIf the answer isn't in the text, say you don't know.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mContext:\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mCommunity summaries vs. source texts. When comparing community summaries to source texts using Graph RAG, \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcommunity summaries generally provided a small but consistent improvement in answer comprehensiveness and \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mdiversity, except for root-level summaries. Intermediate-level summaries in the Podcast dataset and low-level \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcommunity summaries in the News dataset achieved comprehensiveness win rates of 57% and 64%, respectively. \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mDiversity win rates were 57% for Podcast intermediate-level summaries and 60% for News low-level community \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31msummaries. Table 3 also illustrates the scalability advantages of Graph RAG compared to source text \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31msummarization: for low-level community summaries ( C3 ), Graph RAG required 26-33% fewer context tokens, while \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mfor root-level community summaries ( C0 ), it required over 97% fewer tokens. For a modest drop in performance \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcompared with other global methods, root-level Graph RAG offers a highly efficient method for the iterative \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mquestion answering that characterizes sensemaking activity, while retaining advantages in comprehensiveness \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m(72% win rate) and diversity (62% win rate) over na¨ıve RAG.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mWe have presented a global approach to Graph RAG, combining knowledge graph generation, retrieval-augmented \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mgeneration (RAG), and query-focused summarization (QFS) to support human sensemaking over entire text corpora. \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mInitial evaluations show substantial improvements over a na¨ıve RAG baseline for both the comprehensiveness and\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mdiversity of answers, as well as favorable comparisons to a global but graph-free approach using map-reduce \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31msource text summarization. For situations requiring many global queries over the same dataset, summaries of \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mroot-level communities in the entity-based graph index provide a data index that is both superior to na¨ıve RAG\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mand achieves competitive performance to other global methods at a fraction of the token cost.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mTrade-offs of building a graph index . We consistently observed Graph RAG achieve the best headto-head results \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31magainst other methods, but in many cases the graph-free approach to global summarization of source texts \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mperformed competitively. The real-world decision about whether to invest in building a graph index depends on \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mmultiple factors, including the compute budget, expected number of lifetime queries per dataset, and value \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mobtained from other aspects of the graph index (including the generic community summaries and the use of other \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mgraph-related RAG approaches).\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mFuture work . The graph index, rich text annotations, and hierarchical community structure supporting the \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcurrent Graph RAG approach offer many possibilities for refinement and adaptation. This includes RAG approaches\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mthat operate in a more local manner, via embedding-based matching of user queries and graph annotations, as \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mwell as the possibility of hybrid RAG schemes that combine embedding-based matching against community reports \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mbefore employing our map-reduce summarization mechanisms. This 'roll-up' operation could also be extended \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31macross more levels of the community hierarchy, as well as implemented as a more exploratory 'drill down' \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mmechanism that follows the information scent contained in higher-level community summaries.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mAdvanced RAG systems include pre-retrieval, retrieval, post-retrieval strategies designed to overcome the \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mdrawbacks of Na¨ıve RAG, while Modular RAG systems include patterns for iterative and dynamic cycles of \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31minterleaved retrieval and generation (Gao et al., 2023). Our implementation of Graph RAG incorporates multiple \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mconcepts related to other systems. For example, our community summaries are a kind of self-memory (Selfmem, \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mCheng et al., 2024) for generation-augmented retrieval (GAR, Mao et al., 2020) that facilitates future \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mgeneration cycles, while our parallel generation of community answers from these summaries is a kind of \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31miterative (Iter-RetGen, Shao et al., 2023) or federated (FeB4RAG, Wang et al., 2024) retrieval-generation \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mstrategy. Other systems have also combined these concepts for multi-document summarization (CAiRE-COVID, Su et \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mal., 2020) and multi-hop question answering (ITRG, Feng et al., 2023; IR-CoT, Trivedi et al., 2022; DSP, \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mKhattab et al., 2022). Our use of a hierarchical index and summarization also bears resemblance to further \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mapproaches, such as generating a hierarchical index of text chunks by clustering the vectors of text embeddings\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m(RAPTOR, Sarthi et al., 2024) or generating a 'tree of clarifications' to answer multiple interpretations of \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mambiguous questions (Kim et al., 2023). However, none of these iterative or hierarchical approaches use the \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mkind of self-generated graph index that enables Graph RAG.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mThe use of retrieval-augmented generation (RAG) to retrieve relevant information from an external knowledge \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31msource enables large language models (LLMs) to answer questions over private and/or previously unseen document \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcollections. However, RAG fails on global questions directed at an entire text corpus, such as 'What are the \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mmain themes in the dataset?', since this is inherently a queryfocused summarization (QFS) task, rather than an \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mexplicit retrieval task. Prior QFS methods, meanwhile, fail to scale to the quantities of text indexed by \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mtypical RAGsystems. To combine the strengths of these contrasting methods, we propose a Graph RAG approach to \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mquestion answering over private text corpora that scales with both the generality of user questions and the \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mquantity of source text to be indexed. Our approach uses an LLM to build a graph-based text index in two \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mstages: first to derive an entity knowledge graph from the source documents, then to pregenerate community \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31msummaries for all groups of closely-related entities. Given a question, each community summary is used to \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mgenerate a partial response, before all partial responses are again summarized in a final response to the user.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mFor a class of global sensemaking questions over datasets in the 1 million token range, we show that Graph RAG \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mleads to substantial improvements over a na¨ıve RAG baseline for both the comprehensiveness and diversity of \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mgenerated answers. An open-source, Python-based implementation of both global and local Graph RAG approaches is\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mforthcoming at https://aka . ms/graphrag .\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mGiven the multi-stage nature of our Graph RAG mechanism, the multiple conditions we wanted to compare, and the \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mlack of gold standard answers to our activity-based sensemaking questions, we decided to adopt a head-to-head \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcomparison approach using an LLM evaluator. We selected three target metrics capturing qualities that are \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mdesirable for sensemaking activities, as well as a control metric (directness) used as a indicator of validity.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mSince directness is effectively in opposition to comprehensiveness and diversity, we would not expect any \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mmethod to win across all four metrics.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mFigure 1: Graph RAG pipeline using an LLM-derived graph index of source document text. This index spans nodes \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m(e.g., entities), edges (e.g., relationships), and covariates (e.g., claims) that have been detected, \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mextracted, and summarized by LLM prompts tailored to the domain of the dataset. Community detection (e.g., \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mLeiden, Traag et al., 2019) is used to partition the graph index into groups of elements (nodes, edges, \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcovariates) that the LLM can summarize in parallel at both indexing time and query time. The 'global answer' to\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31ma given query is produced using a final round of query-focused summarization over all community summaries \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mreporting relevance to that query.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mRetrieval-augmented generation (RAG, Lewis et al., 2020) is an established approach to answering user questions\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mover entire datasets, but it is designed for situations where these answers are contained locally within \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mregions of text whose retrieval provides sufficient grounding for the generation task. Instead, a more \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mappropriate task framing is query-focused summarization (QFS, Dang, 2006), and in particular, query-focused \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mabstractive summarization that generates natural language summaries and not just concatenated excerpts (Baumel \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31met al., 2018; Laskar et al., 2020; Yao et al., 2017) . In recent years, however, such distinctions between \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31msummarization tasks that are abstractive versus extractive, generic versus query-focused, and single-document \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mversus multi-document, have become less relevant. While early applications of the transformer architecture \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mshowed substantial improvements on the state-of-the-art for all such summarization tasks (Goodwin et al., 2020;\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mLaskar et al., 2022; Liu and Lapata, 2019), these tasks are now trivialized by modern LLMs, including the GPT \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m(Achiam et al., 2023; Brown et al., 2020), Llama (Touvron et al., 2023), and Gemini (Anil et al., 2023) series,\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mall of which can use in-context learning to summarize any content provided in their context window.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m---\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mcommunity descriptions provide complete coverage of the underlying graph index and the input documents it \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mrepresents. Query-focused summarization of an entire corpus is then made possible using a map-reduce approach: \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mfirst using each community summary to answer the query independently and in parallel, then summarizing all \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mrelevant partial answers into a final global answer.\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mQuestion: What are the main advantages of using the Graph RAG approach for query-focused summarization compared\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mto traditional RAG methods?\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31mAnswer:\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m│\u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m \u001b[0m\u001b[1;31m│\u001b[0m\n",
|
||||||
|
"\u001b[1;31m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">╭─────────────────────────────────────────────────</span> RAG Response <span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">──────────────────────────────────────────────────╮</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ The main advantages of using the Graph RAG approach for query-focused summarization compared to traditional RAG │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ methods include: │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ 1. **Improved Comprehensiveness and Diversity**: Graph RAG shows substantial improvements over a naïve RAG │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ baseline in terms of the comprehensiveness and diversity of answers. This is particularly beneficial for global │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ sensemaking questions over large datasets. │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ 2. **Scalability**: Graph RAG provides scalability advantages, achieving efficient summarization with │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ significantly fewer context tokens required. For instance, it requires 26-33% fewer tokens for low-level │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ community summaries and over 97% fewer tokens for root-level summaries compared to source text summarization. │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ 3. **Efficiency in Iterative Question Answering**: Root-level Graph RAG offers a highly efficient method for │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ iterative question answering, which is crucial for sensemaking activities, with only a modest drop in │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ performance compared to other global methods. │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ 4. **Global Query Handling**: It supports handling global queries effectively, as it combines knowledge graph │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ generation, retrieval-augmented generation, and query-focused summarization, making it suitable for sensemaking │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ over entire text corpora. │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ 5. **Hierarchical Indexing and Summarization**: The use of a hierarchical index and summarization allows for │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ efficient processing and summarizing of community summaries into a final global answer, facilitating a │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ comprehensive coverage of the underlying graph index and input documents. │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ 6. **Reduced Token Cost**: For situations requiring many global queries over the same dataset, Graph RAG │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">│ achieves competitive performance to other global methods at a fraction of the token cost. │</span>\n",
|
||||||
|
"<span style=\"color: #008000; text-decoration-color: #008000; font-weight: bold\">╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯</span>\n",
|
||||||
|
"</pre>\n"
|
||||||
|
],
|
||||||
|
"text/plain": [
|
||||||
|
"\u001b[1;32m╭─\u001b[0m\u001b[1;32m────────────────────────────────────────────────\u001b[0m RAG Response \u001b[1;32m─────────────────────────────────────────────────\u001b[0m\u001b[1;32m─╮\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mThe main advantages of using the Graph RAG approach for query-focused summarization compared to traditional RAG\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mmethods include:\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m1. **Improved Comprehensiveness and Diversity**: Graph RAG shows substantial improvements over a naïve RAG \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mbaseline in terms of the comprehensiveness and diversity of answers. This is particularly beneficial for global\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32msensemaking questions over large datasets.\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m2. **Scalability**: Graph RAG provides scalability advantages, achieving efficient summarization with \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32msignificantly fewer context tokens required. For instance, it requires 26-33% fewer tokens for low-level \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mcommunity summaries and over 97% fewer tokens for root-level summaries compared to source text summarization.\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m3. **Efficiency in Iterative Question Answering**: Root-level Graph RAG offers a highly efficient method for \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32miterative question answering, which is crucial for sensemaking activities, with only a modest drop in \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mperformance compared to other global methods.\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m4. **Global Query Handling**: It supports handling global queries effectively, as it combines knowledge graph \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mgeneration, retrieval-augmented generation, and query-focused summarization, making it suitable for sensemaking\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mover entire text corpora.\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m5. **Hierarchical Indexing and Summarization**: The use of a hierarchical index and summarization allows for \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mefficient processing and summarizing of community summaries into a final global answer, facilitating a \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32mcomprehensive coverage of the underlying graph index and input documents.\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m6. **Reduced Token Cost**: For situations requiring many global queries over the same dataset, Graph RAG \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m│\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32machieves competitive performance to other global methods at a fraction of the token cost.\u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m \u001b[0m\u001b[1;32m│\u001b[0m\n",
|
||||||
|
"\u001b[1;32m╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from azure.search.documents.models import VectorizableTextQuery\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def generate_chat_response(prompt: str, system_message: str = None):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Generates a single-turn chat response using Azure OpenAI Chat.\n",
|
||||||
|
" If you need multi-turn conversation or follow-up queries, you'll have to\n",
|
||||||
|
" maintain the messages list externally.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" messages = []\n",
|
||||||
|
" if system_message:\n",
|
||||||
|
" messages.append({\"role\": \"system\", \"content\": system_message})\n",
|
||||||
|
" messages.append({\"role\": \"user\", \"content\": prompt})\n",
|
||||||
|
"\n",
|
||||||
|
" completion = openai_client.chat.completions.create(\n",
|
||||||
|
" model=AZURE_OPENAI_CHAT_MODEL, messages=messages, temperature=0.7\n",
|
||||||
|
" )\n",
|
||||||
|
" return completion.choices[0].message.content\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"user_query = \"What are the main advantages of using the Graph RAG approach for query-focused summarization compared to traditional RAG methods?\"\n",
|
||||||
|
"user_embed = embed_text(user_query)\n",
|
||||||
|
"\n",
|
||||||
|
"vector_query = VectorizableTextQuery(\n",
|
||||||
|
" text=user_query, # passing in text for a hybrid search\n",
|
||||||
|
" k_nearest_neighbors=5,\n",
|
||||||
|
" fields=\"content_vector\",\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"search_results = search_client.search(\n",
|
||||||
|
" search_text=user_query, vector_queries=[vector_query], select=[\"content\"], top=10\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"retrieved_chunks = []\n",
|
||||||
|
"for result in search_results:\n",
|
||||||
|
" snippet = result[\"content\"]\n",
|
||||||
|
" retrieved_chunks.append(snippet)\n",
|
||||||
|
"\n",
|
||||||
|
"context_str = \"\\n---\\n\".join(retrieved_chunks)\n",
|
||||||
|
"rag_prompt = f\"\"\"\n",
|
||||||
|
"You are an AI assistant helping answering questions about Microsoft GraphRAG.\n",
|
||||||
|
"Use ONLY the text below to answer the user's question.\n",
|
||||||
|
"If the answer isn't in the text, say you don't know.\n",
|
||||||
|
"\n",
|
||||||
|
"Context:\n",
|
||||||
|
"{context_str}\n",
|
||||||
|
"\n",
|
||||||
|
"Question: {user_query}\n",
|
||||||
|
"Answer:\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"final_answer = generate_chat_response(rag_prompt)\n",
|
||||||
|
"\n",
|
||||||
|
"console.print(Panel(rag_prompt, title=\"RAG Prompt\", style=\"bold red\"))\n",
|
||||||
|
"console.print(Panel(final_answer, title=\"RAG Response\", style=\"bold green\"))"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"accelerator": "GPU",
|
||||||
|
"colab": {
|
||||||
|
"gpuType": "T4",
|
||||||
|
"provenance": []
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": ".venv",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.12.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
@ -14,6 +14,17 @@
|
|||||||
"# RAG with Haystack"
|
"# RAG with Haystack"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"| Step | Tech | Execution | \n",
|
||||||
|
"| --- | --- | --- |\n",
|
||||||
|
"| Embedding | Hugging Face / Sentence Transformers | 💻 Local |\n",
|
||||||
|
"| Vector store | Milvus | 💻 Local |\n",
|
||||||
|
"| Gen AI | Hugging Face Inference API | 🌐 Remote | "
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -90,6 +101,7 @@
|
|||||||
"from docling_haystack.converter import ExportType\n",
|
"from docling_haystack.converter import ExportType\n",
|
||||||
"from dotenv import load_dotenv\n",
|
"from dotenv import load_dotenv\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"def _get_env_from_colab_or_os(key):\n",
|
"def _get_env_from_colab_or_os(key):\n",
|
||||||
" try:\n",
|
" try:\n",
|
||||||
" from google.colab import userdata\n",
|
" from google.colab import userdata\n",
|
||||||
@ -102,6 +114,7 @@
|
|||||||
" pass\n",
|
" pass\n",
|
||||||
" return os.getenv(key)\n",
|
" return os.getenv(key)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"load_dotenv()\n",
|
"load_dotenv()\n",
|
||||||
"HF_TOKEN = _get_env_from_colab_or_os(\"HF_TOKEN\")\n",
|
"HF_TOKEN = _get_env_from_colab_or_os(\"HF_TOKEN\")\n",
|
||||||
"PATHS = [\"https://arxiv.org/pdf/2408.09869\"] # Docling Technical Report\n",
|
"PATHS = [\"https://arxiv.org/pdf/2408.09869\"] # Docling Technical Report\n",
|
||||||
|
@ -4,7 +4,63 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# RAG with LangChain 🦜🔗"
|
"<a href=\"https://colab.research.google.com/github/DS4SD/docling/blob/main/docs/examples/rag_langchain.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# RAG with LangChain"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"| Step | Tech | Execution | \n",
|
||||||
|
"| --- | --- | --- |\n",
|
||||||
|
"| Embedding | Hugging Face / Sentence Transformers | 💻 Local |\n",
|
||||||
|
"| Vector store | Milvus | 💻 Local |\n",
|
||||||
|
"| Gen AI | Hugging Face Inference API | 🌐 Remote | "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"This example leverages the\n",
|
||||||
|
"[LangChain Docling integration](../../integrations/langchain/), along with a Milvus\n",
|
||||||
|
"vector store, as well as sentence-transformers embeddings.\n",
|
||||||
|
"\n",
|
||||||
|
"The presented `DoclingLoader` component enables you to:\n",
|
||||||
|
"- use various document types in your LLM applications with ease and speed, and\n",
|
||||||
|
"- leverage Docling's rich format for advanced, document-native grounding.\n",
|
||||||
|
"\n",
|
||||||
|
"`DoclingLoader` supports two different export modes:\n",
|
||||||
|
"- `ExportType.MARKDOWN`: if you want to capture each input document as a separate\n",
|
||||||
|
" LangChain document, or\n",
|
||||||
|
"- `ExportType.DOC_CHUNKS` (default): if you want to have each input document chunked and\n",
|
||||||
|
" to then capture each individual chunk as a separate LangChain document downstream.\n",
|
||||||
|
"\n",
|
||||||
|
"The example allows exploring both modes via parameter `EXPORT_TYPE`; depending on the\n",
|
||||||
|
"value set, the example pipeline is then set up accordingly."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Setup"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"- 👉 For best conversion speed, use GPU acceleration whenever available; e.g. if running on Colab, use GPU-enabled runtime.\n",
|
||||||
|
"- Notebook uses HuggingFace's Inference API; for increased LLM quota, token can be provided via env var `HF_TOKEN`.\n",
|
||||||
|
"- Requirements can be installed as shown below (`--no-warn-conflicts` meant for Colab's pre-populated Python env; feel free to remove for stricter usage):"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -21,81 +77,105 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# requirements for this example:\n",
|
"%pip install -q --progress-bar off --no-warn-conflicts langchain-docling langchain-core langchain-huggingface langchain_milvus langchain python-dotenv"
|
||||||
"%pip install -qq docling docling-core python-dotenv langchain-text-splitters langchain-huggingface langchain-milvus"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 2,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"True"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 2,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"import os\n",
|
"import os\n",
|
||||||
|
"from pathlib import Path\n",
|
||||||
|
"from tempfile import mkdtemp\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from dotenv import load_dotenv\n",
|
"from dotenv import load_dotenv\n",
|
||||||
|
"from langchain_core.prompts import PromptTemplate\n",
|
||||||
|
"from langchain_docling.loader import ExportType\n",
|
||||||
"\n",
|
"\n",
|
||||||
"load_dotenv()"
|
"\n",
|
||||||
|
"def _get_env_from_colab_or_os(key):\n",
|
||||||
|
" try:\n",
|
||||||
|
" from google.colab import userdata\n",
|
||||||
|
"\n",
|
||||||
|
" try:\n",
|
||||||
|
" return userdata.get(key)\n",
|
||||||
|
" except userdata.SecretNotFoundError:\n",
|
||||||
|
" pass\n",
|
||||||
|
" except ImportError:\n",
|
||||||
|
" pass\n",
|
||||||
|
" return os.getenv(key)\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"load_dotenv()\n",
|
||||||
|
"\n",
|
||||||
|
"# https://github.com/huggingface/transformers/issues/5486:\n",
|
||||||
|
"os.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\n",
|
||||||
|
"\n",
|
||||||
|
"HF_TOKEN = _get_env_from_colab_or_os(\"HF_TOKEN\")\n",
|
||||||
|
"FILE_PATH = [\"https://arxiv.org/pdf/2408.09869\"] # Docling Technical Report\n",
|
||||||
|
"EMBED_MODEL_ID = \"sentence-transformers/all-MiniLM-L6-v2\"\n",
|
||||||
|
"GEN_MODEL_ID = \"mistralai/Mixtral-8x7B-Instruct-v0.1\"\n",
|
||||||
|
"EXPORT_TYPE = ExportType.DOC_CHUNKS\n",
|
||||||
|
"QUESTION = \"Which are the main AI models in Docling?\"\n",
|
||||||
|
"PROMPT = PromptTemplate.from_template(\n",
|
||||||
|
" \"Context information is below.\\n---------------------\\n{context}\\n---------------------\\nGiven the context information and not prior knowledge, answer the query.\\nQuery: {input}\\nAnswer:\\n\",\n",
|
||||||
|
")\n",
|
||||||
|
"TOP_K = 3\n",
|
||||||
|
"MILVUS_URI = str(Path(mkdtemp()) / \"docling.db\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Setup"
|
"## Document loading\n",
|
||||||
]
|
"\n",
|
||||||
},
|
"Now we can instantiate our loader and load documents."
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### Loader and splitter"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"Below we set up:\n",
|
|
||||||
"- a `Loader` which will be used to create LangChain documents, and\n",
|
|
||||||
"- a splitter, which will be used to split these documents"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Token indices sequence length is longer than the specified maximum sequence length for this model (1041 > 512). Running this sequence through the model will result in indexing errors\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from typing import Iterator\n",
|
"from langchain_docling import DoclingLoader\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_core.document_loaders import BaseLoader\n",
|
"from docling.chunking import HybridChunker\n",
|
||||||
"from langchain_core.documents import Document as LCDocument\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"from docling.document_converter import DocumentConverter\n",
|
"loader = DoclingLoader(\n",
|
||||||
|
" file_path=FILE_PATH,\n",
|
||||||
|
" export_type=EXPORT_TYPE,\n",
|
||||||
|
" chunker=HybridChunker(tokenizer=EMBED_MODEL_ID),\n",
|
||||||
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class DoclingPDFLoader(BaseLoader):\n",
|
"docs = loader.load()"
|
||||||
"\n",
|
]
|
||||||
" def __init__(self, file_path: str | list[str]) -> None:\n",
|
},
|
||||||
" self._file_paths = file_path if isinstance(file_path, list) else [file_path]\n",
|
{
|
||||||
" self._converter = DocumentConverter()\n",
|
"cell_type": "markdown",
|
||||||
"\n",
|
"metadata": {},
|
||||||
" def lazy_load(self) -> Iterator[LCDocument]:\n",
|
"source": [
|
||||||
" for source in self._file_paths:\n",
|
"> Note: a message saying `\"Token indices sequence length is longer than the specified\n",
|
||||||
" dl_doc = self._converter.convert(source).document\n",
|
"maximum sequence length...\"` can be ignored in this case — details\n",
|
||||||
" text = dl_doc.export_to_markdown()\n",
|
"[here](https://github.com/DS4SD/docling-core/issues/119#issuecomment-2577418826)."
|
||||||
" yield LCDocument(page_content=text)"
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Determining the splits:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -104,29 +184,57 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"FILE_PATH = \"https://raw.githubusercontent.com/DS4SD/docling/main/tests/data/2206.01062.pdf\" # DocLayNet paper"
|
"if EXPORT_TYPE == ExportType.DOC_CHUNKS:\n",
|
||||||
]
|
" splits = docs\n",
|
||||||
},
|
"elif EXPORT_TYPE == ExportType.MARKDOWN:\n",
|
||||||
{
|
" from langchain_text_splitters import MarkdownHeaderTextSplitter\n",
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 5,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"loader = DoclingPDFLoader(file_path=FILE_PATH)\n",
|
" splitter = MarkdownHeaderTextSplitter(\n",
|
||||||
"text_splitter = RecursiveCharacterTextSplitter(\n",
|
" headers_to_split_on=[\n",
|
||||||
" chunk_size=1000,\n",
|
" (\"#\", \"Header_1\"),\n",
|
||||||
" chunk_overlap=200,\n",
|
" (\"##\", \"Header_2\"),\n",
|
||||||
")"
|
" (\"###\", \"Header_3\"),\n",
|
||||||
|
" ],\n",
|
||||||
|
" )\n",
|
||||||
|
" splits = [split for doc in docs for split in splitter.split_text(doc.page_content)]\n",
|
||||||
|
"else:\n",
|
||||||
|
" raise ValueError(f\"Unexpected export type: {EXPORT_TYPE}\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"We now used the above-defined objects to get the document splits:"
|
"Inspecting some sample splits:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"- d.page_content='arXiv:2408.09869v5 [cs.CL] 9 Dec 2024'\n",
|
||||||
|
"- d.page_content='Docling Technical Report\\nVersion 1.0\\nChristoph Auer Maksym Lysak Ahmed Nassar Michele Dolfi Nikolaos Livathinos Panos Vagenas Cesar Berrospi Ramis Matteo Omenetti Fabian Lindlbauer Kasper Dinkla Lokesh Mishra Yusik Kim Shubham Gupta Rafael Teixeira de Lima Valery Weber Lucas Morin Ingmar Meijer Viktor Kuropiatnyk Peter W. J. Staar\\nAI4K Group, IBM Research R¨uschlikon, Switzerland'\n",
|
||||||
|
"- d.page_content='Abstract\\nThis technical report introduces Docling , an easy to use, self-contained, MITlicensed open-source package for PDF document conversion. It is powered by state-of-the-art specialized AI models for layout analysis (DocLayNet) and table structure recognition (TableFormer), and runs efficiently on commodity hardware in a small resource budget. The code interface allows for easy extensibility and addition of new features and models.'\n",
|
||||||
|
"...\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"for d in splits[:3]:\n",
|
||||||
|
" print(f\"- {d.page_content=}\")\n",
|
||||||
|
"print(\"...\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Ingestion"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -135,93 +243,27 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"docs = loader.load()\n",
|
"import json\n",
|
||||||
"splits = text_splitter.split_documents(docs)"
|
"from pathlib import Path\n",
|
||||||
]
|
"from tempfile import mkdtemp\n",
|
||||||
},
|
"\n",
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### Embeddings"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 7,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from langchain_huggingface.embeddings import HuggingFaceEmbeddings\n",
|
"from langchain_huggingface.embeddings import HuggingFaceEmbeddings\n",
|
||||||
"\n",
|
|
||||||
"HF_EMBED_MODEL_ID = \"BAAI/bge-small-en-v1.5\"\n",
|
|
||||||
"embeddings = HuggingFaceEmbeddings(model_name=HF_EMBED_MODEL_ID)"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### Vector store"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 8,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"from tempfile import TemporaryDirectory\n",
|
|
||||||
"\n",
|
|
||||||
"from langchain_milvus import Milvus\n",
|
"from langchain_milvus import Milvus\n",
|
||||||
"\n",
|
"\n",
|
||||||
"MILVUS_URI = os.environ.get(\n",
|
"embedding = HuggingFaceEmbeddings(model_name=EMBED_MODEL_ID)\n",
|
||||||
" \"MILVUS_URI\", f\"{(tmp_dir := TemporaryDirectory()).name}/milvus_demo.db\"\n",
|
|
||||||
")\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"milvus_uri = str(Path(mkdtemp()) / \"docling.db\") # or set as needed\n",
|
||||||
"vectorstore = Milvus.from_documents(\n",
|
"vectorstore = Milvus.from_documents(\n",
|
||||||
" splits,\n",
|
" documents=splits,\n",
|
||||||
" embeddings,\n",
|
" embedding=embedding,\n",
|
||||||
" connection_args={\"uri\": MILVUS_URI},\n",
|
" collection_name=\"docling_demo\",\n",
|
||||||
|
" connection_args={\"uri\": milvus_uri},\n",
|
||||||
|
" index_params={\"index_type\": \"FLAT\"},\n",
|
||||||
" drop_old=True,\n",
|
" drop_old=True,\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "markdown",
|
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
|
||||||
"### LLM"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 9,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.\n",
|
|
||||||
"Token is valid (permission: write).\n",
|
|
||||||
"Your token has been saved to /Users/pva/.cache/huggingface/token\n",
|
|
||||||
"Login successful\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"from langchain_huggingface import HuggingFaceEndpoint\n",
|
|
||||||
"\n",
|
|
||||||
"HF_API_KEY = os.environ.get(\"HF_API_KEY\")\n",
|
|
||||||
"HF_LLM_MODEL_ID = \"mistralai/Mistral-7B-Instruct-v0.3\"\n",
|
|
||||||
"\n",
|
|
||||||
"llm = HuggingFaceEndpoint(\n",
|
|
||||||
" repo_id=HF_LLM_MODEL_ID,\n",
|
|
||||||
" huggingfacehub_api_token=HF_API_KEY,\n",
|
|
||||||
")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -231,55 +273,89 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": 7,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Note: Environment variable`HF_TOKEN` is set and is the current active token independently from the token you've just configured.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"from typing import Iterable\n",
|
"from langchain.chains import create_retrieval_chain\n",
|
||||||
|
"from langchain.chains.combine_documents import create_stuff_documents_chain\n",
|
||||||
|
"from langchain_huggingface import HuggingFaceEndpoint\n",
|
||||||
"\n",
|
"\n",
|
||||||
"from langchain_core.documents import Document as LCDocument\n",
|
"retriever = vectorstore.as_retriever(search_kwargs={\"k\": TOP_K})\n",
|
||||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
"llm = HuggingFaceEndpoint(\n",
|
||||||
"from langchain_core.prompts import PromptTemplate\n",
|
" repo_id=GEN_MODEL_ID,\n",
|
||||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
" huggingfacehub_api_token=HF_TOKEN,\n",
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"def format_docs(docs: Iterable[LCDocument]):\n",
|
|
||||||
" return \"\\n\\n\".join(doc.page_content for doc in docs)\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"retriever = vectorstore.as_retriever()\n",
|
|
||||||
"\n",
|
|
||||||
"prompt = PromptTemplate.from_template(\n",
|
|
||||||
" \"Context information is below.\\n---------------------\\n{context}\\n---------------------\\nGiven the context information and not prior knowledge, answer the query.\\nQuery: {question}\\nAnswer:\\n\"\n",
|
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"rag_chain = (\n",
|
"\n",
|
||||||
" {\"context\": retriever | format_docs, \"question\": RunnablePassthrough()}\n",
|
"def clip_text(text, threshold=100):\n",
|
||||||
" | prompt\n",
|
" return f\"{text[:threshold]}...\" if len(text) > threshold else text"
|
||||||
" | llm\n",
|
|
||||||
" | StrOutputParser()\n",
|
|
||||||
")"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": 8,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"name": "stdout",
|
||||||
"text/plain": [
|
"output_type": "stream",
|
||||||
"'- 80,863 pages were human annotated for DocLayNet.'"
|
"text": [
|
||||||
]
|
"Question:\n",
|
||||||
},
|
"Which are the main AI models in Docling?\n",
|
||||||
"execution_count": 11,
|
"\n",
|
||||||
"metadata": {},
|
"Answer:\n",
|
||||||
"output_type": "execute_result"
|
"Docling initially releases two AI models, 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 tab...\n",
|
||||||
|
"\n",
|
||||||
|
"Source 1:\n",
|
||||||
|
" text: \"3.2 AI models\\nAs 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 re...\"\n",
|
||||||
|
" dl_meta: {'schema_name': 'docling_core.transforms.chunker.DocMeta', 'version': '1.0.0', 'doc_items': [{'self_ref': '#/texts/50', 'parent': {'$ref': '#/body'}, 'children': [], 'label': 'text', 'prov': [{'page_no': 3, 'bbox': {'l': 108.0, 't': 405.1419982910156, 'r': 504.00299072265625, 'b': 330.7799987792969, 'coord_origin': 'BOTTOMLEFT'}, 'charspan': [0, 608]}]}], 'headings': ['3.2 AI models'], 'origin': {'mimetype': 'application/pdf', 'binary_hash': 11465328351749295394, 'filename': '2408.09869v5.pdf'}}\n",
|
||||||
|
" source: https://arxiv.org/pdf/2408.09869\n",
|
||||||
|
"\n",
|
||||||
|
"Source 2:\n",
|
||||||
|
" text: \"3 Processing pipeline\\nDocling implements a linear pipeline of operations, which execute sequentially on each given document (see Fig. 1). Each document is first parsed by a PDF backend, which retrieves the programmatic text tokens, consisting of string content and its coordinates on the page, and also renders a bitmap image of each page to support ...\"\n",
|
||||||
|
" dl_meta: {'schema_name': 'docling_core.transforms.chunker.DocMeta', 'version': '1.0.0', 'doc_items': [{'self_ref': '#/texts/26', 'parent': {'$ref': '#/body'}, 'children': [], 'label': 'text', 'prov': [{'page_no': 2, 'bbox': {'l': 108.0, 't': 273.01800537109375, 'r': 504.00299072265625, 'b': 176.83799743652344, 'coord_origin': 'BOTTOMLEFT'}, 'charspan': [0, 796]}]}], 'headings': ['3 Processing pipeline'], 'origin': {'mimetype': 'application/pdf', 'binary_hash': 11465328351749295394, 'filename': '2408.09869v5.pdf'}}\n",
|
||||||
|
" source: https://arxiv.org/pdf/2408.09869\n",
|
||||||
|
"\n",
|
||||||
|
"Source 3:\n",
|
||||||
|
" text: \"6 Future work and contributions\\nDocling is designed to allow easy extension of the model library and pipelines. In the future, we plan to extend Docling with several more models, such as a figure-classifier model, an equationrecognition model, a code-recognition model and more. This will help improve the quality of conversion for specific types of ...\"\n",
|
||||||
|
" dl_meta: {'schema_name': 'docling_core.transforms.chunker.DocMeta', 'version': '1.0.0', 'doc_items': [{'self_ref': '#/texts/76', 'parent': {'$ref': '#/body'}, 'children': [], 'label': 'text', 'prov': [{'page_no': 5, 'bbox': {'l': 108.0, 't': 322.468994140625, 'r': 504.00299072265625, 'b': 259.0169982910156, 'coord_origin': 'BOTTOMLEFT'}, 'charspan': [0, 543]}]}, {'self_ref': '#/texts/77', 'parent': {'$ref': '#/body'}, 'children': [], 'label': 'text', 'prov': [{'page_no': 5, 'bbox': {'l': 108.0, 't': 251.6540069580078, 'r': 504.00299072265625, 'b': 198.99200439453125, 'coord_origin': 'BOTTOMLEFT'}, 'charspan': [0, 402]}]}], 'headings': ['6 Future work and contributions'], 'origin': {'mimetype': 'application/pdf', 'binary_hash': 11465328351749295394, 'filename': '2408.09869v5.pdf'}}\n",
|
||||||
|
" source: https://arxiv.org/pdf/2408.09869\n"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"rag_chain.invoke(\"How many pages were human annotated for DocLayNet?\")"
|
"question_answer_chain = create_stuff_documents_chain(llm, PROMPT)\n",
|
||||||
|
"rag_chain = create_retrieval_chain(retriever, question_answer_chain)\n",
|
||||||
|
"resp_dict = rag_chain.invoke({\"input\": QUESTION})\n",
|
||||||
|
"\n",
|
||||||
|
"clipped_answer = clip_text(resp_dict[\"answer\"], threshold=200)\n",
|
||||||
|
"print(f\"Question:\\n{resp_dict['input']}\\n\\nAnswer:\\n{clipped_answer}\")\n",
|
||||||
|
"for i, doc in enumerate(resp_dict[\"context\"]):\n",
|
||||||
|
" print()\n",
|
||||||
|
" print(f\"Source {i+1}:\")\n",
|
||||||
|
" print(f\" text: {json.dumps(clip_text(doc.page_content, threshold=350))}\")\n",
|
||||||
|
" for key in doc.metadata:\n",
|
||||||
|
" if key != \"pk\":\n",
|
||||||
|
" val = doc.metadata.get(key)\n",
|
||||||
|
" clipped_val = clip_text(val) if isinstance(val, str) else val\n",
|
||||||
|
" print(f\" {key}: {clipped_val}\")"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -298,7 +374,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.12.4"
|
"version": "3.12.8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -11,7 +11,18 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# RAG with LlamaIndex 🦙"
|
"# RAG with LlamaIndex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"| Step | Tech | Execution | \n",
|
||||||
|
"| --- | --- | --- |\n",
|
||||||
|
"| Embedding | Hugging Face / Sentence Transformers | 💻 Local |\n",
|
||||||
|
"| Vector store | Milvus | 💻 Local |\n",
|
||||||
|
"| Gen AI | Hugging Face Inference API | 🌐 Remote | "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -462,7 +473,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.12.4"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -1,14 +1,33 @@
|
|||||||
{
|
{
|
||||||
"cells": [
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"[](https://colab.research.google.com/github/DS4SD/docling/blob/main/docs/examples/rag_weaviate.ipynb)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"id": "Ag9kcX2B_atc"
|
"id": "Ag9kcX2B_atc"
|
||||||
},
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"# Performing RAG over PDFs with Weaviate and Docling\n",
|
"# RAG with Weaviate"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"\n",
|
||||||
|
"| Step | Tech | Execution | \n",
|
||||||
|
"| --- | --- | --- |\n",
|
||||||
|
"| Embedding | Open AI | 🌐 Remote |\n",
|
||||||
|
"| Vector store | Weavieate | 💻 Local |\n",
|
||||||
|
"| Gen AI | Open AI | 🌐 Remote |\n",
|
||||||
|
"\n",
|
||||||
"## A recipe 🧑🍳 🐥 💚\n",
|
"## A recipe 🧑🍳 🐥 💚\n",
|
||||||
"[](https://colab.research.google.com/github/DS4SD/docling/blob/tree/main/docs/examples/rag_weaviate.ipynb)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"This is a code recipe that uses [Weaviate](https://weaviate.io/) to perform RAG over PDF documents parsed by [Docling](https://ds4sd.github.io/docling/).\n",
|
"This is a code recipe that uses [Weaviate](https://weaviate.io/) to perform RAG over PDF documents parsed by [Docling](https://ds4sd.github.io/docling/).\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -711,7 +730,8 @@
|
|||||||
"provenance": []
|
"provenance": []
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3",
|
"display_name": ".venv",
|
||||||
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
@ -724,7 +744,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.11.10"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -12,7 +12,17 @@
|
|||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"# Hybrid RAG with Qdrant"
|
"# Retrieval with Qdrant"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"| Step | Tech | Execution | \n",
|
||||||
|
"| --- | --- | --- |\n",
|
||||||
|
"| Embedding | FastEmbed | 💻 Local |\n",
|
||||||
|
"| Vector store | Qdrant | 💻 Local |"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -47,22 +57,19 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
|
||||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.3.1\u001b[0m\n",
|
|
||||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
|
|
||||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --no-warn-conflicts -q qdrant-client docling docling-core fastembed"
|
"%pip install --no-warn-conflicts -q qdrant-client docling fastembed"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -74,13 +81,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 2,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from docling_core.transforms.chunker import HierarchicalChunker\n",
|
|
||||||
"from qdrant_client import QdrantClient\n",
|
"from qdrant_client import QdrantClient\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"from docling.chunking import HybridChunker\n",
|
||||||
"from docling.datamodel.base_models import InputFormat\n",
|
"from docling.datamodel.base_models import InputFormat\n",
|
||||||
"from docling.document_converter import DocumentConverter"
|
"from docling.document_converter import DocumentConverter"
|
||||||
]
|
]
|
||||||
@ -95,36 +102,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"name": "stderr",
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
"output_type": "stream",
|
||||||
"model_id": "c1077c6634d9434584c41cc12f9107c9",
|
"text": [
|
||||||
"version_major": 2,
|
"/Users/pva/work/github.com/DS4SD/docling/.venv/lib/python3.12/site-packages/huggingface_hub/utils/tqdm.py:155: UserWarning: Cannot enable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=1` is set and has priority.\n",
|
||||||
"version_minor": 0
|
" warnings.warn(\n"
|
||||||
},
|
]
|
||||||
"text/plain": [
|
|
||||||
"Fetching 5 files: 0%| | 0/5 [00:00<?, ?it/s]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"application/vnd.jupyter.widget-view+json": {
|
|
||||||
"model_id": "67069c07b73448d491944452159d10bc",
|
|
||||||
"version_major": 2,
|
|
||||||
"version_minor": 0
|
|
||||||
},
|
|
||||||
"text/plain": [
|
|
||||||
"Fetching 29 files: 0%| | 0/29 [00:00<?, ?it/s]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
@ -149,7 +136,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 4,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
@ -157,7 +144,7 @@
|
|||||||
" \"https://www.sagacify.com/news/a-guide-to-chunking-strategies-for-retrieval-augmented-generation-rag\"\n",
|
" \"https://www.sagacify.com/news/a-guide-to-chunking-strategies-for-retrieval-augmented-generation-rag\"\n",
|
||||||
")\n",
|
")\n",
|
||||||
"documents, metadatas = [], []\n",
|
"documents, metadatas = [], []\n",
|
||||||
"for chunk in HierarchicalChunker().chunk(result.document):\n",
|
"for chunk in HybridChunker().chunk(result.document):\n",
|
||||||
" documents.append(chunk.text)\n",
|
" documents.append(chunk.text)\n",
|
||||||
" metadatas.append(chunk.meta.export_json_dict())"
|
" metadatas.append(chunk.meta.export_json_dict())"
|
||||||
]
|
]
|
||||||
@ -173,95 +160,119 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"['e74ae15be5eb4805858307846318e784',\n",
|
|
||||||
" 'f83f6125b0fa4a0595ae6a0777c9d90d',\n",
|
|
||||||
" '9cf63c7f30764715bf3804a19db36d7d',\n",
|
|
||||||
" '007dbe6d355b4b49af3b736cbd63a4d8',\n",
|
|
||||||
" 'e5e31f21f2e84aa68beca0dfc532cbe9',\n",
|
|
||||||
" '69c10816af204bb28630a1f957d8dd3e',\n",
|
|
||||||
" 'b63546b9b1744063bdb076b234d883ca',\n",
|
|
||||||
" '90ad15ba8fa6494489e1d3221e30bfcf',\n",
|
|
||||||
" '13517debb483452ea40fc7aa04c08c50',\n",
|
|
||||||
" '84ccab5cfab74e27a55acef1c63e3fad',\n",
|
|
||||||
" 'e8aa2ef46d234c5a8a9da64b701d60b4',\n",
|
|
||||||
" '190bea5ba43c45e792197c50898d1d90',\n",
|
|
||||||
" 'a730319ea65645ca81e735ace0bcc72e',\n",
|
|
||||||
" '415e7f6f15864e30b836e23ae8d71b43',\n",
|
|
||||||
" '5569bce4e65541868c762d149c6f491e',\n",
|
|
||||||
" '74d9b234e9c04ebeb8e4e1ca625789ac',\n",
|
|
||||||
" '308b1c5006a94a679f4c8d6f2396993c',\n",
|
|
||||||
" 'aaa5ec6d385a418388e660c425bf1dbe',\n",
|
|
||||||
" '630be8e43e4e4472a9cdb9af9462a43a',\n",
|
|
||||||
" '643b316224de4770a5349bf69cf93471',\n",
|
|
||||||
" 'da9265e6f6c2485493d15223eefdf411',\n",
|
|
||||||
" 'a916e447d52c4084b5ce81a0c5a65b07',\n",
|
|
||||||
" '2883c620858e4e728b88e127155a4f2c',\n",
|
|
||||||
" '2a998f0e9c124af99027060b94027874',\n",
|
|
||||||
" 'be551fbd2b9e42f48ebae0cbf1f481bc',\n",
|
|
||||||
" '95b7f7608e974ca6847097ee4590fba1',\n",
|
|
||||||
" '309db4f3863b4e3aaf16d5f346c309f3',\n",
|
|
||||||
" 'c818383267f64fd68b2237b024bd724e',\n",
|
|
||||||
" '1f16e78338c94238892171b400051cd4',\n",
|
|
||||||
" '25c680c3e064462cab071ea9bf1bad8c',\n",
|
|
||||||
" 'f41ab7e480a248c6bb87019341c7ca74',\n",
|
|
||||||
" 'd440128bed6d4dcb987152b48ecd9a8a',\n",
|
|
||||||
" 'c110d5dfdc5849808851788c2404dd15']"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 4,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"client.add(COLLECTION_NAME, documents=documents, metadata=metadatas, batch_size=64)"
|
"_ = client.add(\n",
|
||||||
|
" collection_name=COLLECTION_NAME,\n",
|
||||||
|
" documents=documents,\n",
|
||||||
|
" metadata=metadatas,\n",
|
||||||
|
" batch_size=64,\n",
|
||||||
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Query Documents"
|
"## Retrieval"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"points = client.query(\n",
|
||||||
|
" collection_name=COLLECTION_NAME,\n",
|
||||||
|
" query_text=\"Can I split documents?\",\n",
|
||||||
|
" limit=10,\n",
|
||||||
|
")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"<=== Retrieved documents ===>\n",
|
"=== 0 ===\n",
|
||||||
"Document Specific Chunking is a strategy that respects the document's structure. Rather than using a set number of characters or a recursive process, it creates chunks that align with the logical sections of the document, like paragraphs or subsections. This approach maintains the original author's organization of content and helps keep the text coherent. It makes the retrieved information more relevant and useful, particularly for structured documents with clearly defined sections.\n",
|
"Have you ever wondered how we, humans, would chunk? Here's a breakdown of a possible way a human would process a new document:\n",
|
||||||
"Document Specific Chunking can handle a variety of document formats, such as:\n",
|
|
||||||
"Consequently, there are also splitters available for this purpose.\n",
|
|
||||||
"1. We start at the top of the document, treating the first part as a chunk.\n",
|
"1. We start at the top of the document, treating the first part as a chunk.\n",
|
||||||
" 2. We continue down the document, deciding if a new sentence or piece of information belongs with the first chunk or should start a new one.\n",
|
" 2. We continue down the document, deciding if a new sentence or piece of information belongs with the first chunk or should start a new one.\n",
|
||||||
" 3. We keep this up until we reach the end of the document.\n",
|
" 3. We keep this up until we reach the end of the document.\n",
|
||||||
"Have you ever wondered how we, humans, would chunk? Here's a breakdown of a possible way a human would process a new document:\n",
|
"The ultimate dream? Having an agent do this for you. But slow down! This approach is still being tested and isn't quite ready for the big leagues due to the time it takes to process multiple LLM calls and the cost of those calls. There's no implementation available in public libraries just yet. However, Greg Kamradt has his version available here.\n",
|
||||||
"The goal of chunking is, as its name says, to chunk the information into multiple smaller pieces in order to store it in a more efficient and meaningful way. This allows the retrieval to capture pieces of information that are more related to the question at hand, and the generation to be more precise, but also less costly, as only a part of a document will be included in the LLM prompt, instead of the whole document.\n",
|
"\n",
|
||||||
"To put these strategies into action, there's a whole array of tools and libraries at your disposal. For example, llama_index is a fantastic tool that lets you create document indices and retrieve chunked documents. Let's not forget LangChain, another remarkable tool that makes implementing chunking strategies a breeze, particularly when dealing with multi-language data. Diving into these tools and understanding how they can work in harmony with the chunking strategies we've discussed is a crucial part of mastering Retrieval Augmented Generation.\n",
|
"=== 1 ===\n",
|
||||||
"Semantic chunking involves taking the embeddings of every sentence in the document, comparing the similarity of all sentences with each other, and then grouping sentences with the most similar embeddings together.\n",
|
"Document Specific Chunking is a strategy that respects the document's structure. Rather than using a set number of characters or a recursive process, it creates chunks that align with the logical sections of the document, like paragraphs or subsections. This approach maintains the original author's organization of content and helps keep the text coherent. It makes the retrieved information more relevant and useful, particularly for structured documents with clearly defined sections.\n",
|
||||||
|
"Document Specific Chunking can handle a variety of document formats, such as:\n",
|
||||||
|
"Markdown\n",
|
||||||
|
"HTML\n",
|
||||||
|
"Python\n",
|
||||||
|
"etc\n",
|
||||||
|
"Here we’ll take Markdown as our example and use a modified version of our first sample text:\n",
|
||||||
|
"\n",
|
||||||
|
"The result is the following:\n",
|
||||||
"You can see here that with a chunk size of 105, the Markdown structure of the document is taken into account, and the chunks thus preserve the semantics of the text!\n",
|
"You can see here that with a chunk size of 105, the Markdown structure of the document is taken into account, and the chunks thus preserve the semantics of the text!\n",
|
||||||
"And there you have it! These chunking strategies are like a personal toolbox when it comes to implementing Retrieval Augmented Generation. They're a ton of ways to slice and dice text, each with its unique features and quirks. This variety gives you the freedom to pick the strategy that suits your project best, allowing you to tailor your approach to perfectly fit the unique needs of your work.\n"
|
"\n",
|
||||||
|
"=== 2 ===\n",
|
||||||
|
"And there you have it! These chunking strategies are like a personal toolbox when it comes to implementing Retrieval Augmented Generation. They're a ton of ways to slice and dice text, each with its unique features and quirks. This variety gives you the freedom to pick the strategy that suits your project best, allowing you to tailor your approach to perfectly fit the unique needs of your work.\n",
|
||||||
|
"To put these strategies into action, there's a whole array of tools and libraries at your disposal. For example, llama_index is a fantastic tool that lets you create document indices and retrieve chunked documents. Let's not forget LangChain, another remarkable tool that makes implementing chunking strategies a breeze, particularly when dealing with multi-language data. Diving into these tools and understanding how they can work in harmony with the chunking strategies we've discussed is a crucial part of mastering Retrieval Augmented Generation.\n",
|
||||||
|
"By the way, if you're eager to experiment with your own examples using the chunking visualisation tool featured in this blog, feel free to give it a try! You can access it right here. Enjoy, and happy chunking! 😉\n",
|
||||||
|
"\n",
|
||||||
|
"=== 3 ===\n",
|
||||||
|
"Retrieval Augmented Generation (RAG) has been a hot topic in understanding, interpreting, and generating text with AI for the last few months. It's like a wonderful union of retrieval-based and generative models, creating a playground for researchers, data scientists, and natural language processing enthusiasts, like you and me.\n",
|
||||||
|
"To truly control the results produced by our RAG, we need to understand chunking strategies and their role in the process of retrieving and generating text. Indeed, each chunking strategy enhances RAG's effectiveness in its unique way.\n",
|
||||||
|
"The goal of chunking is, as its name says, to chunk the information into multiple smaller pieces in order to store it in a more efficient and meaningful way. This allows the retrieval to capture pieces of information that are more related to the question at hand, and the generation to be more precise, but also less costly, as only a part of a document will be included in the LLM prompt, instead of the whole document.\n",
|
||||||
|
"Let's explore some chunking strategies together.\n",
|
||||||
|
"The methods mentioned in the article you're about to read usually make use of two key parameters. First, we have [chunk_size]— which controls the size of your text chunks. Then there's [chunk_overlap], which takes care of how much text overlaps between one chunk and the next.\n",
|
||||||
|
"\n",
|
||||||
|
"=== 4 ===\n",
|
||||||
|
"Semantic Chunking considers the relationships within the text. It divides the text into meaningful, semantically complete chunks. This approach ensures the information's integrity during retrieval, leading to a more accurate and contextually appropriate outcome.\n",
|
||||||
|
"Semantic chunking involves taking the embeddings of every sentence in the document, comparing the similarity of all sentences with each other, and then grouping sentences with the most similar embeddings together.\n",
|
||||||
|
"By focusing on the text's meaning and context, Semantic Chunking significantly enhances the quality of retrieval. It's a top-notch choice when maintaining the semantic integrity of the text is vital.\n",
|
||||||
|
"However, this method does require more effort and is notably slower than the previous ones.\n",
|
||||||
|
"On our example text, since it is quite short and does not expose varied subjects, this method would only generate a single chunk.\n",
|
||||||
|
"\n",
|
||||||
|
"=== 5 ===\n",
|
||||||
|
"Language models used in the rest of your possible RAG pipeline have a token limit, which should not be exceeded. When dividing your text into chunks, it's advisable to count the number of tokens. Plenty of tokenizers are available. To ensure accuracy, use the same tokenizer for counting tokens as the one used in the language model.\n",
|
||||||
|
"Consequently, there are also splitters available for this purpose.\n",
|
||||||
|
"For instance, by using the [SpacyTextSplitter] from LangChain, the following chunks are created:\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"=== 6 ===\n",
|
||||||
|
"First things first, we have Character Chunking. This strategy divides the text into chunks based on a fixed number of characters. Its simplicity makes it a great starting point, but it can sometimes disrupt the text's flow, breaking sentences or words in unexpected places. Despite its limitations, it's a great stepping stone towards more advanced methods.\n",
|
||||||
|
"Now let’s see that in action with an example. Imagine a text that reads:\n",
|
||||||
|
"If we decide to set our chunk size to 100 and no chunk overlap, we'd end up with the following chunks. As you can see, Character Chunking can lead to some intriguing, albeit sometimes nonsensical, results, cutting some of the sentences in their middle.\n",
|
||||||
|
"By choosing a smaller chunk size, we would obtain more chunks, and by setting a bigger chunk overlap, we could obtain something like this:\n",
|
||||||
|
"\n",
|
||||||
|
"Also, by default this method creates chunks character by character based on the empty character [’ ’]. But you can specify a different one in order to chunk on something else, even a complete word! For instance, by specifying [' '] as the separator, you can avoid cutting words in their middle.\n",
|
||||||
|
"\n",
|
||||||
|
"=== 7 ===\n",
|
||||||
|
"Next, let's take a look at Recursive Character Chunking. Based on the basic concept of Character Chunking, this advanced version takes it up a notch by dividing the text into chunks until a certain condition is met, such as reaching a minimum chunk size. This method ensures that the chunking process aligns with the text's structure, preserving more meaning. Its adaptability makes Recursive Character Chunking great for texts with varied structures.\n",
|
||||||
|
"Again, let’s use the same example in order to illustrate this method. With a chunk size of 100, and the default settings for the other parameters, we obtain the following chunks:\n",
|
||||||
|
"\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"points = client.query(COLLECTION_NAME, query_text=\"Can I split documents?\", limit=10)\n",
|
"for i, point in enumerate(points):\n",
|
||||||
"\n",
|
" print(f\"=== {i} ===\")\n",
|
||||||
"print(\"<=== Retrieved documents ===>\")\n",
|
" print(point.document)\n",
|
||||||
"for point in points:\n",
|
" print()"
|
||||||
" print(point.document)"
|
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -280,7 +291,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.13.0"
|
"version": "3.12.7"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
75
docs/examples/translate.py
Normal file
75
docs/examples/translate.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import logging
|
||||||
|
import time
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from docling_core.types.doc import ImageRefMode, PictureItem, TableItem, TextItem
|
||||||
|
|
||||||
|
from docling.datamodel.base_models import FigureElement, InputFormat, Table
|
||||||
|
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
||||||
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
||||||
|
|
||||||
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
IMAGE_RESOLUTION_SCALE = 2.0
|
||||||
|
|
||||||
|
|
||||||
|
# FIXME: put in your favorite translation code ....
|
||||||
|
def translate(text: str, src: str = "en", dest: str = "de"):
|
||||||
|
|
||||||
|
_log.warning("!!! IMPLEMENT HERE YOUR FAVORITE TRANSLATION CODE!!!")
|
||||||
|
# from googletrans import Translator
|
||||||
|
|
||||||
|
# Initialize the translator
|
||||||
|
# translator = Translator()
|
||||||
|
|
||||||
|
# Translate text from English to German
|
||||||
|
# text = "Hello, how are you?"
|
||||||
|
# translated = translator.translate(text, src="en", dest="de")
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
input_doc_path = Path("./tests/data/2206.01062.pdf")
|
||||||
|
output_dir = Path("scratch")
|
||||||
|
|
||||||
|
# Important: For operating with page images, we must keep them, otherwise the DocumentConverter
|
||||||
|
# will destroy them for cleaning up memory.
|
||||||
|
# This is done by setting PdfPipelineOptions.images_scale, which also defines the scale of images.
|
||||||
|
# scale=1 correspond of a standard 72 DPI image
|
||||||
|
# The PdfPipelineOptions.generate_* are the selectors for the document elements which will be enriched
|
||||||
|
# with the image field
|
||||||
|
pipeline_options = PdfPipelineOptions()
|
||||||
|
pipeline_options.images_scale = IMAGE_RESOLUTION_SCALE
|
||||||
|
pipeline_options.generate_page_images = True
|
||||||
|
pipeline_options.generate_picture_images = True
|
||||||
|
|
||||||
|
doc_converter = DocumentConverter(
|
||||||
|
format_options={
|
||||||
|
InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
conv_res = doc_converter.convert(input_doc_path)
|
||||||
|
conv_doc = conv_res.document
|
||||||
|
|
||||||
|
# Save markdown with embedded pictures in original text
|
||||||
|
md_filename = output_dir / f"{doc_filename}-with-images-orig.md"
|
||||||
|
conv_doc.save_as_markdown(md_filename, image_mode=ImageRefMode.EMBEDDED)
|
||||||
|
|
||||||
|
for element, _level in conv_res.document.iterate_items():
|
||||||
|
if isinstance(element, TextItem):
|
||||||
|
element.orig = element.text
|
||||||
|
element.text = translate(text=element.text)
|
||||||
|
|
||||||
|
elif isinstance(element, TableItem):
|
||||||
|
for cell in element.data.table_cells:
|
||||||
|
cell.text = translate(text=element.text)
|
||||||
|
|
||||||
|
# Save markdown with embedded pictures in translated text
|
||||||
|
md_filename = output_dir / f"{doc_filename}-with-images-translated.md"
|
||||||
|
conv_doc.save_as_markdown(md_filename, image_mode=ImageRefMode.EMBEDDED)
|
@ -21,7 +21,7 @@ Docling parses documents and exports them to the desired format with ease and sp
|
|||||||
* 🗂️ Reads popular document formats (PDF, DOCX, PPTX, XLSX, Images, HTML, AsciiDoc & Markdown) and exports to HTML, Markdown and JSON (with embedded and referenced images)
|
* 🗂️ Reads popular document formats (PDF, DOCX, PPTX, XLSX, Images, HTML, AsciiDoc & Markdown) and exports to HTML, Markdown and JSON (with embedded and referenced images)
|
||||||
* 📑 Advanced PDF document understanding incl. page layout, reading order & table structures
|
* 📑 Advanced PDF document understanding incl. page layout, reading order & table structures
|
||||||
* 🧩 Unified, expressive [DoclingDocument](./concepts/docling_document.md) representation format
|
* 🧩 Unified, expressive [DoclingDocument](./concepts/docling_document.md) representation format
|
||||||
* 🤖 Easy integration with 🦙 LlamaIndex & 🦜🔗 LangChain for powerful RAG / QA applications
|
* 🤖 Plug-and-play [integrations](https://ds4sd.github.io/docling/integrations/) incl. LangChain, LlamaIndex, Crew AI & Haystack for agentic AI
|
||||||
* 🔍 OCR support for scanned PDFs
|
* 🔍 OCR support for scanned PDFs
|
||||||
* 💻 Simple and convenient CLI
|
* 💻 Simple and convenient CLI
|
||||||
|
|
||||||
@ -29,7 +29,15 @@ Docling parses documents and exports them to the desired format with ease and sp
|
|||||||
|
|
||||||
* ♾️ Equation & code extraction
|
* ♾️ Equation & code extraction
|
||||||
* 📝 Metadata extraction, including title, authors, references & language
|
* 📝 Metadata extraction, including title, authors, references & language
|
||||||
* 🦜🔗 Native LangChain extension
|
|
||||||
|
## Get started
|
||||||
|
|
||||||
|
<div class="grid">
|
||||||
|
<a href="concepts/" class="card"><b>Concepts</b><br />Learn Docling fundamendals</a>
|
||||||
|
<a href="examples/" class="card"><b>Examples</b><br />Try out recipes for various use cases, including conversion, RAG, and more</a>
|
||||||
|
<a href="integrations/" class="card"><b>Integrations</b><br />Check out integrations with popular frameworks and tools</a>
|
||||||
|
<a href="reference/document_converter/" class="card"><b>Reference</b><br />See more API details</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
## IBM ❤️ Open Source AI
|
## IBM ❤️ Open Source AI
|
||||||
|
|
||||||
|
10
docs/integrations/crewai.md
Normal file
10
docs/integrations/crewai.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
Docling is available in [CrewAI](https://www.crewai.com/) as the `CrewDoclingSource`
|
||||||
|
knowledge source.
|
||||||
|
|
||||||
|
- 💻 [Crew AI GitHub][github]
|
||||||
|
- 📖 [Crew AI knowledge docs][docs]
|
||||||
|
- 📦 [Crew AI PyPI][package]
|
||||||
|
|
||||||
|
[github]: https://github.com/crewAIInc/crewAI/
|
||||||
|
[docs]: https://docs.crewai.com/concepts/knowledge
|
||||||
|
[package]: https://pypi.org/project/crewai/
|
@ -1,6 +1,6 @@
|
|||||||
Docling is available as a converter in [Haystack](https://haystack.deepset.ai/):
|
Docling is available as a converter in [Haystack](https://haystack.deepset.ai/):
|
||||||
|
|
||||||
- 📖 [Docling Haystack integration docs](https://haystack.deepset.ai/integrations/docling)
|
- 📖 [Docling Haystack integration docs][docs]
|
||||||
- 💻 [Docling Haystack integration GitHub][github]
|
- 💻 [Docling Haystack integration GitHub][github]
|
||||||
- 🧑🏽🍳 [Docling Haystack integration example][example]
|
- 🧑🏽🍳 [Docling Haystack integration example][example]
|
||||||
- 📦 [Docling Haystack integration PyPI][pypi]
|
- 📦 [Docling Haystack integration PyPI][pypi]
|
||||||
@ -8,4 +8,4 @@ Docling is available as a converter in [Haystack](https://haystack.deepset.ai/):
|
|||||||
[github]: https://github.com/DS4SD/docling-haystack
|
[github]: https://github.com/DS4SD/docling-haystack
|
||||||
[docs]: https://haystack.deepset.ai/integrations/docling
|
[docs]: https://haystack.deepset.ai/integrations/docling
|
||||||
[pypi]: https://pypi.org/project/docling-haystack
|
[pypi]: https://pypi.org/project/docling-haystack
|
||||||
[example]: https://ds4sd.github.io/docling/examples/rag_haystack/
|
[example]: ../examples/rag_haystack.ipynb
|
||||||
|
14
docs/integrations/langchain.md
Normal file
14
docs/integrations/langchain.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
Docling is available as an official [LangChain](https://python.langchain.com/) extension.
|
||||||
|
|
||||||
|
To get started, check out the [step-by-step guide in LangChain][guide].
|
||||||
|
|
||||||
|
- 📖 [LangChain Docling integration docs][docs]
|
||||||
|
- 💻 [LangChain Docling integration GitHub][github]
|
||||||
|
- 🧑🏽🍳 [LangChain Docling integration example][example]
|
||||||
|
- 📦 [LangChain Docling integration PyPI][pypi]
|
||||||
|
|
||||||
|
[docs]: https://python.langchain.com/docs/integrations/providers/docling/
|
||||||
|
[github]: https://github.com/DS4SD/docling-langchain
|
||||||
|
[guide]: https://python.langchain.com/docs/integrations/document_loaders/docling/
|
||||||
|
[example]: ../examples/rag_langchain.ipynb
|
||||||
|
[pypi]: https://pypi.org/project/langchain-docling/
|
6
docs/integrations/nvidia.md
Normal file
6
docs/integrations/nvidia.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
Docling is powering the NVIDIA *PDF to Podcast* agentic AI blueprint:
|
||||||
|
|
||||||
|
- [🏠 PDF to Podcast home](https://build.nvidia.com/nvidia/pdf-to-podcast)
|
||||||
|
- [💻 PDF to Podcast GitHub](https://github.com/NVIDIA-AI-Blueprints/pdf-to-podcast)
|
||||||
|
- [📣 PDF to Podcast announcement](https://nvidianews.nvidia.com/news/nvidia-launches-ai-foundation-models-for-rtx-ai-pcs)
|
||||||
|
- [✍️ PDF to Podcast blog post](https://blogs.nvidia.com/blog/agentic-ai-blueprints/)
|
5
docs/integrations/opencontracts.md
Normal file
5
docs/integrations/opencontracts.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Docling is available an ingestion engine for [OpenContracts](https://github.com/JSv4/OpenContracts), allowing you to use Docling's OCR engine(s), chunker(s), labels, etc. and load them into a platform supporting bulk data extraction, text annotating, and question-answering:
|
||||||
|
|
||||||
|
- 💻 [OpenContracts GitHub](https://github.com/JSv4/OpenContracts)
|
||||||
|
- 📖 [OpenContracts Docs](https://jsv4.github.io/OpenContracts/)
|
||||||
|
- ▶️ [OpenContracts x Docling PDF annotation screen capture](https://github.com/JSv4/OpenContracts/blob/main/docs/assets/images/gifs/PDF%20Annotation%20Flow.gif)
|
@ -32,6 +32,7 @@ This is an automatic generated API reference of the DoclingDocument type.
|
|||||||
- CoordOrigin
|
- CoordOrigin
|
||||||
- ImageRefMode
|
- ImageRefMode
|
||||||
- Size
|
- Size
|
||||||
|
docstring_style: sphinx
|
||||||
show_if_no_docstring: true
|
show_if_no_docstring: true
|
||||||
show_submodules: true
|
show_submodules: true
|
||||||
docstring_section_style: list
|
docstring_section_style: list
|
||||||
|
@ -95,8 +95,8 @@ doc_converter = (
|
|||||||
|
|
||||||
More options are shown in the following example units:
|
More options are shown in the following example units:
|
||||||
|
|
||||||
- [run_with_formats.py](../examples/run_with_formats/)
|
- [run_with_formats.py](examples/run_with_formats.py)
|
||||||
- [custom_convert.py](../examples/custom_convert/)
|
- [custom_convert.py](examples/custom_convert.py)
|
||||||
|
|
||||||
### Converting documents
|
### Converting documents
|
||||||
|
|
||||||
@ -226,4 +226,4 @@ leverages the new `DoclingDocument` and provides a new, richer chunk output form
|
|||||||
- any applicable headings for context
|
- any applicable headings for context
|
||||||
- any applicable captions for context
|
- any applicable captions for context
|
||||||
|
|
||||||
For an example, check out [Chunking usage](../usage/#chunking).
|
For an example, check out [Chunking usage](usage.md#chunking).
|
||||||
|
56
mkdocs.yml
56
mkdocs.yml
@ -65,7 +65,7 @@ nav:
|
|||||||
- Chunking: concepts/chunking.md
|
- Chunking: concepts/chunking.md
|
||||||
- Examples:
|
- Examples:
|
||||||
- Examples: examples/index.md
|
- Examples: examples/index.md
|
||||||
- Conversion:
|
- 🔀 Conversion:
|
||||||
- "Simple conversion": examples/minimal.py
|
- "Simple conversion": examples/minimal.py
|
||||||
- "Custom conversion": examples/custom_convert.py
|
- "Custom conversion": examples/custom_convert.py
|
||||||
- "Batch conversion": examples/batch_convert.py
|
- "Batch conversion": examples/batch_convert.py
|
||||||
@ -76,30 +76,40 @@ nav:
|
|||||||
- "Multimodal export": examples/export_multimodal.py
|
- "Multimodal export": examples/export_multimodal.py
|
||||||
- "Force full page OCR": examples/full_page_ocr.py
|
- "Force full page OCR": examples/full_page_ocr.py
|
||||||
- "Accelerator options": examples/run_with_accelerator.py
|
- "Accelerator options": examples/run_with_accelerator.py
|
||||||
- Chunking:
|
- "Simple translation": examples/translate.py
|
||||||
- "Hybrid chunking": examples/hybrid_chunking.ipynb
|
- ✂️ Chunking:
|
||||||
- RAG / QA:
|
- examples/hybrid_chunking.ipynb
|
||||||
- "RAG with Haystack": examples/rag_haystack.ipynb
|
- 🤖 RAG with AI dev frameworks:
|
||||||
- "RAG with LlamaIndex 🦙": examples/rag_llamaindex.ipynb
|
- examples/rag_haystack.ipynb
|
||||||
- "RAG with LangChain 🦜🔗": examples/rag_langchain.ipynb
|
- examples/rag_langchain.ipynb
|
||||||
- "RAG with Weaviate": examples/rag_weaviate.ipynb
|
- examples/rag_llamaindex.ipynb
|
||||||
- "Hybrid RAG with Qdrant": examples/hybrid_rag_qdrant.ipynb
|
- 🗂️ More examples:
|
||||||
|
- examples/rag_weaviate.ipynb
|
||||||
|
- RAG with Granite [↗]: https://github.com/ibm-granite-community/granite-snack-cookbook/blob/main/recipes/RAG/Granite_Docling_RAG.ipynb
|
||||||
|
- examples/rag_azuresearch.ipynb
|
||||||
|
- examples/retrieval_qdrant.ipynb
|
||||||
- Integrations:
|
- Integrations:
|
||||||
- Integrations: integrations/index.md
|
- Integrations: integrations/index.md
|
||||||
- "🐝 Bee": integrations/bee.md
|
- 🤖 Agentic / AI dev frameworks:
|
||||||
- "Cloudera": integrations/cloudera.md
|
- "Bee Agent Framework": integrations/bee.md
|
||||||
- "Data Prep Kit": integrations/data_prep_kit.md
|
- "Crew AI": integrations/crewai.md
|
||||||
- "DocETL": integrations/docetl.md
|
- "Haystack": integrations/haystack.md
|
||||||
- "Haystack": integrations/haystack.md
|
- "LangChain": integrations/langchain.md
|
||||||
- "🐶 InstructLab": integrations/instructlab.md
|
- "LlamaIndex": integrations/llamaindex.md
|
||||||
- "Kotaemon": integrations/kotaemon.md
|
- "txtai": integrations/txtai.md
|
||||||
- "🦙 LlamaIndex": integrations/llamaindex.md
|
- ⭐️ Featured:
|
||||||
- "Prodigy": integrations/prodigy.md
|
- "Data Prep Kit": integrations/data_prep_kit.md
|
||||||
- "RHEL AI": integrations/rhel_ai.md
|
- "InstructLab": integrations/instructlab.md
|
||||||
- "spaCy": integrations/spacy.md
|
- "NVIDIA": integrations/nvidia.md
|
||||||
- "txtai": integrations/txtai.md
|
- "Prodigy": integrations/prodigy.md
|
||||||
- "Vectara": integrations/vectara.md
|
- "RHEL AI": integrations/rhel_ai.md
|
||||||
# - "LangChain 🦜🔗": integrations/langchain.md
|
- "spaCy": integrations/spacy.md
|
||||||
|
- 🗂️ More integrations:
|
||||||
|
- "Cloudera": integrations/cloudera.md
|
||||||
|
- "DocETL": integrations/docetl.md
|
||||||
|
- "Kotaemon": integrations/kotaemon.md
|
||||||
|
- "OpenContracts": integrations/opencontracts.md
|
||||||
|
- "Vectara": integrations/vectara.md
|
||||||
- Reference:
|
- Reference:
|
||||||
- Python API:
|
- Python API:
|
||||||
- Document Converter: reference/document_converter.md
|
- Document Converter: reference/document_converter.md
|
||||||
|
2404
poetry.lock
generated
2404
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "docling"
|
name = "docling"
|
||||||
version = "2.14.0" # DO NOT EDIT, updated automatically
|
version = "2.15.1" # DO NOT EDIT, updated automatically
|
||||||
description = "SDK and CLI for parsing PDF, DOCX, HTML, and more, to a unified document representation for powering downstream workflows such as gen AI applications."
|
description = "SDK and CLI for parsing PDF, DOCX, HTML, and more, to a unified document representation for powering downstream workflows such as gen AI applications."
|
||||||
authors = ["Christoph Auer <cau@zurich.ibm.com>", "Michele Dolfi <dol@zurich.ibm.com>", "Maxim Lysak <mly@zurich.ibm.com>", "Nikos Livathinos <nli@zurich.ibm.com>", "Ahmed Nassar <ahn@zurich.ibm.com>", "Panos Vagenas <pva@zurich.ibm.com>", "Peter Staar <taa@zurich.ibm.com>"]
|
authors = ["Christoph Auer <cau@zurich.ibm.com>", "Michele Dolfi <dol@zurich.ibm.com>", "Maxim Lysak <mly@zurich.ibm.com>", "Nikos Livathinos <nli@zurich.ibm.com>", "Ahmed Nassar <ahn@zurich.ibm.com>", "Panos Vagenas <pva@zurich.ibm.com>", "Peter Staar <taa@zurich.ibm.com>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@ -25,16 +25,16 @@ packages = [{include = "docling"}]
|
|||||||
# actual dependencies:
|
# actual dependencies:
|
||||||
######################
|
######################
|
||||||
python = "^3.9"
|
python = "^3.9"
|
||||||
docling-core = { version = "^2.12.1", extras = ["chunking"] }
|
|
||||||
pydantic = "^2.0.0"
|
pydantic = "^2.0.0"
|
||||||
docling-ibm-models = "^3.1.0"
|
docling-core = { version = "^2.15.1", extras = ["chunking"] }
|
||||||
|
docling-ibm-models = "^3.2.1"
|
||||||
deepsearch-glm = "^1.0.0"
|
deepsearch-glm = "^1.0.0"
|
||||||
docling-parse = "^3.0.0"
|
docling-parse = "^3.1.0"
|
||||||
filetype = "^1.2.0"
|
filetype = "^1.2.0"
|
||||||
pypdfium2 = "^4.30.0"
|
pypdfium2 = "^4.30.0"
|
||||||
pydantic-settings = "^2.3.0"
|
pydantic-settings = "^2.3.0"
|
||||||
huggingface_hub = ">=0.23,<1"
|
huggingface_hub = ">=0.23,<1"
|
||||||
requests = "^2.32.3"
|
requests = "^2.32.2"
|
||||||
easyocr = "^1.7"
|
easyocr = "^1.7"
|
||||||
tesserocr = { version = "^2.7.1", optional = true }
|
tesserocr = { version = "^2.7.1", optional = true }
|
||||||
certifi = ">=2024.7.4"
|
certifi = ">=2024.7.4"
|
||||||
|
BIN
tests/data/code_and_formula.pdf
Normal file
BIN
tests/data/code_and_formula.pdf
Normal file
Binary file not shown.
@ -0,0 +1,13 @@
|
|||||||
|
<document>
|
||||||
|
<subtitle-level-1><location><page_1><loc_22><loc_83><loc_45><loc_84></location>Java Code Example</subtitle-level-1>
|
||||||
|
<paragraph><location><page_1><loc_22><loc_63><loc_78><loc_81></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</paragraph>
|
||||||
|
<paragraph><location><page_1><loc_39><loc_61><loc_61><loc_62></location>Listing 1: Simple Java Program</paragraph>
|
||||||
|
<paragraph><location><page_1><loc_22><loc_56><loc_55><loc_60></location>public static void print() { System.out.println( "Java Code" ); }</paragraph>
|
||||||
|
<paragraph><location><page_1><loc_22><loc_37><loc_78><loc_55></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</paragraph>
|
||||||
|
<subtitle-level-1><location><page_2><loc_22><loc_84><loc_32><loc_85></location>Formula</subtitle-level-1>
|
||||||
|
<paragraph><location><page_2><loc_22><loc_65><loc_80><loc_82></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</paragraph>
|
||||||
|
<paragraph><location><page_2><loc_22><loc_58><loc_80><loc_65></location>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt.</paragraph>
|
||||||
|
<paragraph><location><page_2><loc_22><loc_38><loc_80><loc_55></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</paragraph>
|
||||||
|
<paragraph><location><page_2><loc_22><loc_29><loc_80><loc_38></location>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</paragraph>
|
||||||
|
<paragraph><location><page_2><loc_22><loc_21><loc_80><loc_29></location>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</paragraph>
|
||||||
|
</document>
|
1
tests/data/groundtruth/docling_v1/code_and_formula.json
Normal file
1
tests/data/groundtruth/docling_v1/code_and_formula.json
Normal file
File diff suppressed because one or more lines are too long
19
tests/data/groundtruth/docling_v1/code_and_formula.md
Normal file
19
tests/data/groundtruth/docling_v1/code_and_formula.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
## Java Code Example
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Listing 1: Simple Java Program
|
||||||
|
|
||||||
|
public static void print() { System.out.println( "Java Code" ); }
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
## Formula
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt.
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,14 @@
|
|||||||
|
<document>
|
||||||
|
<section_header_level_1><location><page_1><loc_22><loc_83><loc_45><loc_84></location>Java Code Example</section_header_level_1>
|
||||||
|
<text><location><page_1><loc_22><loc_63><loc_78><loc_81></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</text>
|
||||||
|
<paragraph><location><page_1><loc_39><loc_61><loc_61><loc_62></location>Listing 1: Simple Java Program</paragraph>
|
||||||
|
<code><location><page_1><loc_22><loc_56><loc_55><loc_60></location>public static void print() { System.out.println( "Java Code" ); }</code>
|
||||||
|
<text><location><page_1><loc_22><loc_37><loc_78><loc_55></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</text>
|
||||||
|
<section_header_level_1><location><page_2><loc_22><loc_84><loc_32><loc_85></location>Formula</section_header_level_1>
|
||||||
|
<text><location><page_2><loc_22><loc_65><loc_80><loc_82></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</text>
|
||||||
|
<text><location><page_2><loc_22><loc_58><loc_80><loc_65></location>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt.</text>
|
||||||
|
<formula><location><page_2><loc_47><loc_56><loc_56><loc_57></location>a 2 + 8 = 12</formula>
|
||||||
|
<text><location><page_2><loc_22><loc_38><loc_80><loc_55></location>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</text>
|
||||||
|
<text><location><page_2><loc_22><loc_29><loc_80><loc_38></location>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</text>
|
||||||
|
<text><location><page_2><loc_22><loc_21><loc_80><loc_29></location>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</text>
|
||||||
|
</document>
|
1
tests/data/groundtruth/docling_v2/code_and_formula.json
Normal file
1
tests/data/groundtruth/docling_v2/code_and_formula.json
Normal file
File diff suppressed because one or more lines are too long
25
tests/data/groundtruth/docling_v2/code_and_formula.md
Normal file
25
tests/data/groundtruth/docling_v2/code_and_formula.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
## Java Code Example
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Listing 1: Simple Java Program
|
||||||
|
|
||||||
|
```
|
||||||
|
public static void print() { System.out.println( "Java Code" ); }
|
||||||
|
```
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
## Formula
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt.
|
||||||
|
|
||||||
|
a 2 + 8 = 12
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
|
||||||
|
|
||||||
|
Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
|
File diff suppressed because one or more lines are too long
66
tests/test_code_formula.py
Normal file
66
tests/test_code_formula.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from docling_core.types.doc import CodeItem, TextItem
|
||||||
|
from docling_core.types.doc.labels import CodeLanguageLabel, DocItemLabel
|
||||||
|
|
||||||
|
from docling.backend.docling_parse_backend import DoclingParseDocumentBackend
|
||||||
|
from docling.backend.docling_parse_v2_backend import DoclingParseV2DocumentBackend
|
||||||
|
from docling.datamodel.base_models import InputFormat
|
||||||
|
from docling.datamodel.document import ConversionResult
|
||||||
|
from docling.datamodel.pipeline_options import PdfPipelineOptions
|
||||||
|
from docling.document_converter import DocumentConverter, PdfFormatOption
|
||||||
|
from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline
|
||||||
|
|
||||||
|
|
||||||
|
def get_converter():
|
||||||
|
|
||||||
|
pipeline_options = PdfPipelineOptions()
|
||||||
|
pipeline_options.generate_page_images = True
|
||||||
|
|
||||||
|
pipeline_options.do_ocr = False
|
||||||
|
pipeline_options.do_table_structure = False
|
||||||
|
pipeline_options.do_code_enrichment = True
|
||||||
|
pipeline_options.do_formula_enrichment = True
|
||||||
|
|
||||||
|
converter = DocumentConverter(
|
||||||
|
format_options={
|
||||||
|
InputFormat.PDF: PdfFormatOption(
|
||||||
|
backend=DoclingParseV2DocumentBackend,
|
||||||
|
pipeline_cls=StandardPdfPipeline,
|
||||||
|
pipeline_options=pipeline_options,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return converter
|
||||||
|
|
||||||
|
|
||||||
|
def test_code_and_formula_conversion():
|
||||||
|
pdf_path = Path("tests/data/code_and_formula.pdf")
|
||||||
|
converter = get_converter()
|
||||||
|
|
||||||
|
print(f"converting {pdf_path}")
|
||||||
|
|
||||||
|
doc_result: ConversionResult = converter.convert(pdf_path)
|
||||||
|
|
||||||
|
results = doc_result.document.texts
|
||||||
|
|
||||||
|
code_blocks = [el for el in results if isinstance(el, CodeItem)]
|
||||||
|
assert len(code_blocks) == 1
|
||||||
|
|
||||||
|
gt = 'public static void print() {\n System.out.println("Java Code");\n}'
|
||||||
|
|
||||||
|
predicted = code_blocks[0].text.strip()
|
||||||
|
assert predicted == gt, f"mismatch in text {predicted=}, {gt=}"
|
||||||
|
assert code_blocks[0].code_language == CodeLanguageLabel.JAVA
|
||||||
|
|
||||||
|
formula_blocks = [
|
||||||
|
el
|
||||||
|
for el in results
|
||||||
|
if isinstance(el, TextItem) and el.label == DocItemLabel.FORMULA
|
||||||
|
]
|
||||||
|
assert len(formula_blocks) == 1
|
||||||
|
|
||||||
|
gt = "a ^ { 2 } + 8 = 1 2"
|
||||||
|
predicted = formula_blocks[0].text
|
||||||
|
assert predicted == gt, f"mismatch in text {predicted=}, {gt=}"
|
@ -60,6 +60,7 @@ def test_e2e_conversions():
|
|||||||
RapidOcrOptions(),
|
RapidOcrOptions(),
|
||||||
EasyOcrOptions(force_full_page_ocr=True),
|
EasyOcrOptions(force_full_page_ocr=True),
|
||||||
TesseractOcrOptions(force_full_page_ocr=True),
|
TesseractOcrOptions(force_full_page_ocr=True),
|
||||||
|
TesseractOcrOptions(force_full_page_ocr=True, lang=["auto"]),
|
||||||
TesseractCliOcrOptions(force_full_page_ocr=True),
|
TesseractCliOcrOptions(force_full_page_ocr=True),
|
||||||
RapidOcrOptions(force_full_page_ocr=True),
|
RapidOcrOptions(force_full_page_ocr=True),
|
||||||
]
|
]
|
||||||
@ -70,7 +71,9 @@ def test_e2e_conversions():
|
|||||||
engines.append(OcrMacOptions(force_full_page_ocr=True))
|
engines.append(OcrMacOptions(force_full_page_ocr=True))
|
||||||
|
|
||||||
for ocr_options in engines:
|
for ocr_options in engines:
|
||||||
print(f"Converting with ocr_engine: {ocr_options.kind}")
|
print(
|
||||||
|
f"Converting with ocr_engine: {ocr_options.kind}, language: {ocr_options.lang}"
|
||||||
|
)
|
||||||
converter = get_converter(ocr_options=ocr_options)
|
converter = get_converter(ocr_options=ocr_options)
|
||||||
for pdf_path in pdf_paths:
|
for pdf_path in pdf_paths:
|
||||||
print(f"converting {pdf_path}")
|
print(f"converting {pdf_path}")
|
||||||
|
Loading…
Reference in New Issue
Block a user