Minor fixes
This commit is contained in:
parent
9c5bef163e
commit
3fe953eccb
4 changed files with 64 additions and 9 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
try:
|
try: # pragma: no cover
|
||||||
from compression.zstd import ( # ty: ignore[unresolved-import]
|
from compression.zstd import ( # ty: ignore[unresolved-import]
|
||||||
compress as _zstd_compress, # type: ignore[import-not-found]
|
compress as _zstd_compress, # type: ignore[import-not-found]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -15,13 +15,11 @@ _docling_document_cache: LRUCache[str, "DoclingDocument"] = LRUCache(maxsize=100
|
||||||
|
|
||||||
|
|
||||||
def _validate_without_pages(compressed_data: bytes) -> "DoclingDocument":
|
def _validate_without_pages(compressed_data: bytes) -> "DoclingDocument":
|
||||||
"""Decompress and validate DoclingDocument, stripping page images."""
|
"""Decompress and validate DoclingDocument."""
|
||||||
from docling_core.types.doc.document import DoclingDocument
|
from docling_core.types.doc.document import DoclingDocument
|
||||||
|
|
||||||
json_str = decompress_json(compressed_data)
|
json_str = decompress_json(compressed_data)
|
||||||
data = json.loads(json_str)
|
return DoclingDocument.model_validate_json(json_str)
|
||||||
data.pop("pages", None)
|
|
||||||
return DoclingDocument.model_validate(data)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_cached_docling_document(
|
def _get_cached_docling_document(
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ def _apply_split_pages_zstd(store: Store) -> None: # pragma: no cover
|
||||||
.to_arrow()
|
.to_arrow()
|
||||||
.to_pylist()
|
.to_pylist()
|
||||||
]
|
]
|
||||||
except Exception:
|
except (pa.ArrowInvalid, pa.ArrowNotImplementedError, OSError):
|
||||||
ids = []
|
ids = []
|
||||||
|
|
||||||
if not ids:
|
if not ids:
|
||||||
|
|
@ -132,7 +132,7 @@ def _apply_split_pages_zstd(store: Store) -> None: # pragma: no cover
|
||||||
range(0, len(staging_ids), BATCH_SIZE), 1
|
range(0, len(staging_ids), BATCH_SIZE), 1
|
||||||
):
|
):
|
||||||
batch_ids = staging_ids[i : i + BATCH_SIZE]
|
batch_ids = staging_ids[i : i + BATCH_SIZE]
|
||||||
id_list = ", ".join(f"'{id}'" for id in batch_ids)
|
id_list = ", ".join(f"'{doc_id}'" for doc_id in batch_ids)
|
||||||
batch = (
|
batch = (
|
||||||
staging_table.search()
|
staging_table.search()
|
||||||
.where(f"id IN ({id_list})")
|
.where(f"id IN ({id_list})")
|
||||||
|
|
@ -174,7 +174,7 @@ def _apply_split_pages_zstd(store: Store) -> None: # pragma: no cover
|
||||||
|
|
||||||
for batch_num, i in enumerate(range(0, len(ids), BATCH_SIZE), 1):
|
for batch_num, i in enumerate(range(0, len(ids), BATCH_SIZE), 1):
|
||||||
batch_ids = ids[i : i + BATCH_SIZE]
|
batch_ids = ids[i : i + BATCH_SIZE]
|
||||||
id_list = ", ".join(f"'{id}'" for id in batch_ids)
|
id_list = ", ".join(f"'{doc_id}'" for doc_id in batch_ids)
|
||||||
|
|
||||||
batch = (
|
batch = (
|
||||||
store.documents_table.search()
|
store.documents_table.search()
|
||||||
|
|
@ -212,7 +212,7 @@ def _apply_split_pages_zstd(store: Store) -> None: # pragma: no cover
|
||||||
|
|
||||||
for batch_num, i in enumerate(range(0, len(staging_ids), BATCH_SIZE), 1):
|
for batch_num, i in enumerate(range(0, len(staging_ids), BATCH_SIZE), 1):
|
||||||
batch_ids = staging_ids[i : i + BATCH_SIZE]
|
batch_ids = staging_ids[i : i + BATCH_SIZE]
|
||||||
id_list = ", ".join(f"'{id}'" for id in batch_ids)
|
id_list = ", ".join(f"'{doc_id}'" for doc_id in batch_ids)
|
||||||
|
|
||||||
batch = (
|
batch = (
|
||||||
staging_table.search().where(f"id IN ({id_list})").to_arrow().to_pylist()
|
staging_table.search().where(f"id IN ({id_list})").to_arrow().to_pylist()
|
||||||
|
|
|
||||||
|
|
@ -367,6 +367,63 @@ async def test_get_docling_data_loads_only_docling_columns(
|
||||||
store.close()
|
store.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_pages_data_loads_only_pages_column(qa_corpus: Dataset, temp_db_path):
|
||||||
|
"""get_pages_data returns only page image data for a document."""
|
||||||
|
import json
|
||||||
|
|
||||||
|
from haiku.rag.store.compression import compress_json
|
||||||
|
|
||||||
|
pages_blob = compress_json(
|
||||||
|
json.dumps({"1": {"size": {"width": 612, "height": 792}, "page_no": 1}})
|
||||||
|
)
|
||||||
|
|
||||||
|
store = Store(temp_db_path, create=True)
|
||||||
|
doc_repo = DocumentRepository(store)
|
||||||
|
|
||||||
|
doc = Document(
|
||||||
|
content=qa_corpus[0]["document_extracted"],
|
||||||
|
uri="https://example.com/doc.txt",
|
||||||
|
docling_pages=pages_blob,
|
||||||
|
)
|
||||||
|
created = await doc_repo.create(doc)
|
||||||
|
assert created.id is not None
|
||||||
|
|
||||||
|
result = await doc_repo.get_pages_data(created.id)
|
||||||
|
assert result is not None
|
||||||
|
assert result.id == created.id
|
||||||
|
assert result.content == ""
|
||||||
|
assert result.docling_pages == pages_blob
|
||||||
|
|
||||||
|
# Non-existent ID returns None
|
||||||
|
assert await doc_repo.get_pages_data("nonexistent-id") is None
|
||||||
|
|
||||||
|
store.close()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_pages_data_none_for_markdown_document(
|
||||||
|
qa_corpus: Dataset, temp_db_path
|
||||||
|
):
|
||||||
|
"""Markdown documents have no page images — get_pages_data returns None pages."""
|
||||||
|
store = Store(temp_db_path, create=True)
|
||||||
|
doc_repo = DocumentRepository(store)
|
||||||
|
|
||||||
|
doc = Document(
|
||||||
|
content=qa_corpus[0]["document_extracted"],
|
||||||
|
uri="https://example.com/doc.md",
|
||||||
|
)
|
||||||
|
created = await doc_repo.create(doc)
|
||||||
|
assert created.id is not None
|
||||||
|
|
||||||
|
result = await doc_repo.get_pages_data(created.id)
|
||||||
|
assert result is not None
|
||||||
|
assert result.id == created.id
|
||||||
|
assert result.docling_pages is None
|
||||||
|
|
||||||
|
store.close()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_document_get_by_uri_with_special_characters(
|
async def test_document_get_by_uri_with_special_characters(
|
||||||
qa_corpus: Dataset, temp_db_path
|
qa_corpus: Dataset, temp_db_path
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue