ML_classifier / api /schemas.py
MadalinaCarcea221989
Deploy CloudPulse AI service
81e3338
Raw History Blame Contribute Delete
7.8 kB
"""
Pydantic schemas for the CloudPulse AI service.
These define the request and response contracts that the data engineer's pipeline
will conform to. Every endpoint is fully typed, every field is documented.
"""
from datetime import datetime
from typing import Literal, Optional
from pydantic import BaseModel, Field
# Severity values used across the system
Severity4Class = Literal["low", "medium", "high", "critical"]
Severity3Class = Literal["low", "medium", "high"]
SeverityBinary = Literal["severe", "not_severe"]
OriginalImpact = Literal["none", "minor", "major", "critical"]
# ─────────────────────────────────────────────────────────────────────────────
# /classify
# ─────────────────────────────────────────────────────────────────────────────
class ClassifyRequest(BaseModel):
"""Incident classification request from the data engineer's pipeline."""
incident_id: str = Field(..., description="Unique incident identifier")
description: str = Field(..., min_length=1, description="Raw incident description text")
original_impact: Optional[OriginalImpact] = Field(
None,
description="Provider's own severity tag from the upstream API. Used by the "
"escalation rule to upgrade high to critical when applicable.",
)
provider: Optional[str] = Field(None, description="Cloud or service provider name")
service: Optional[str] = Field(None, description="Affected service name")
affected_services_count: Optional[int] = Field(
None, ge=0, description="Number of affected services for anomaly detection"
)
service_impact: Optional[str] = Field(
None, description="Type of service impact (compute, ai, platform, storage, network)"
)
class RemediationBlock(BaseModel):
"""Structured remediation generated by Claude Haiku."""
summary: str
root_cause_hypothesis: str
immediate_mitigation: list[str]
investigation: list[str]
prevention: list[str]
class ClassifyResponse(BaseModel):
"""Full classification response sent back to the pipeline."""
incident_id: str
ml_severity_3class: Severity3Class = Field(
..., description="Raw 3-class prediction from the trained classifier"
)
final_severity: Severity4Class = Field(
..., description="Final label after escalation rule. high becomes critical "
"if original_impact is critical."
)
severity_binary: SeverityBinary = Field(
..., description="Binary classification (severe vs not_severe)"
)
confidence: Literal["high", "medium", "low"] = Field(
..., description="Confidence label derived from model probability"
)
confidence_score: float = Field(
..., ge=0.0, le=1.0, description="Probability of the predicted class"
)
escalated: bool = Field(
..., description="True if the rule upgraded the model prediction to critical"
)
escalation_reason: Optional[str] = Field(
None, description="Why the escalation fired (or None)"
)
explanation: dict = Field(
..., description="Top contributing TF-IDF terms with weights"
)
anomaly: dict = Field(
..., description="Anomaly detection result"
)
remediation: Optional[RemediationBlock] = Field(
None, description="Structured remediation. Returned when ?include_remediation=true"
)
processing_ms: int
# ─────────────────────────────────────────────────────────────────────────────
# /similar
# ─────────────────────────────────────────────────────────────────────────────
class SimilarRequest(BaseModel):
description: str = Field(..., min_length=1)
top_k: int = Field(3, ge=1, le=10)
class SimilarIncident(BaseModel):
incident_id: str
provider: str
service: Optional[str]
severity: str
description: str
similarity: float = Field(..., ge=0.0, le=1.0)
class SimilarResponse(BaseModel):
results: list[SimilarIncident]
# ─────────────────────────────────────────────────────────────────────────────
# /metrics
# ─────────────────────────────────────────────────────────────────────────────
class MetricsResponse(BaseModel):
"""Static evaluation results loaded from notebook 09."""
dataset_size: int
train_size: int
val_size: int
test_size: int
best_3class_model: str
best_3class_macro_f1: float
best_binary_model: str
best_binary_macro_f1: float
per_class_f1: dict
# ─────────────────────────────────────────────────────────────────────────────
# /health
# ─────────────────────────────────────────────────────────────────────────────
class HealthResponse(BaseModel):
status: Literal["ok", "degraded"]
artefacts_loaded: dict[str, bool]
timestamp: datetime
# ─────────────────────────────────────────────────────────────────────────────
# /weekly
# ─────────────────────────────────────────────────────────────────────────────
class ClassifiedIncidentInput(BaseModel):
"""Input for an already-classified incident in the weekly batch.
Mădălina'\''s pipeline classifies incidents via /classify throughout the week,
then batches them and posts to /weekly for the natural-language report.
"""
incident_id : str
provider : str
service : Optional[str] = None
final_severity: Severity4Class
description : Optional[str] = None
class WeeklyRequest(BaseModel):
"""Weekly intelligence pass request."""
period_start : str = Field(..., description="ISO date, e.g. 2026-04-27")
period_end : str = Field(..., description="ISO date, e.g. 2026-05-04")
incidents : list[ClassifiedIncidentInput]
previous_week: Optional[list[ClassifiedIncidentInput]] = Field(
None,
description="Previous week incidents for week-on-week trend deltas. Optional.",
)
class WeeklyResponse(BaseModel):
"""Weekly intelligence pass response."""
period_start : str
period_end : str
total_incidents: int
summary : str = Field(..., description="Natural-language report from Claude Haiku")
aggregation : Optional[dict] = Field(
None,
description="Deterministic aggregation: hotspots, severity counts, trend deltas, notable incidents",
)