Improve AI logic

This commit is contained in:
Urtzi Alfaro
2025-11-05 13:34:56 +01:00
parent 5c87fbcf48
commit 394ad3aea4
218 changed files with 30627 additions and 7658 deletions

View File

@@ -141,6 +141,27 @@ class ForecastServiceClient(BaseServiceClient):
}
return await self.post("forecasting/operations/validate-predictions", params=params, tenant_id=tenant_id)
async def validate_forecasts(
self,
tenant_id: str,
date: date
) -> Optional[Dict[str, Any]]:
"""
Validate forecasts for a specific date against actual sales.
Calculates MAPE, RMSE, MAE and identifies products with poor accuracy.
Args:
tenant_id: Tenant UUID
date: Date to validate
Returns:
Dict with overall metrics and poor accuracy products list
"""
params = {
"validation_date": date.isoformat()
}
return await self.post("forecasting/operations/validate-forecasts", params=params, tenant_id=tenant_id)
async def get_forecast_statistics(
self,
tenant_id: str,
@@ -179,10 +200,81 @@ class ForecastServiceClient(BaseServiceClient):
return await self.get("forecasting/analytics/predictions-performance", tenant_id=tenant_id, params=params)
# ================================================================
# ML INSIGHTS: Dynamic Rules Generation
# ================================================================
async def trigger_rules_generation(
self,
tenant_id: str,
product_ids: Optional[List[str]] = None,
lookback_days: int = 90,
min_samples: int = 10
) -> Optional[Dict[str, Any]]:
"""
Trigger dynamic business rules learning for demand forecasting.
Args:
tenant_id: Tenant UUID
product_ids: Specific product IDs to analyze. If None, analyzes all products
lookback_days: Days of historical data to analyze (30-365)
min_samples: Minimum samples required for rule learning (5-100)
Returns:
Dict with rules generation results including insights posted
"""
data = {
"product_ids": product_ids,
"lookback_days": lookback_days,
"min_samples": min_samples
}
return await self.post("forecasting/ml/insights/generate-rules", data=data, tenant_id=tenant_id)
# ================================================================
# Legacy/Compatibility Methods (deprecated)
# ================================================================
async def generate_forecasts(
self,
tenant_id: str,
forecast_days: int = 7,
inventory_product_ids: Optional[List[str]] = None
) -> Optional[Dict[str, Any]]:
"""
COMPATIBILITY: Orchestrator-friendly method to generate forecasts
This method is called by the orchestrator service and generates batch forecasts
for either specified products or all products.
Args:
tenant_id: Tenant UUID
forecast_days: Number of days to forecast (default 7)
inventory_product_ids: Optional list of product IDs. If None, forecasts all products.
Returns:
Dict with forecast results
"""
from datetime import datetime
# If no product IDs specified, let the backend handle it
if not inventory_product_ids:
# Call the batch operation endpoint to forecast all products
# The forecasting service will handle fetching all products internally
data = {
"batch_name": f"orchestrator-batch-{datetime.now().strftime('%Y%m%d')}",
"inventory_product_ids": [], # Empty list will trigger fetching all products
"forecast_days": forecast_days
}
return await self.post("forecasting/operations/batch", data=data, tenant_id=tenant_id)
# Otherwise use the standard batch forecast
return await self.generate_batch_forecast(
tenant_id=tenant_id,
inventory_product_ids=inventory_product_ids,
forecast_date=datetime.now().date(),
forecast_days=forecast_days
)
async def create_forecast(
self,
tenant_id: str,