feat(ingestion): implement Phase 4 chunking and vector embedding generation

- Add heading and paragraph-preserving chunking algorithms with bounded overlap
- Implement OpenAI embeddings client adapter with explicit token limits and batching
- Implement stage_document pipeline with strict operational limits and memory protections
- Add unit and integration tests covering chunking boundaries, timeouts, and vector generation
This commit is contained in:
amirhosein mazaheri
2026-10-07 16:21:17 +03:30
parent 5ef02d39a9
commit 39ed0422c5
11 changed files with 2830 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
"""Phase 3 preparation only. No claims, attachment, embedding or publication."""
from dataclasses import dataclass
import hashlib
from .chunking import chunk_extraction
from .types import ChunkDraft, ExtractionEnvelope
from .extraction import ExtractionError
from .extraction_artifacts import validate_artifact, serialize_artifact
@dataclass(frozen=True)
class PreparedExtraction:
envelope: ExtractionEnvelope
chunks: tuple[ChunkDraft, ...]
candidate: object
def prepare_extraction(store, candidates, extractor, *, document, corpus_purpose="source_document"):
profile = document["processing_profile"]
content = store.read(document["storage_key"])
if (len(content) != document["size_bytes"]
or hashlib.sha256(content).hexdigest() != document["file_sha256"]):
raise ExtractionError("Immutable original integrity mismatch")
envelope = extractor.extract(document_id=document["id"], title=document["title"],
original_sha256=document["file_sha256"], content=content,
mime_type=document["mime_type"], processing_profile=profile)
payload = serialize_artifact(envelope, profile)
envelope = validate_artifact(payload, artifact_sha256=hashlib.sha256(payload).hexdigest(),
document_id=document["id"], original_sha256=document["file_sha256"], processing_profile=profile)
# Candidate retention is independent of OCR/chunk/embedding identity. Nothing
# here attaches a pointer to a document or accepts a candidate for runtime reuse.
candidate = candidates.put(envelope, processing_profile=profile)
chunks = chunk_extraction(envelope, profile, corpus_purpose=corpus_purpose)
return PreparedExtraction(envelope, chunks, candidate)