mirror of
https://github.com/docling-project/docling.git
synced 2026-05-17 13:10:38 +00:00
* docs(opensearch): update the example notebook RAG with OpenSearch Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com> * docs(uspto): remove direct usage of the backend class for conversion Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com> * docs: remove direct usage of backends from documentation Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com> --------- Signed-off-by: Cesar Berrospi Ramis <ceb@zurich.ibm.com>
40 KiB
Vendored
40 KiB
Vendored
In [1]:
from docling.document_converter import DocumentConverter
# a sample PMC article:
source = "../../tests/data/jats/elife-56337.nxml"
converter = DocumentConverter()
result = converter.convert(source)
print(result.status)ConversionStatus.SUCCESS
In [2]:
md_doc = result.document.export_to_markdown()
delim = "\n"
print(delim.join(md_doc.split(delim)[:8]))# KRAB-zinc finger protein gene expansion in response to active retrotransposons in the murine lineage Gernot Wolf, Alberto de Iaco, Ming-An Sun, Melania Bruno, Matthew Tinkham, Don Hoang, Apratim Mitra, Sherry Ralls, Didier Trono, Todd S Macfarlan The Eunice Kennedy Shriver National Institute of Child Health and Human Development, The National Institutes of Health, Bethesda, United States; School of Life Sciences, École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland ## Abstract
In [3]:
from io import BytesIO
from docling.datamodel.base_models import DocumentStream
from docling.exceptions import ConversionError
xml_content = (
b'<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE docling_test SYSTEM '
b'"test.dtd"><docling>Random content</docling>'
)
stream = DocumentStream(name="docling_test.xml", stream=BytesIO(xml_content))
try:
result = converter.convert(stream)
except ConversionError as ce:
print(ce)Input document docling_test.xml does not match any allowed format.
File format not allowed: docling_test.xml
In [4]:
%pip install -q --progress-bar off --no-warn-conflicts llama-index-core llama-index-readers-docling llama-index-node-parser-docling llama-index-embeddings-huggingface llama-index-llms-huggingface-api llama-index-vector-stores-milvus llama-index-readers-file python-dotenvNote: you may need to restart the kernel to use updated packages.
In [5]:
import os
from warnings import filterwarnings
from dotenv import load_dotenv
def _get_env_from_colab_or_os(key):
try:
from google.colab import userdata
try:
return userdata.get(key)
except userdata.SecretNotFoundError:
pass
except ImportError:
pass
return os.getenv(key)
load_dotenv()
filterwarnings(action="ignore", category=UserWarning, module="pydantic")In [6]:
from pathlib import Path
from tempfile import mkdtemp
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
EMBED_MODEL_ID = "BAAI/bge-small-en-v1.5"
EMBED_MODEL = HuggingFaceEmbedding(model_name=EMBED_MODEL_ID)
TEMP_DIR = Path(mkdtemp())
MILVUS_URI = str(TEMP_DIR / "docling.db")
GEN_MODEL = HuggingFaceInferenceAPI(
token=_get_env_from_colab_or_os("HF_TOKEN"),
model_name="mistralai/Mixtral-8x7B-Instruct-v0.1",
)
embed_dim = len(EMBED_MODEL.get_text_embedding("hi"))
# https://github.com/huggingface/transformers/issues/5486:
os.environ["TOKENIZERS_PARALLELISM"] = "false"In [7]:
import tarfile
from io import BytesIO
import requests
# PMC article PMC11703268
url: str = "https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/e3/6b/PMC11703268.tar.gz"
print(f"Downloading {url}...")
buf = BytesIO(requests.get(url).content)
print("Extracting and storing the XML file containing the article text...")
with tarfile.open(fileobj=buf, mode="r:gz") as tar_file:
for tarinfo in tar_file:
if tarinfo.isreg():
file_path = Path(tarinfo.name)
if file_path.suffix == ".nxml":
with open(TEMP_DIR / file_path.name, "wb") as file_obj:
file_obj.write(tar_file.extractfile(tarinfo).read())
print(f"Stored XML file {file_path.name}")Downloading https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_package/e3/6b/PMC11703268.tar.gz... Extracting and storing the XML file containing the article text... Stored XML file nihpp-2024.12.26.630351v1.nxml
In [8]:
import zipfile
# Patent grants from December 17-23, 2024
url: str = (
"https://bulkdata.uspto.gov/data/patent/grant/redbook/fulltext/2024/ipg241217.zip"
)
XML_SPLITTER: str = '<?xml version="1.0"'
doc_num: int = 0
print(f"Downloading {url}...")
buf = BytesIO(requests.get(url).content)
print("Parsing zip file, splitting into XML sections, and exporting to files...")
with zipfile.ZipFile(buf) as zf:
res = zf.testzip()
if res:
print("Error validating zip file")
else:
with zf.open(zf.namelist()[0]) as xf:
is_patent = False
patent_buffer = BytesIO()
for xf_line in xf:
decoded_line = xf_line.decode(errors="ignore").rstrip()
xml_index = decoded_line.find(XML_SPLITTER)
if xml_index != -1:
if (
xml_index > 0
): # cases like </sequence-cwu><?xml version="1.0"...
patent_buffer.write(xf_line[:xml_index])
patent_buffer.write(b"\r\n")
xf_line = xf_line[xml_index:]
if patent_buffer.getbuffer().nbytes > 0 and is_patent:
doc_num += 1
patent_id = f"ipg241217-{doc_num}"
with open(TEMP_DIR / f"{patent_id}.xml", "wb") as file_obj:
file_obj.write(patent_buffer.getbuffer())
is_patent = False
patent_buffer = BytesIO()
elif decoded_line.startswith("<!DOCTYPE"):
is_patent = True
patent_buffer.write(xf_line)Downloading https://bulkdata.uspto.gov/data/patent/grant/redbook/fulltext/2024/ipg241217.zip... Parsing zip file, splitting into XML sections, and exporting to files...
In [9]:
print(f"Fetched and exported {doc_num} documents.")Fetched and exported 4014 documents.
In [13]:
from llama_index.core import SimpleDirectoryReader
from llama_index.readers.docling import DoclingReader
reader = DoclingReader(export_type=DoclingReader.ExportType.JSON)
dir_reader = SimpleDirectoryReader(
input_dir=TEMP_DIR,
exclude=["docling.db", "*.nxml"],
file_extractor={".xml": reader},
filename_as_id=True,
num_files_limit=100,
)In [14]:
from llama_index.node_parser.docling import DoclingNodeParser
node_parser = DoclingNodeParser()In [ ]:
from llama_index.core import StorageContext, VectorStoreIndex
from llama_index.vector_stores.milvus import MilvusVectorStore
vector_store = MilvusVectorStore(
uri=MILVUS_URI,
dim=embed_dim,
overwrite=True,
)
index = VectorStoreIndex.from_documents(
documents=dir_reader.load_data(show_progress=True),
transformations=[node_parser],
storage_context=StorageContext.from_defaults(vector_store=vector_store),
embed_model=EMBED_MODEL,
show_progress=True,
)In [14]:
index.from_documents(
documents=reader.load_data(TEMP_DIR / "nihpp-2024.12.26.630351v1.nxml"),
transformations=[node_parser],
storage_context=StorageContext.from_defaults(vector_store=vector_store),
embed_model=EMBED_MODEL,
)Out [14]:
<llama_index.core.indices.vector_store.base.VectorStoreIndex at 0x373a7f7d0>
In [15]:
retriever = index.as_retriever(similarity_top_k=3)
results = retriever.retrieve("What patents are related to fitness devices?")
for item in results:
print(item)Node ID: 5afd36c0-a739-4a88-a51c-6d0f75358db5 Text: The portable fitness monitoring device 102 may be a device such as, for example, a mobile phone, a personal digital assistant, a music file player (e.g. and MP3 player), an intelligent article for wearing (e.g. a fitness monitoring garment, wrist band, or watch), a dongle (e.g. a small hardware device that protects software) that includes a fitn... Score: 0.772 Node ID: f294b5fd-9089-43cb-8c4e-d1095a634ff1 Text: US Patent Application US 20120071306 entitled “Portable Multipurpose Whole Body Exercise Device” discloses a portable multipurpose whole body exercise device which can be used for general fitness, Pilates-type, core strengthening, therapeutic, and rehabilitative exercises as well as stretching and physical therapy and which includes storable acc... Score: 0.749 Node ID: 8251c7ef-1165-42e1-8c91-c99c8a711bf7 Text: Program products, methods, and systems for providing fitness monitoring services of the present invention can include any software application executed by one or more computing devices. A computing device can be any type of computing device having one or more processors. For example, a computing device can be a workstation, mobile device (e.g., ... Score: 0.744
In [16]:
from llama_index.core.base.llms.types import ChatMessage, MessageRole
from rich.console import Console
from rich.panel import Panel
console = Console()
query = "Do mosquitoes in high altitude expand viruses over large distances?"
usr_msg = ChatMessage(role=MessageRole.USER, content=query)
response = GEN_MODEL.chat(messages=[usr_msg])
console.print(Panel(query, title="Prompt", border_style="bold red"))
console.print(
Panel(
response.message.content.strip(),
title="Generated Content",
border_style="bold green",
)
)╭──────────────────────────────────────────────────── Prompt ─────────────────────────────────────────────────────╮ │ Do mosquitoes in high altitude expand viruses over large distances? │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────── Generated Content ───────────────────────────────────────────────╮ │ Mosquitoes can be found at high altitudes, but their ability to transmit viruses over long distances is not │ │ primarily dependent on altitude. Mosquitoes are vectors for various diseases, such as malaria, dengue fever, │ │ and Zika virus, and their transmission range is more closely related to their movement, the presence of a host, │ │ and environmental conditions that support their survival and reproduction. │ │ │ │ At high altitudes, the environment can be less suitable for mosquitoes due to factors such as colder │ │ temperatures, lower humidity, and stronger winds, which can limit their population size and distribution. │ │ However, some species of mosquitoes have adapted to high-altitude environments and can still transmit diseases │ │ in these areas. │ │ │ │ It is possible for mosquitoes to be transported by wind or human activities to higher altitudes, but this is │ │ not a significant factor in their ability to transmit viruses over long distances. Instead, long-distance │ │ transmission of viruses is more often associated with human travel and transportation, which can rapidly spread │ │ infected mosquitoes or humans to new areas, leading to the spread of disease. │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
In [17]:
from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters
filters = MetadataFilters(
filters=[
ExactMatchFilter(key="filename", value="nihpp-2024.12.26.630351v1.nxml"),
]
)
query_engine = index.as_query_engine(llm=GEN_MODEL, filter=filters, similarity_top_k=3)
result = query_engine.query(query)
console.print(
Panel(
result.response.strip(),
title="Generated Content with RAG",
border_style="bold green",
)
)╭────────────────────────────────────────── Generated Content with RAG ───────────────────────────────────────────╮ │ Yes, mosquitoes in high altitude can expand viruses over large distances. A study intercepted 1,017 female │ │ mosquitoes at altitudes of 120-290 m above ground over Mali and Ghana and screened them for infection with │ │ arboviruses, plasmodia, and filariae. The study found that 3.5% of the mosquitoes were infected with │ │ flaviviruses, and 1.1% were infectious. Additionally, the study identified 19 mosquito-borne pathogens, │ │ including three arboviruses that affect humans (dengue, West Nile, and M’Poko viruses). The study provides │ │ compelling evidence that mosquito-borne pathogens are often spread by windborne mosquitoes at altitude. │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯