Improve AI logic
This commit is contained in:
27
services/ai_insights/app/schemas/__init__.py
Normal file
27
services/ai_insights/app/schemas/__init__.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""Pydantic schemas for AI Insights Service."""
|
||||
|
||||
from app.schemas.insight import (
|
||||
AIInsightBase,
|
||||
AIInsightCreate,
|
||||
AIInsightUpdate,
|
||||
AIInsightResponse,
|
||||
AIInsightList,
|
||||
InsightMetrics,
|
||||
InsightFilters
|
||||
)
|
||||
from app.schemas.feedback import (
|
||||
InsightFeedbackCreate,
|
||||
InsightFeedbackResponse
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"AIInsightBase",
|
||||
"AIInsightCreate",
|
||||
"AIInsightUpdate",
|
||||
"AIInsightResponse",
|
||||
"AIInsightList",
|
||||
"InsightMetrics",
|
||||
"InsightFilters",
|
||||
"InsightFeedbackCreate",
|
||||
"InsightFeedbackResponse",
|
||||
]
|
||||
37
services/ai_insights/app/schemas/feedback.py
Normal file
37
services/ai_insights/app/schemas/feedback.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""Pydantic schemas for Insight Feedback."""
|
||||
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import Optional, Dict, Any
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
class InsightFeedbackBase(BaseModel):
|
||||
"""Base schema for Insight Feedback."""
|
||||
|
||||
action_taken: str
|
||||
result_data: Optional[Dict[str, Any]] = Field(default_factory=dict)
|
||||
success: bool
|
||||
error_message: Optional[str] = None
|
||||
expected_impact_value: Optional[Decimal] = None
|
||||
actual_impact_value: Optional[Decimal] = None
|
||||
variance_percentage: Optional[Decimal] = None
|
||||
|
||||
|
||||
class InsightFeedbackCreate(InsightFeedbackBase):
|
||||
"""Schema for creating feedback."""
|
||||
|
||||
insight_id: UUID
|
||||
applied_by: Optional[str] = "system"
|
||||
|
||||
|
||||
class InsightFeedbackResponse(InsightFeedbackBase):
|
||||
"""Schema for feedback response."""
|
||||
|
||||
id: UUID
|
||||
insight_id: UUID
|
||||
applied_by: str
|
||||
created_at: datetime
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
93
services/ai_insights/app/schemas/insight.py
Normal file
93
services/ai_insights/app/schemas/insight.py
Normal file
@@ -0,0 +1,93 @@
|
||||
"""Pydantic schemas for AI Insights."""
|
||||
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import Optional, Dict, Any, List
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
class AIInsightBase(BaseModel):
|
||||
"""Base schema for AI Insight."""
|
||||
|
||||
type: str = Field(..., description="optimization, alert, prediction, recommendation, insight, anomaly")
|
||||
priority: str = Field(..., description="low, medium, high, critical")
|
||||
category: str = Field(..., description="forecasting, inventory, production, procurement, customer, etc.")
|
||||
title: str = Field(..., max_length=255)
|
||||
description: str
|
||||
impact_type: Optional[str] = Field(None, description="cost_savings, revenue_increase, waste_reduction, etc.")
|
||||
impact_value: Optional[Decimal] = None
|
||||
impact_unit: Optional[str] = Field(None, description="euros, percentage, hours, units, etc.")
|
||||
confidence: int = Field(..., ge=0, le=100, description="Confidence score 0-100")
|
||||
metrics_json: Optional[Dict[str, Any]] = Field(default_factory=dict)
|
||||
actionable: bool = True
|
||||
recommendation_actions: Optional[List[Dict[str, str]]] = Field(default_factory=list)
|
||||
source_service: Optional[str] = None
|
||||
source_data_id: Optional[str] = None
|
||||
|
||||
|
||||
class AIInsightCreate(AIInsightBase):
|
||||
"""Schema for creating a new AI Insight."""
|
||||
|
||||
tenant_id: UUID
|
||||
|
||||
|
||||
class AIInsightUpdate(BaseModel):
|
||||
"""Schema for updating an AI Insight."""
|
||||
|
||||
status: Optional[str] = Field(None, description="new, acknowledged, in_progress, applied, dismissed, expired")
|
||||
applied_at: Optional[datetime] = None
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class AIInsightResponse(AIInsightBase):
|
||||
"""Schema for AI Insight response."""
|
||||
|
||||
id: UUID
|
||||
tenant_id: UUID
|
||||
status: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
applied_at: Optional[datetime] = None
|
||||
expired_at: Optional[datetime] = None
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class AIInsightList(BaseModel):
|
||||
"""Paginated list of AI Insights."""
|
||||
|
||||
items: List[AIInsightResponse]
|
||||
total: int
|
||||
page: int
|
||||
page_size: int
|
||||
total_pages: int
|
||||
|
||||
|
||||
class InsightMetrics(BaseModel):
|
||||
"""Aggregate metrics for insights."""
|
||||
|
||||
total_insights: int
|
||||
actionable_insights: int
|
||||
average_confidence: float
|
||||
high_priority_count: int
|
||||
medium_priority_count: int
|
||||
low_priority_count: int
|
||||
critical_priority_count: int
|
||||
by_category: Dict[str, int]
|
||||
by_status: Dict[str, int]
|
||||
total_potential_impact: Optional[Decimal] = None
|
||||
|
||||
|
||||
class InsightFilters(BaseModel):
|
||||
"""Filters for querying insights."""
|
||||
|
||||
category: Optional[str] = None
|
||||
priority: Optional[str] = None
|
||||
status: Optional[str] = None
|
||||
actionable_only: bool = False
|
||||
min_confidence: int = 0
|
||||
source_service: Optional[str] = None
|
||||
from_date: Optional[datetime] = None
|
||||
to_date: Optional[datetime] = None
|
||||
Reference in New Issue
Block a user