diff --git a/src/mlops_nlp/utils/drift.py b/src/mlops_nlp/utils/drift.py index a56241f..3e0e85f 100644 --- a/src/mlops_nlp/utils/drift.py +++ b/src/mlops_nlp/utils/drift.py @@ -15,6 +15,32 @@ LOGGER = get_logger(__name__) +def log_inference( + log_path: str | Path, + text: str, + prediction: str, + confidence: float, + model_version: str, +) -> None: + """Logs inference data to a JSONL file for future drift detection.""" + from datetime import datetime, timezone + log_entry = { + "timestamp": datetime.now(timezone.utc).isoformat(), + "text": text, + "prediction": prediction, + "confidence": confidence, + "model_version": model_version, + } + + path = Path(log_path) + path.parent.mkdir(parents=True, exist_ok=True) + + try: + with path.open("a", encoding="utf-8") as f: + f.write(json.dumps(log_entry) + "\n") + except Exception as e: + LOGGER.error("Failed to log inference data: %s", e) + def run_drift_check(config: AppConfig) -> Dict[str, Any]: """ Runs a drift detection check comparing reference (training) data