Simplify _resolve_title()

This commit is contained in:
Yiorgis Gozadinos 2026-02-26 10:56:28 +02:00
parent d3a1031ec4
commit b5cb2852c4
No known key found for this signature in database
2 changed files with 15 additions and 30 deletions

View file

@ -310,20 +310,13 @@ class HaikuRAG:
async def _resolve_title(
self,
title: str | None,
docling_document: "DoclingDocument",
content: str,
) -> str | None:
"""Resolve the title for a document.
"""Auto-generate a title from document structure or LLM.
1. Explicit title always wins.
2. If auto_title is disabled, return None.
3. Try structural extraction from docling metadata.
4. Fall back to LLM generation.
Returns None if auto_title is disabled or generation fails.
"""
if title is not None:
return title
if not self._config.processing.auto_title:
return None
@ -493,7 +486,8 @@ class HaikuRAG:
# The original content is preserved in docling_document
stored_content = docling_document.export_to_markdown()
title = await self._resolve_title(title, docling_document, stored_content)
if title is None:
title = await self._resolve_title(docling_document, stored_content)
# Create document model
document = Document(
@ -533,7 +527,8 @@ class HaikuRAG:
The created Document instance.
"""
content = docling_document.export_to_markdown()
title = await self._resolve_title(title, docling_document, content)
if title is None:
title = await self._resolve_title(docling_document, content)
document = Document(
content=content,
@ -685,14 +680,15 @@ class HaikuRAG:
existing_doc.title = title
elif existing_doc.title is None:
existing_doc.title = await self._resolve_title(
None, docling_document, stored_content
docling_document, stored_content
)
return await self._update_document_with_chunks(
existing_doc, embedded_chunks
)
else:
# Create new document
title = await self._resolve_title(title, docling_document, stored_content)
if title is None:
title = await self._resolve_title(docling_document, stored_content)
document = Document(
content=stored_content,
uri=uri,
@ -801,16 +797,15 @@ class HaikuRAG:
existing_doc.title = title
elif existing_doc.title is None:
existing_doc.title = await self._resolve_title(
None, docling_document, stored_content
docling_document, stored_content
)
return await self._update_document_with_chunks(
existing_doc, embedded_chunks
)
else:
# Create new document
title = await self._resolve_title(
title, docling_document, stored_content
)
if title is None:
title = await self._resolve_title(docling_document, stored_content)
document = Document(
content=stored_content,
uri=url,

View file

@ -143,16 +143,6 @@ class TestResolveTitle:
config = AppConfig(processing=ProcessingConfig(auto_title=auto_title))
return HaikuRAG(tmp_path / "test.lancedb", config=config, create=True)
@pytest.mark.asyncio
async def test_explicit_title_always_wins(self, tmp_path):
"""Caller-supplied title is never overridden."""
doc = DoclingDocument(name="test")
doc.add_text(label=DocItemLabel.TITLE, text="Structural Title")
client = self._make_client(tmp_path)
result = await client._resolve_title("My Explicit Title", doc, "some content")
assert result == "My Explicit Title"
@pytest.mark.asyncio
async def test_auto_title_disabled_returns_none(self, tmp_path):
"""When auto_title is False, returns None (no title generation)."""
@ -160,7 +150,7 @@ class TestResolveTitle:
doc.add_text(label=DocItemLabel.TITLE, text="Structural Title")
client = self._make_client(tmp_path, auto_title=False)
result = await client._resolve_title(None, doc, "some content")
result = await client._resolve_title(doc, "some content")
assert result is None
@pytest.mark.asyncio
@ -170,7 +160,7 @@ class TestResolveTitle:
doc.add_text(label=DocItemLabel.TITLE, text="Auto Extracted Title")
client = self._make_client(tmp_path)
result = await client._resolve_title(None, doc, "some content")
result = await client._resolve_title(doc, "some content")
assert result == "Auto Extracted Title"
@pytest.mark.asyncio
@ -185,7 +175,7 @@ class TestResolveTitle:
raise RuntimeError("LLM is down")
monkeypatch.setattr(HaikuRAG, "_generate_title_with_llm", exploding_llm)
result = await client._resolve_title(None, doc, "some content")
result = await client._resolve_title(doc, "some content")
assert result is None