Start fixing forecast service API 12
This commit is contained in:
@@ -73,20 +73,42 @@ class ModelClient:
|
||||
logger.warning("No trained models found", tenant_id=tenant_id)
|
||||
return None
|
||||
|
||||
# Get model metrics to validate quality
|
||||
metrics = await self.clients.training.get_model_metrics(
|
||||
tenant_id=tenant_id,
|
||||
model_id=latest_model["id"]
|
||||
)
|
||||
|
||||
if metrics and metrics.get("accuracy", 0) > 0.7: # 70% accuracy threshold
|
||||
logger.info(f"Selected model {latest_model['id']} with accuracy {metrics.get('accuracy')}",
|
||||
tenant_id=tenant_id)
|
||||
return latest_model
|
||||
else:
|
||||
logger.warning(f"Model accuracy too low: {metrics.get('accuracy', 'unknown')}",
|
||||
tenant_id=tenant_id)
|
||||
# ✅ FIX 1: Use "model_id" instead of "id"
|
||||
model_id = latest_model.get("model_id")
|
||||
if not model_id:
|
||||
logger.error("Model response missing model_id field", tenant_id=tenant_id)
|
||||
return None
|
||||
|
||||
# ✅ FIX 2: Handle metrics endpoint failure gracefully
|
||||
try:
|
||||
# Get model metrics to validate quality
|
||||
metrics = await self.clients.training.get_model_metrics(
|
||||
tenant_id=tenant_id,
|
||||
model_id=model_id
|
||||
)
|
||||
|
||||
# If metrics call succeeded, check accuracy threshold
|
||||
if metrics and metrics.get("accuracy", 0) > 0.7: # 70% accuracy threshold
|
||||
logger.info(f"Selected model {model_id} with accuracy {metrics.get('accuracy')}",
|
||||
tenant_id=tenant_id)
|
||||
return latest_model
|
||||
elif metrics:
|
||||
logger.warning(f"Model accuracy too low: {metrics.get('accuracy', 'unknown')}",
|
||||
tenant_id=tenant_id)
|
||||
# Still return the model even if accuracy is low - better than no prediction
|
||||
logger.info("Returning model despite low accuracy - no alternative available",
|
||||
tenant_id=tenant_id)
|
||||
return latest_model
|
||||
else:
|
||||
logger.warning("No metrics returned from training service", tenant_id=tenant_id)
|
||||
# Return model anyway - metrics service might be temporarily down
|
||||
return latest_model
|
||||
|
||||
except Exception as metrics_error:
|
||||
# ✅ FIX 3: If metrics endpoint fails, still return the model
|
||||
logger.warning(f"Failed to get model metrics: {metrics_error}", tenant_id=tenant_id)
|
||||
logger.info("Proceeding with model despite metrics failure", tenant_id=tenant_id)
|
||||
return latest_model
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error selecting best model: {e}", tenant_id=tenant_id)
|
||||
|
||||
Reference in New Issue
Block a user