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:
@@ -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)
|
||||
Reference in New Issue
Block a user