feat: adiciona formatos e metadados ao RAG
This commit is contained in:
parent
e7850f0416
commit
c2c2fc716c
10 changed files with 400 additions and 21 deletions
|
|
@ -1,8 +1,10 @@
|
|||
import hashlib
|
||||
import zipfile
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO_ROOT / "bin"))
|
||||
|
|
@ -187,6 +189,97 @@ class IngestTests(unittest.TestCase):
|
|||
self.assertEqual(self.session.upsert_calls, [])
|
||||
self.assertEqual(self.session.delete_calls, [])
|
||||
|
||||
def test_supported_plain_and_html_formats_preserve_sections(self):
|
||||
(self.root / "nota.txt").write_text("Texto simples", encoding="utf-8")
|
||||
(self.root / "pagina.html").write_text(
|
||||
"<html><body><h1>Atendimento</h1><p>Horário da recepção.</p>"
|
||||
"<script>segredo que não deve ser indexado</script></body></html>",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
documents = ingest.discover_documents(self.root)
|
||||
|
||||
self.assertEqual([document.relative for document in documents], ["nota.txt", "pagina.html"])
|
||||
html_doc = documents[1]
|
||||
self.assertIn("Horário da recepção", html_doc.chunks[0])
|
||||
self.assertNotIn("segredo", " ".join(html_doc.chunks))
|
||||
self.assertEqual(html_doc.chunk_metadata[0]["section"], "Atendimento")
|
||||
|
||||
def test_docx_and_xlsx_extract_text_and_section_metadata(self):
|
||||
docx = self.root / "manual.docx"
|
||||
with zipfile.ZipFile(docx, "w") as archive:
|
||||
archive.writestr(
|
||||
"word/document.xml",
|
||||
"""<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
|
||||
<w:body><w:p><w:pPr><w:pStyle w:val="Heading1"/></w:pPr>
|
||||
<w:r><w:t>Cadastro</w:t></w:r></w:p><w:p><w:r><w:t>Dados necessários</w:t></w:r></w:p>
|
||||
</w:body></w:document>""",
|
||||
)
|
||||
xlsx = self.root / "dados.xlsx"
|
||||
with zipfile.ZipFile(xlsx, "w") as archive:
|
||||
archive.writestr(
|
||||
"xl/workbook.xml",
|
||||
"""<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
|
||||
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
||||
<sheets><sheet name="Unidades" sheetId="1" r:id="rId1"/></sheets></workbook>""",
|
||||
)
|
||||
archive.writestr(
|
||||
"xl/_rels/workbook.xml.rels",
|
||||
"""<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>""",
|
||||
)
|
||||
archive.writestr(
|
||||
"xl/worksheets/sheet1.xml",
|
||||
"""<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
||||
<sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>Unidade A</t></is></c>
|
||||
<c r="B1" t="inlineStr"><is><t>Centro</t></is></c></row></sheetData></worksheet>""",
|
||||
)
|
||||
|
||||
documents = ingest.discover_documents(self.root)
|
||||
by_name = {document.relative: document for document in documents}
|
||||
|
||||
self.assertIn("Dados necessários", " ".join(by_name["manual.docx"].chunks))
|
||||
self.assertEqual(by_name["manual.docx"].chunk_metadata[0]["section"], "Cadastro")
|
||||
self.assertIn("Unidade A | Centro", " ".join(by_name["dados.xlsx"].chunks))
|
||||
self.assertEqual(by_name["dados.xlsx"].chunk_metadata[0]["section"], "Unidades")
|
||||
|
||||
@patch.object(ingest.subprocess, "run")
|
||||
def test_pdf_pages_are_preserved_as_chunk_metadata(self, run):
|
||||
run.return_value.stdout = "Página um\fPágina dois\f"
|
||||
run.return_value.check_returncode = lambda: None
|
||||
(self.root / "manual.pdf").write_bytes(b"%PDF-mock")
|
||||
|
||||
document = ingest.discover_documents(self.root)[0]
|
||||
|
||||
self.assertEqual(document.chunks, ("Página um", "Página dois"))
|
||||
self.assertEqual([item["page"] for item in document.chunk_metadata], [1, 2])
|
||||
|
||||
def test_unsupported_only_root_fails_closed(self):
|
||||
(self.root / "imagem.png").write_bytes(b"not indexed")
|
||||
with self.assertRaisesRegex(ingest.IngestError, "Formatos não suportados"):
|
||||
ingest.discover_documents(self.root)
|
||||
|
||||
def test_mixed_supported_and_unsupported_files_fail_before_partial_plan(self):
|
||||
(self.root / "documento.md").write_text("conteúdo válido", encoding="utf-8")
|
||||
(self.root / "imagem.png").write_bytes(b"not indexed")
|
||||
|
||||
with self.assertRaisesRegex(ingest.IngestError, "imagem.png"):
|
||||
self.run_sync("dry-run")
|
||||
|
||||
self.assertEqual(self.session.upsert_calls, [])
|
||||
self.assertEqual(self.session.delete_calls, [])
|
||||
|
||||
def test_file_type_version_and_status_are_written_to_qdrant_payload(self):
|
||||
(self.root / "nota.txt").write_text("Texto indexável", encoding="utf-8")
|
||||
|
||||
self.run_sync("apply")
|
||||
|
||||
payload = next(iter(self.session.points.values()))["payload"]
|
||||
self.assertEqual(payload["file_type"], "txt")
|
||||
self.assertEqual(payload["document_version"], payload["document_hash"][:16])
|
||||
self.assertEqual(payload["document_status"], "active")
|
||||
self.assertRegex(payload["indexed_at"], r"^\d{4}-\d\d-\d\dT.*\+00:00$")
|
||||
|
||||
def test_unowned_points_are_preserved(self):
|
||||
(self.root / "a.md").write_text("conteúdo novo", encoding="utf-8")
|
||||
self.session.points["foreign-id"] = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue