from datetime import datetime
from kodexa_document import ProcessingStep
from kodexa_document.processing.processing_step import KnowledgeItem, KnowledgeFeature
# Build a processing pipeline history
pipeline = ProcessingStep(
name="Invoice Processing Pipeline",
start_timestamp=datetime.now(),
metadata={"version": "3.0"}
)
# Stage 1: PDF extraction
extraction = ProcessingStep(
name="PDF Extraction",
metadata={"model": "fast-pdf-model", "pages": 3}
)
pipeline.add_child(extraction)
# Stage 2: Classification
classification = ProcessingStep(
name="Document Classification",
metadata={"result": "invoice", "confidence": 0.98}
)
pipeline.add_child(classification)
# Stage 3: Data extraction (depends on both previous steps)
data_extraction = ProcessingStep(
name="Data Extraction",
metadata={"fields_extracted": 12}
)
extraction.add_child(data_extraction)
classification.add_child(data_extraction)
# Attach knowledge item to the data extraction step
item = KnowledgeItem(
title="Invoice #12345",
knowledge_item_type_ref="knowledge-item-type://acme/invoice",
properties={"vendor": "Acme Corp", "total": 1234.56}
)
item.features.append(KnowledgeFeature(
feature_type_ref="knowledge-feature-type://acme/extraction-score",
properties={"overall_confidence": 0.95}
))
data_extraction.knowledge_items.append(item)
# Serialize the full pipeline
pipeline_json = pipeline.to_json()
# Deserialize later
restored = ProcessingStep.from_json(pipeline_json)
print(f"Pipeline: {restored.name}")
print(f"Steps: {len(restored.children)}")