AI UPDATE
This commit is contained in:
@@ -46,15 +46,85 @@ AI_DRIVEN_CARDS = {
|
||||
}
|
||||
|
||||
|
||||
def _farm_profile_from_context(context: dict) -> dict:
|
||||
sensor = context.get("sensor")
|
||||
location = context.get("location")
|
||||
plants = context.get("plants", [])
|
||||
irrigation_methods = context.get("irrigation_methods", [])
|
||||
|
||||
return {
|
||||
"sensor_id": str(getattr(sensor, "uuid_sensor", "")) if sensor else "",
|
||||
"crop_type": getattr(plants[0], "name", None) if plants else None,
|
||||
"region": {
|
||||
"latitude": float(location.latitude) if location else None,
|
||||
"longitude": float(location.longitude) if location else None,
|
||||
},
|
||||
"season": timezone.now().date().isoformat(),
|
||||
"farming_method": getattr(irrigation_methods[0], "name", None) if irrigation_methods else None,
|
||||
}
|
||||
|
||||
|
||||
def _sensor_trends_payload(sensor_id: str, context: dict) -> dict:
|
||||
chart = build_sensor_comparison_chart(sensor_id=sensor_id, context=context, ai_bundle=None)
|
||||
return {
|
||||
"current_value": chart.get("currentValue"),
|
||||
"current_value_quality": chart.get("currentValueQuality"),
|
||||
"vs_last_week": chart.get("vsLastWeekValue"),
|
||||
"series": chart.get("series", []),
|
||||
}
|
||||
|
||||
|
||||
def _alerts_payload(sensor_id: str, context: dict) -> dict:
|
||||
tracker = build_farm_alerts_tracker(sensor_id=sensor_id, context=context, ai_bundle=None)
|
||||
return {
|
||||
"total_alerts": tracker.get("totalAlerts", 0),
|
||||
"alerts": tracker.get("alerts", []),
|
||||
"most_critical_issue": tracker.get("mostCriticalIssue"),
|
||||
"clusters": tracker.get("alertClusters", []),
|
||||
}
|
||||
|
||||
|
||||
def _anomalies_payload(sensor_id: str, context: dict) -> dict:
|
||||
anomaly_card = build_anomaly_detection_card(sensor_id=sensor_id, context=context, ai_bundle=None)
|
||||
return {
|
||||
"anomalies": anomaly_card.get("anomalies", []),
|
||||
"interpretation_seed": anomaly_card.get("interpretation"),
|
||||
}
|
||||
|
||||
|
||||
def _build_ai_payload_request(sensor_id: str, context: dict) -> dict:
|
||||
structured_context = {
|
||||
"farm_profile": _farm_profile_from_context(context),
|
||||
"detected_alerts": _alerts_payload(sensor_id, context),
|
||||
"anomaly_events": _anomalies_payload(sensor_id, context),
|
||||
"sensor_trends": _sensor_trends_payload(sensor_id, context),
|
||||
"timestamps": {
|
||||
"generated_at": timezone.now().isoformat(),
|
||||
},
|
||||
}
|
||||
system_prompt = (
|
||||
"You are an agricultural decision-support assistant. "
|
||||
"Use only the structured data provided. "
|
||||
"Do not hallucinate sensor values, timestamps, severities, or agronomic events. "
|
||||
"Generate concise, actionable outputs.\n\n"
|
||||
"For the timeline, explain what happened, when it happened, and why it matters.\n"
|
||||
"For recommendations, prioritize by alert severity, time proximity, and potential crop impact.\n"
|
||||
"Return recommendation objects with: recommendation_title, explanation, suggested_action, urgency_level, related_alert_id (optional)."
|
||||
)
|
||||
return {
|
||||
"sensor_id": sensor_id,
|
||||
"cards": sorted(AI_DRIVEN_CARDS),
|
||||
"system_prompt": system_prompt,
|
||||
"structured_context": structured_context,
|
||||
}
|
||||
|
||||
|
||||
def build_dashboard_payload(sensor_id: str) -> dict:
|
||||
context = load_dashboard_context(sensor_id)
|
||||
if context is None:
|
||||
return {}
|
||||
|
||||
ai_payload_request = {
|
||||
"sensor_id": sensor_id,
|
||||
"cards": sorted(AI_DRIVEN_CARDS),
|
||||
}
|
||||
ai_payload_request = _build_ai_payload_request(sensor_id, context)
|
||||
ai_bundle = request_dashboard_ai_bundle(sensor_id=sensor_id, payload=ai_payload_request)
|
||||
|
||||
return {
|
||||
@@ -88,10 +158,7 @@ def build_dashboard_payload_with_cache(sensor_id: str) -> dict:
|
||||
if context is None:
|
||||
return {}
|
||||
|
||||
ai_payload_request = {
|
||||
"sensor_id": sensor_id,
|
||||
"cards": sorted(AI_DRIVEN_CARDS),
|
||||
}
|
||||
ai_payload_request = _build_ai_payload_request(sensor_id, context)
|
||||
ai_bundle = request_dashboard_ai_bundle(sensor_id=sensor_id, payload=ai_payload_request)
|
||||
|
||||
payload = {}
|
||||
|
||||
Reference in New Issue
Block a user