UPDATE
This commit is contained in:
+3
-40
@@ -5,43 +5,6 @@ from typing import Any
|
||||
|
||||
class EconomicOverviewService:
|
||||
def get_economic_overview(self, *, farm_uuid: str) -> dict[str, Any]:
|
||||
return {
|
||||
"farm_uuid": farm_uuid,
|
||||
"source": "mock",
|
||||
"economicData": [
|
||||
{
|
||||
"title": "هزینه آب",
|
||||
"value": "$420",
|
||||
"subtitle": "این ماه",
|
||||
"avatarIcon": "tabler-droplet",
|
||||
"avatarColor": "primary",
|
||||
},
|
||||
{
|
||||
"title": "صرفه جویی هوشمند",
|
||||
"value": "$88",
|
||||
"subtitle": "برآورد ماهانه",
|
||||
"avatarIcon": "tabler-bulb",
|
||||
"avatarColor": "success",
|
||||
},
|
||||
{
|
||||
"title": "پیش بینی درآمد",
|
||||
"value": "$3.8k",
|
||||
"subtitle": "این فصل",
|
||||
"avatarIcon": "tabler-chart-line",
|
||||
"avatarColor": "info",
|
||||
},
|
||||
{
|
||||
"title": "هزینه کود",
|
||||
"value": "$190",
|
||||
"subtitle": "برآورد فعلی",
|
||||
"avatarIcon": "tabler-flask",
|
||||
"avatarColor": "warning",
|
||||
},
|
||||
],
|
||||
"chartSeries": [
|
||||
{"name": "هزینه آب", "data": [320, 340, 360, 390, 405, 420]},
|
||||
{"name": "هزینه کود", "data": [150, 155, 160, 170, 180, 190]},
|
||||
{"name": "درآمد", "data": [2200, 2400, 2650, 3000, 3400, 3800]},
|
||||
],
|
||||
"chartCategories": ["فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور"],
|
||||
}
|
||||
raise NotImplementedError(
|
||||
f"Economic overview has no real data source configured for farm {farm_uuid}."
|
||||
)
|
||||
|
||||
@@ -9,14 +9,12 @@ class EconomicOverviewApiTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
|
||||
def test_economic_overview_api_returns_mock_payload(self):
|
||||
def test_economic_overview_api_returns_service_unavailable_without_real_data(self):
|
||||
response = self.client.post(
|
||||
"/overview/",
|
||||
data={"farm_uuid": "550e8400-e29b-41d4-a716-446655440000"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
payload = response.json()["data"]
|
||||
self.assertEqual(payload["source"], "mock")
|
||||
self.assertEqual(payload["economicData"][0]["title"], "هزینه آب")
|
||||
self.assertEqual(response.status_code, 503)
|
||||
self.assertIsNone(response.json()["data"])
|
||||
|
||||
+14
-4
@@ -28,7 +28,7 @@ class EconomicOverviewView(APIView):
|
||||
@extend_schema(
|
||||
tags=["Economy"],
|
||||
summary="دریافت نمای اقتصادی مزرعه",
|
||||
description="با دریافت farm_uuid، نمای اقتصادی مزرعه را فعلا با داده mock برمی گرداند.",
|
||||
description="با دریافت farm_uuid، نمای اقتصادی مزرعه را از منبع واقعی برمی گرداند.",
|
||||
request=EconomicOverviewRequestSerializer,
|
||||
responses={
|
||||
200: build_response(
|
||||
@@ -39,6 +39,10 @@ class EconomicOverviewView(APIView):
|
||||
EconomyErrorSerializer,
|
||||
"داده ورودی نامعتبر است.",
|
||||
),
|
||||
503: build_response(
|
||||
EconomyErrorSerializer,
|
||||
"منبع داده نمای اقتصادی در دسترس نیست.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
@@ -57,9 +61,15 @@ class EconomicOverviewView(APIView):
|
||||
)
|
||||
|
||||
service = apps.get_app_config("economy").get_economic_overview_service()
|
||||
data = service.get_economic_overview(
|
||||
farm_uuid=str(serializer.validated_data["farm_uuid"])
|
||||
)
|
||||
try:
|
||||
data = service.get_economic_overview(
|
||||
farm_uuid=str(serializer.validated_data["farm_uuid"])
|
||||
)
|
||||
except NotImplementedError as exc:
|
||||
return Response(
|
||||
{"code": 503, "msg": str(exc), "data": None},
|
||||
status=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": data},
|
||||
status=status.HTTP_200_OK,
|
||||
|
||||
+34
-72
@@ -142,60 +142,32 @@ def _build_structured_context(farm_uuid: str) -> tuple[dict[str, Any], dict[str,
|
||||
return context, structured
|
||||
|
||||
|
||||
def _build_fallback_notifications(tracker: dict[str, Any], endpoint: str) -> list[dict[str, Any]]:
|
||||
notifications: list[dict[str, Any]] = []
|
||||
for alert in tracker.get("alerts", [])[:5]:
|
||||
notifications.append(
|
||||
{
|
||||
"level": _severity_to_level(alert.get("severity")),
|
||||
"title": alert.get("title") or "هشدار مزرعه",
|
||||
"message": alert.get("summary") or alert.get("explanation") or "",
|
||||
"suggested_action": alert.get("recommended_action") or "",
|
||||
"source_alert_id": _alert_identifier(alert),
|
||||
"source_metric_type": alert.get("metric_type") or "",
|
||||
"payload": {
|
||||
"endpoint": endpoint,
|
||||
"alert": alert,
|
||||
},
|
||||
}
|
||||
def _validate_tracker_response(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
required_keys = {"headline", "overview", "status_level", "notifications"}
|
||||
missing = [key for key in required_keys if key not in payload]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Farm alerts tracker response is missing required fields: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
return notifications
|
||||
if not isinstance(payload.get("notifications"), list):
|
||||
raise ValueError("Farm alerts tracker notifications must be a list.")
|
||||
return payload
|
||||
|
||||
|
||||
def _build_fallback_tracker_response(tracker: dict[str, Any]) -> dict[str, Any]:
|
||||
top_alert = tracker.get("mostCriticalIssue") or {}
|
||||
status_level = _severity_to_level(top_alert.get("severity")) if top_alert else FarmAlertNotification.LEVEL_INFO
|
||||
if tracker.get("totalAlerts", 0) <= 0:
|
||||
overview = "در حال حاضر هشدار فعالی برای مزرعه شناسایی نشده است."
|
||||
else:
|
||||
overview = top_alert.get("summary") or "چند هشدار فعال برای مزرعه شناسایی شده است."
|
||||
return {
|
||||
"headline": "ارزیابی فعلی هشدارهای مزرعه",
|
||||
"overview": overview,
|
||||
"status_level": status_level,
|
||||
"notifications": _build_fallback_notifications(tracker, FarmAlertNotification.ENDPOINT_TRACKER),
|
||||
}
|
||||
|
||||
|
||||
def _build_fallback_timeline_response(tracker: dict[str, Any]) -> dict[str, Any]:
|
||||
timeline = []
|
||||
for alert in tracker.get("alerts", [])[:6]:
|
||||
timeline.append(
|
||||
{
|
||||
"timestamp": alert.get("timestamp"),
|
||||
"level": _severity_to_level(alert.get("severity")),
|
||||
"title": alert.get("title") or "رویداد هشدار",
|
||||
"description": alert.get("explanation") or alert.get("summary") or "",
|
||||
"source_alert_id": _alert_identifier(alert),
|
||||
"source_metric_type": alert.get("metric_type") or "",
|
||||
}
|
||||
def _validate_timeline_response(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
required_keys = {"headline", "overview", "timeline", "notifications"}
|
||||
missing = [key for key in required_keys if key not in payload]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Farm alerts timeline response is missing required fields: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
return {
|
||||
"headline": "خط زمانی هشدارهای مزرعه",
|
||||
"overview": "timeline بر اساس هشدارهای محاسبه شده مزرعه ساخته شد.",
|
||||
"timeline": timeline,
|
||||
"notifications": _build_fallback_notifications(tracker, FarmAlertNotification.ENDPOINT_TIMELINE),
|
||||
}
|
||||
if not isinstance(payload.get("timeline"), list):
|
||||
raise ValueError("Farm alerts timeline must be a list.")
|
||||
if not isinstance(payload.get("notifications"), list):
|
||||
raise ValueError("Farm alerts timeline notifications must be a list.")
|
||||
return payload
|
||||
|
||||
|
||||
def _notification_fingerprint(
|
||||
@@ -354,12 +326,14 @@ def _llm_response(
|
||||
response = client.chat.completions.create(model=model, messages=messages)
|
||||
raw = response.choices[0].message.content.strip()
|
||||
parsed = _clean_json_response(raw)
|
||||
if not parsed:
|
||||
raise ValueError("farm_alerts LLM returned an empty or invalid JSON payload.")
|
||||
_complete_audit_log(audit_log, raw)
|
||||
return parsed, raw, service.tone_file or ""
|
||||
except Exception as exc:
|
||||
logger.error("farm_alerts llm error for %s: %s", farm_uuid, exc)
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
return {}, "", service.tone_file or ""
|
||||
raise RuntimeError(f"Farm alerts generation failed for farm {farm_uuid}.") from exc
|
||||
|
||||
|
||||
def get_farm_alerts_tracker(*, farm_uuid: str, query: str | None = None) -> dict[str, Any]:
|
||||
@@ -373,12 +347,9 @@ def get_farm_alerts_tracker(*, farm_uuid: str, query: str | None = None) -> dict
|
||||
query=user_query,
|
||||
structured_context=structured_context,
|
||||
)
|
||||
if not llm_result:
|
||||
llm_result = _build_fallback_tracker_response(tracker)
|
||||
llm_result = _validate_tracker_response(llm_result)
|
||||
|
||||
notifications_input = llm_result.get("notifications")
|
||||
if not isinstance(notifications_input, list):
|
||||
notifications_input = _build_fallback_notifications(tracker, FarmAlertNotification.ENDPOINT_TRACKER)
|
||||
notifications_input = llm_result["notifications"]
|
||||
saved_notifications = _save_notifications(
|
||||
farm_uuid=farm_uuid,
|
||||
endpoint=FarmAlertNotification.ENDPOINT_TRACKER,
|
||||
@@ -387,11 +358,9 @@ def get_farm_alerts_tracker(*, farm_uuid: str, query: str | None = None) -> dict
|
||||
return {
|
||||
"farm_uuid": farm_uuid,
|
||||
"service_id": SERVICE_ID,
|
||||
"knowledge_base": KB_NAME,
|
||||
"tone_file": tone_file,
|
||||
"tracker": tracker,
|
||||
"headline": llm_result.get("headline") or "ارزیابی فعلی هشدارهای مزرعه",
|
||||
"overview": llm_result.get("overview") or "",
|
||||
"headline": llm_result["headline"],
|
||||
"overview": llm_result["overview"],
|
||||
"status_level": _normalize_level(llm_result.get("status_level")),
|
||||
"notifications": [_serialize_notification(item) for item in saved_notifications],
|
||||
"raw_llm_response": raw_response or None,
|
||||
@@ -410,15 +379,10 @@ def get_farm_alerts_timeline(*, farm_uuid: str, query: str | None = None) -> dic
|
||||
query=user_query,
|
||||
structured_context=structured_context,
|
||||
)
|
||||
if not llm_result:
|
||||
llm_result = _build_fallback_timeline_response(tracker)
|
||||
llm_result = _validate_timeline_response(llm_result)
|
||||
|
||||
timeline = llm_result.get("timeline")
|
||||
if not isinstance(timeline, list):
|
||||
timeline = _build_fallback_timeline_response(tracker).get("timeline", [])
|
||||
notifications_input = llm_result.get("notifications")
|
||||
if not isinstance(notifications_input, list):
|
||||
notifications_input = _build_fallback_notifications(tracker, FarmAlertNotification.ENDPOINT_TIMELINE)
|
||||
timeline = llm_result["timeline"]
|
||||
notifications_input = llm_result["notifications"]
|
||||
|
||||
saved_notifications = _save_notifications(
|
||||
farm_uuid=farm_uuid,
|
||||
@@ -428,11 +392,9 @@ def get_farm_alerts_timeline(*, farm_uuid: str, query: str | None = None) -> dic
|
||||
return {
|
||||
"farm_uuid": farm_uuid,
|
||||
"service_id": SERVICE_ID,
|
||||
"knowledge_base": KB_NAME,
|
||||
"tone_file": tone_file,
|
||||
"tracker": tracker,
|
||||
"headline": llm_result.get("headline") or "خط زمانی هشدارهای مزرعه",
|
||||
"overview": llm_result.get("overview") or "",
|
||||
"headline": llm_result["headline"],
|
||||
"overview": llm_result["overview"],
|
||||
"timeline": timeline,
|
||||
"notifications": [_serialize_notification(item) for item in saved_notifications],
|
||||
"raw_llm_response": raw_response or None,
|
||||
|
||||
@@ -218,11 +218,7 @@ class ReportingAndAiJourneyTests(IntegrationAPITestCase):
|
||||
data={"farm_uuid": str(self.farm_uuid)},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(water_stress_response.status_code, 200)
|
||||
self.assertEqual(
|
||||
water_stress_response.json()["data"]["sourceMetric"]["engine"],
|
||||
"sensor_fallback",
|
||||
)
|
||||
self.assertEqual(water_stress_response.status_code, 500)
|
||||
|
||||
def test_ai_assistant_and_recommendation_endpoints_use_farm_context(self) -> None:
|
||||
with ExitStack() as stack:
|
||||
|
||||
@@ -1,25 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from django.apps import apps
|
||||
|
||||
from farm_data.models import SensorData
|
||||
|
||||
|
||||
def build_water_stress_summary(sensor: Any) -> dict[str, Any]:
|
||||
moisture = float(getattr(sensor, "soil_moisture", None) or 0.0)
|
||||
water_stress = max(0, min(100, round(35 - (moisture / 2))))
|
||||
return {
|
||||
"waterStressIndex": water_stress,
|
||||
"level": "پایین" if water_stress <= 20 else "متوسط" if water_stress <= 45 else "بالا",
|
||||
"sourceMetric": {
|
||||
"soilMoisture": round(moisture, 2),
|
||||
"formula": "clamp(round(35 - (soil_moisture / 2)), 0, 100)",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class WaterStressService:
|
||||
def get_water_stress(self, *, farm_uuid: str, plant_name: str | None = None) -> dict[str, Any]:
|
||||
sensor = SensorData.objects.filter(farm_uuid=farm_uuid).first()
|
||||
@@ -32,10 +16,6 @@ class WaterStressService:
|
||||
plant_name=plant_name,
|
||||
)
|
||||
except Exception as exc:
|
||||
fallback = {
|
||||
"farm_uuid": str(sensor.farm_uuid),
|
||||
**build_water_stress_summary(sensor),
|
||||
}
|
||||
fallback["sourceMetric"]["engine"] = "sensor_fallback"
|
||||
fallback["sourceMetric"]["fallbackReason"] = str(exc)
|
||||
return fallback
|
||||
raise RuntimeError(
|
||||
f"Water stress simulation failed for farm {sensor.farm_uuid}: {exc}"
|
||||
) from exc
|
||||
|
||||
@@ -68,7 +68,7 @@ class WaterStressServiceTests(TestCase):
|
||||
|
||||
@patch("irrigation.indicators.apps.get_app_config")
|
||||
@patch("irrigation.indicators.SensorData.objects.filter")
|
||||
def test_service_falls_back_when_crop_simulation_fails(self, mock_filter, mock_get_app_config):
|
||||
def test_service_raises_when_crop_simulation_fails(self, mock_filter, mock_get_app_config):
|
||||
mock_filter.return_value.first.return_value = SimpleNamespace(
|
||||
farm_uuid="550e8400-e29b-41d4-a716-446655440000",
|
||||
soil_moisture=46.0,
|
||||
@@ -82,9 +82,7 @@ class WaterStressServiceTests(TestCase):
|
||||
get_water_stress_service=lambda: BrokenService()
|
||||
)
|
||||
|
||||
payload = WaterStressService().get_water_stress(
|
||||
farm_uuid="550e8400-e29b-41d4-a716-446655440000"
|
||||
)
|
||||
|
||||
self.assertEqual(payload["sourceMetric"]["engine"], "sensor_fallback")
|
||||
self.assertEqual(payload["sourceMetric"]["fallbackReason"], "simulation offline")
|
||||
with self.assertRaises(RuntimeError):
|
||||
WaterStressService().get_water_stress(
|
||||
farm_uuid="550e8400-e29b-41d4-a716-446655440000"
|
||||
)
|
||||
|
||||
+18
-228
@@ -35,226 +35,24 @@ def _get_optimizer():
|
||||
return apps.get_app_config("crop_simulation").get_recommendation_optimizer()
|
||||
|
||||
|
||||
def _unique_items(items: list[str]) -> list[str]:
|
||||
seen = set()
|
||||
output = []
|
||||
for item in items:
|
||||
normalized = (item or "").strip()
|
||||
if not normalized or normalized in seen:
|
||||
continue
|
||||
seen.add(normalized)
|
||||
output.append(normalized)
|
||||
return output
|
||||
|
||||
|
||||
def _find_section(sections: list[dict], section_type: str) -> dict | None:
|
||||
for section in sections:
|
||||
if isinstance(section, dict) and section.get("type") == section_type:
|
||||
return section
|
||||
return None
|
||||
|
||||
|
||||
def _field_sources(llm_section: dict, fallback_section: dict, merged_section: dict) -> dict[str, str]:
|
||||
sources: dict[str, str] = {}
|
||||
for key, value in merged_section.items():
|
||||
if key == "provenance":
|
||||
continue
|
||||
llm_value = llm_section.get(key)
|
||||
fallback_value = fallback_section.get(key)
|
||||
if key in llm_section and value == llm_value and value != fallback_value:
|
||||
sources[key] = "llm"
|
||||
elif key in fallback_section and value == fallback_value and value != llm_value:
|
||||
sources[key] = "fallback"
|
||||
elif key in llm_section and key in fallback_section and llm_value == fallback_value == value:
|
||||
sources[key] = "shared"
|
||||
elif key in llm_section and key in fallback_section:
|
||||
sources[key] = "merged"
|
||||
else:
|
||||
sources[key] = "fallback" if key in fallback_section else "llm"
|
||||
return sources
|
||||
|
||||
|
||||
def _attach_provenance(section_type: str, llm_section: dict, fallback_section: dict, merged_section: dict) -> dict:
|
||||
merged = dict(merged_section)
|
||||
field_sources = _field_sources(llm_section, fallback_section, merged)
|
||||
merged["provenance"] = {
|
||||
"sectionType": section_type,
|
||||
"llmProvided": bool(llm_section),
|
||||
"fallbackUsed": any(source != "llm" for source in field_sources.values()),
|
||||
"fieldSources": field_sources,
|
||||
}
|
||||
return merged
|
||||
|
||||
|
||||
def _fallback_with_provenance(fallback: dict, reason: str) -> dict:
|
||||
sections = []
|
||||
for section in fallback.get("sections", []):
|
||||
section_with_provenance = dict(section)
|
||||
section_with_provenance["provenance"] = {
|
||||
"sectionType": section.get("type"),
|
||||
"llmProvided": False,
|
||||
"fallbackUsed": True,
|
||||
"fieldSources": {
|
||||
key: "fallback"
|
||||
for key in section.keys()
|
||||
if key != "provenance"
|
||||
},
|
||||
}
|
||||
sections.append(section_with_provenance)
|
||||
return {
|
||||
"sections": sections,
|
||||
"mergeMetadata": {
|
||||
"source": "fallback_only",
|
||||
"reason": reason,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _build_fertilization_fallback(*, optimized_result: dict | None) -> dict:
|
||||
if optimized_result:
|
||||
recommended = optimized_result["recommended_strategy"]
|
||||
list_items = [
|
||||
f"دوز پیشنهادی: {recommended['amount_kg_per_ha']} کیلوگرم در هکتار",
|
||||
f"روش مصرف: {recommended['application_method']}",
|
||||
f"پنجره اجرا: {recommended['validity_period']}",
|
||||
]
|
||||
warning_text = "قبل از اختلاط یا محلول سازی، سازگاری کود با آب و شرایط مزرعه بررسی شود."
|
||||
return {
|
||||
"sections": [
|
||||
{
|
||||
"type": "recommendation",
|
||||
"title": "برنامه کودهی بهینه",
|
||||
"icon": "leaf",
|
||||
"content": (
|
||||
f"سناریوی {recommended['label']} برای این مزرعه مناسب تر ارزیابی شد."
|
||||
),
|
||||
"fertilizerType": recommended["fertilizer_type"],
|
||||
"amount": f"{recommended['amount_kg_per_ha']} کیلوگرم در هکتار",
|
||||
"applicationMethod": recommended["application_method"],
|
||||
"timing": recommended["timing"],
|
||||
"validityPeriod": recommended["validity_period"],
|
||||
"expandableExplanation": " ".join(recommended.get("reasoning", [])),
|
||||
},
|
||||
{
|
||||
"type": "list",
|
||||
"title": "نکات اجرایی و اختلاط",
|
||||
"icon": "list",
|
||||
"items": _unique_items(list_items),
|
||||
},
|
||||
{
|
||||
"type": "warning",
|
||||
"title": "هشدار کودهی",
|
||||
"icon": "alert-triangle",
|
||||
"content": warning_text,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return {
|
||||
"sections": [
|
||||
{
|
||||
"type": "recommendation",
|
||||
"title": "برنامه کودهی پیشنهادی",
|
||||
"icon": "leaf",
|
||||
"content": "پیشنهاد کودهی بر اساس داده های فعلی با قطعیت متوسط آماده شده است.",
|
||||
"fertilizerType": "کود کامل متعادل",
|
||||
"amount": "پس از پایش دوباره عناصر اصلی تعیین شود",
|
||||
"applicationMethod": "ترجیحا همراه آب آبیاری",
|
||||
"timing": "صبح زود",
|
||||
"validityPeriod": "معتبر برای 5 روز آینده",
|
||||
"expandableExplanation": "به دلیل محدود بودن داده های تغذیه ای، تصمیم نهایی باید با پایش مجدد تکمیل شود.",
|
||||
},
|
||||
{
|
||||
"type": "list",
|
||||
"title": "نکات اجرایی و اختلاط",
|
||||
"icon": "list",
|
||||
"items": [
|
||||
"قبل از مصرف، EC و pH محلول بررسی شود.",
|
||||
"در صورت مشاهده بارش موثر، زمان مصرف بازبینی شود.",
|
||||
],
|
||||
},
|
||||
{
|
||||
"type": "warning",
|
||||
"title": "هشدار کودهی",
|
||||
"icon": "alert-triangle",
|
||||
"content": "بدون بررسی دوباره مزرعه از مصرف سنگین کود خودداری شود.",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def _merge_fertilization_response(
|
||||
*,
|
||||
parsed_result: dict,
|
||||
optimized_result: dict | None,
|
||||
) -> dict:
|
||||
fallback = _build_fertilization_fallback(optimized_result=optimized_result)
|
||||
def _validate_fertilization_response(parsed_result: dict) -> dict:
|
||||
if not isinstance(parsed_result, dict):
|
||||
return _fallback_with_provenance(fallback, "invalid_llm_payload")
|
||||
raise ValueError("Fertilization recommendation response is not a JSON object.")
|
||||
|
||||
sections = parsed_result.get("sections")
|
||||
if not isinstance(sections, list):
|
||||
return _fallback_with_provenance(fallback, "missing_sections")
|
||||
if not isinstance(sections, list) or not sections:
|
||||
raise ValueError("Fertilization recommendation response is missing sections.")
|
||||
|
||||
recommendation = _find_section(sections, "recommendation") or {}
|
||||
list_section = _find_section(sections, "list") or {}
|
||||
warning_section = _find_section(sections, "warning") or {}
|
||||
for index, section in enumerate(sections):
|
||||
if not isinstance(section, dict):
|
||||
raise ValueError(f"Fertilization recommendation section {index} is invalid.")
|
||||
missing = [key for key in ("type", "title", "icon") if key not in section]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
f"Fertilization recommendation section {index} is missing fields: {', '.join(missing)}"
|
||||
)
|
||||
|
||||
fallback_recommendation = fallback["sections"][0]
|
||||
fallback_list = fallback["sections"][1]
|
||||
fallback_warning = fallback["sections"][2]
|
||||
|
||||
merged_recommendation = {**recommendation, **fallback_recommendation}
|
||||
merged_recommendation["content"] = recommendation.get("content") or fallback_recommendation["content"]
|
||||
merged_recommendation["title"] = recommendation.get("title") or fallback_recommendation["title"]
|
||||
merged_recommendation["expandableExplanation"] = (
|
||||
recommendation.get("expandableExplanation")
|
||||
or fallback_recommendation["expandableExplanation"]
|
||||
)
|
||||
|
||||
merged_list = {
|
||||
**fallback_list,
|
||||
**list_section,
|
||||
"items": _unique_items(
|
||||
list(list_section.get("items", [])) + list(fallback_list["items"])
|
||||
)[:5],
|
||||
}
|
||||
merged_warning = {
|
||||
**fallback_warning,
|
||||
**warning_section,
|
||||
"content": warning_section.get("content") or fallback_warning["content"],
|
||||
}
|
||||
|
||||
merged_recommendation = _attach_provenance(
|
||||
"recommendation",
|
||||
recommendation,
|
||||
fallback_recommendation,
|
||||
merged_recommendation,
|
||||
)
|
||||
merged_list = _attach_provenance(
|
||||
"list",
|
||||
list_section,
|
||||
fallback_list,
|
||||
merged_list,
|
||||
)
|
||||
merged_warning = _attach_provenance(
|
||||
"warning",
|
||||
warning_section,
|
||||
fallback_warning,
|
||||
merged_warning,
|
||||
)
|
||||
|
||||
return {
|
||||
"sections": [merged_recommendation, merged_list, merged_warning],
|
||||
"mergeMetadata": {
|
||||
"source": "llm_with_fallback_merge",
|
||||
"llmSectionsDetected": [section.get("type") for section in sections if isinstance(section, dict)],
|
||||
"fallbackSectionsApplied": [
|
||||
item["type"]
|
||||
for item in (fallback_recommendation, fallback_list, fallback_warning)
|
||||
],
|
||||
},
|
||||
}
|
||||
return parsed_result
|
||||
|
||||
|
||||
def get_fertilization_recommendation(
|
||||
@@ -382,15 +180,10 @@ def get_fertilization_recommendation(
|
||||
raw = response.choices[0].message.content.strip()
|
||||
except Exception as exc:
|
||||
logger.error("Fertilization recommendation error for %s: %s", resolved_farm_uuid, exc)
|
||||
result = _build_fertilization_fallback(optimized_result=optimized_result)
|
||||
result["error"] = f"خطا در دریافت توصیه: {exc}"
|
||||
result["raw_response"] = None
|
||||
_fail_audit_log(
|
||||
audit_log,
|
||||
str(exc),
|
||||
response_text=json.dumps(result, ensure_ascii=False, default=str),
|
||||
)
|
||||
return result
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
raise RuntimeError(
|
||||
f"Fertilization recommendation failed for farm {resolved_farm_uuid}."
|
||||
) from exc
|
||||
|
||||
try:
|
||||
cleaned = raw
|
||||
@@ -400,10 +193,7 @@ def get_fertilization_recommendation(
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
result = {}
|
||||
|
||||
result = _merge_fertilization_response(
|
||||
parsed_result=result,
|
||||
optimized_result=optimized_result,
|
||||
)
|
||||
result = _validate_fertilization_response(result)
|
||||
result["raw_response"] = raw
|
||||
result["simulation_optimizer"] = optimized_result
|
||||
_complete_audit_log(
|
||||
|
||||
+18
-250
@@ -38,244 +38,24 @@ def _get_optimizer():
|
||||
return apps.get_app_config("crop_simulation").get_recommendation_optimizer()
|
||||
|
||||
|
||||
def _unique_items(items: list[str]) -> list[str]:
|
||||
seen = set()
|
||||
output = []
|
||||
for item in items:
|
||||
normalized = (item or "").strip()
|
||||
if not normalized or normalized in seen:
|
||||
continue
|
||||
seen.add(normalized)
|
||||
output.append(normalized)
|
||||
return output
|
||||
|
||||
|
||||
def _find_section(sections: list[dict], section_type: str) -> dict | None:
|
||||
for section in sections:
|
||||
if isinstance(section, dict) and section.get("type") == section_type:
|
||||
return section
|
||||
return None
|
||||
|
||||
|
||||
def _field_sources(llm_section: dict, fallback_section: dict, merged_section: dict) -> dict[str, str]:
|
||||
sources: dict[str, str] = {}
|
||||
for key, value in merged_section.items():
|
||||
if key == "provenance":
|
||||
continue
|
||||
llm_value = llm_section.get(key)
|
||||
fallback_value = fallback_section.get(key)
|
||||
if key in llm_section and value == llm_value and value != fallback_value:
|
||||
sources[key] = "llm"
|
||||
elif key in fallback_section and value == fallback_value and value != llm_value:
|
||||
sources[key] = "fallback"
|
||||
elif key in llm_section and key in fallback_section and llm_value == fallback_value == value:
|
||||
sources[key] = "shared"
|
||||
elif key in llm_section and key in fallback_section:
|
||||
sources[key] = "merged"
|
||||
else:
|
||||
sources[key] = "fallback" if key in fallback_section else "llm"
|
||||
return sources
|
||||
|
||||
|
||||
def _attach_provenance(section_type: str, llm_section: dict, fallback_section: dict, merged_section: dict) -> dict:
|
||||
merged = dict(merged_section)
|
||||
field_sources = _field_sources(llm_section, fallback_section, merged)
|
||||
merged["provenance"] = {
|
||||
"sectionType": section_type,
|
||||
"llmProvided": bool(llm_section),
|
||||
"fallbackUsed": any(source != "llm" for source in field_sources.values()),
|
||||
"fieldSources": field_sources,
|
||||
}
|
||||
return merged
|
||||
|
||||
|
||||
def _fallback_with_provenance(fallback: dict, reason: str) -> dict:
|
||||
sections = []
|
||||
for section in fallback.get("sections", []):
|
||||
section_with_provenance = dict(section)
|
||||
section_with_provenance["provenance"] = {
|
||||
"sectionType": section.get("type"),
|
||||
"llmProvided": False,
|
||||
"fallbackUsed": True,
|
||||
"fieldSources": {
|
||||
key: "fallback"
|
||||
for key in section.keys()
|
||||
if key != "provenance"
|
||||
},
|
||||
}
|
||||
sections.append(section_with_provenance)
|
||||
return {
|
||||
"sections": sections,
|
||||
"mergeMetadata": {
|
||||
"source": "fallback_only",
|
||||
"reason": reason,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _build_irrigation_fallback(
|
||||
*,
|
||||
optimized_result: dict | None,
|
||||
daily_water_needs: list[dict],
|
||||
) -> dict:
|
||||
if optimized_result:
|
||||
recommended = optimized_result["recommended_strategy"]
|
||||
content = (
|
||||
f"{recommended['events']} نوبت آبیاری با حدود "
|
||||
f"{recommended['amount_per_event_mm']} میلی متر در هر نوبت اجرا شود."
|
||||
)
|
||||
list_items = [
|
||||
f"در بازه اعتبار حدود {recommended['total_irrigation_mm']} میلی متر آب توزیع شود.",
|
||||
f"نوبت های پیشنهادی: {', '.join(recommended['event_dates']) or 'بر اساس پایش روزانه مزرعه'}",
|
||||
f"رطوبت خاک نزدیک {recommended['moisture_target_percent']} درصد نگه داشته شود.",
|
||||
]
|
||||
warning = optimized_result.get("alternatives", [])
|
||||
warning_text = (
|
||||
f"اگر شرایط مزرعه تغییر کرد، سناریوی جایگزین {warning[0]['label']} هم قابل بررسی است."
|
||||
if warning
|
||||
else "در صورت تغییر ناگهانی بارش یا باد، برنامه را دوباره ارزیابی کنید."
|
||||
)
|
||||
explanation = " ".join(recommended.get("reasoning", []))
|
||||
return {
|
||||
"sections": [
|
||||
{
|
||||
"type": "recommendation",
|
||||
"title": "برنامه آبیاری بهینه",
|
||||
"icon": "droplet",
|
||||
"content": content,
|
||||
"frequency": f"{recommended['events']} نوبت در بازه اعتبار",
|
||||
"amount": (
|
||||
f"{recommended['amount_per_event_mm']} میلی متر در هر نوبت "
|
||||
f"(جمع کل {recommended['total_irrigation_mm']} میلی متر)"
|
||||
),
|
||||
"timing": recommended["timing"],
|
||||
"validityPeriod": recommended["validity_period"],
|
||||
"expandableExplanation": explanation,
|
||||
},
|
||||
{
|
||||
"type": "list",
|
||||
"title": "اقدامات اجرایی",
|
||||
"icon": "list",
|
||||
"items": _unique_items(list_items),
|
||||
},
|
||||
{
|
||||
"type": "warning",
|
||||
"title": "هشدار آبیاری",
|
||||
"icon": "alert-triangle",
|
||||
"content": warning_text,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
total_mm = round(sum(float(item.get("gross_irrigation_mm", 0.0) or 0.0) for item in daily_water_needs), 2)
|
||||
return {
|
||||
"sections": [
|
||||
{
|
||||
"type": "recommendation",
|
||||
"title": "برنامه آبیاری پیشنهادی",
|
||||
"icon": "droplet",
|
||||
"content": "پایش رطوبت خاک ادامه پیدا کند و برنامه آبیاری بر اساس نیاز روزانه تنظیم شود.",
|
||||
"frequency": "بر اساس پایش روزانه",
|
||||
"amount": f"جمع نیاز تخمینی این بازه حدود {total_mm} میلی متر است.",
|
||||
"timing": "اوایل صبح یا نزدیک غروب",
|
||||
"validityPeriod": "معتبر برای 3 روز آینده",
|
||||
"expandableExplanation": "به دلیل محدود بودن داده ها، توصیه با اتکا به نیاز آبی روزانه ساخته شده است.",
|
||||
},
|
||||
{
|
||||
"type": "list",
|
||||
"title": "اقدامات اجرایی",
|
||||
"icon": "list",
|
||||
"items": [
|
||||
"قبل از هر نوبت آبیاری رطوبت خاک سطحی را دوباره بررسی کنید.",
|
||||
"اگر بارش موثر رخ داد، نوبت بعدی را به تعویق بیندازید.",
|
||||
],
|
||||
},
|
||||
{
|
||||
"type": "warning",
|
||||
"title": "هشدار آبیاری",
|
||||
"icon": "alert-triangle",
|
||||
"content": "با تغییر دما یا بارش پیش بینی شده، برنامه فعلی باید بازبینی شود.",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def _merge_irrigation_response(
|
||||
*,
|
||||
parsed_result: dict,
|
||||
optimized_result: dict | None,
|
||||
daily_water_needs: list[dict],
|
||||
) -> dict:
|
||||
fallback = _build_irrigation_fallback(
|
||||
optimized_result=optimized_result,
|
||||
daily_water_needs=daily_water_needs,
|
||||
)
|
||||
def _validate_irrigation_response(parsed_result: dict) -> dict:
|
||||
if not isinstance(parsed_result, dict):
|
||||
return _fallback_with_provenance(fallback, "invalid_llm_payload")
|
||||
raise ValueError("Irrigation recommendation response is not a JSON object.")
|
||||
|
||||
sections = parsed_result.get("sections")
|
||||
if not isinstance(sections, list):
|
||||
return _fallback_with_provenance(fallback, "missing_sections")
|
||||
if not isinstance(sections, list) or not sections:
|
||||
raise ValueError("Irrigation recommendation response is missing sections.")
|
||||
|
||||
recommendation = _find_section(sections, "recommendation") or {}
|
||||
list_section = _find_section(sections, "list") or {}
|
||||
warning_section = _find_section(sections, "warning") or {}
|
||||
for index, section in enumerate(sections):
|
||||
if not isinstance(section, dict):
|
||||
raise ValueError(f"Irrigation recommendation section {index} is invalid.")
|
||||
missing = [key for key in ("type", "title", "icon") if key not in section]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
f"Irrigation recommendation section {index} is missing fields: {', '.join(missing)}"
|
||||
)
|
||||
|
||||
fallback_recommendation = fallback["sections"][0]
|
||||
fallback_list = fallback["sections"][1]
|
||||
fallback_warning = fallback["sections"][2]
|
||||
|
||||
merged_recommendation = {**recommendation, **fallback_recommendation}
|
||||
merged_recommendation["expandableExplanation"] = (
|
||||
recommendation.get("expandableExplanation")
|
||||
or fallback_recommendation["expandableExplanation"]
|
||||
)
|
||||
merged_recommendation["content"] = recommendation.get("content") or fallback_recommendation["content"]
|
||||
merged_recommendation["title"] = recommendation.get("title") or fallback_recommendation["title"]
|
||||
|
||||
merged_list = {
|
||||
**fallback_list,
|
||||
**list_section,
|
||||
"items": _unique_items(
|
||||
list(list_section.get("items", [])) + list(fallback_list["items"])
|
||||
)[:5],
|
||||
}
|
||||
merged_warning = {
|
||||
**fallback_warning,
|
||||
**warning_section,
|
||||
"content": warning_section.get("content") or fallback_warning["content"],
|
||||
}
|
||||
|
||||
merged_recommendation = _attach_provenance(
|
||||
"recommendation",
|
||||
recommendation,
|
||||
fallback_recommendation,
|
||||
merged_recommendation,
|
||||
)
|
||||
merged_list = _attach_provenance(
|
||||
"list",
|
||||
list_section,
|
||||
fallback_list,
|
||||
merged_list,
|
||||
)
|
||||
merged_warning = _attach_provenance(
|
||||
"warning",
|
||||
warning_section,
|
||||
fallback_warning,
|
||||
merged_warning,
|
||||
)
|
||||
|
||||
return {
|
||||
"sections": [merged_recommendation, merged_list, merged_warning],
|
||||
"mergeMetadata": {
|
||||
"source": "llm_with_fallback_merge",
|
||||
"llmSectionsDetected": [section.get("type") for section in sections if isinstance(section, dict)],
|
||||
"fallbackSectionsApplied": [
|
||||
item["type"]
|
||||
for item in (fallback_recommendation, fallback_list, fallback_warning)
|
||||
],
|
||||
},
|
||||
}
|
||||
return parsed_result
|
||||
|
||||
|
||||
def _resolve_irrigation_method(
|
||||
@@ -466,18 +246,10 @@ def get_irrigation_recommendation(
|
||||
raw = response.choices[0].message.content.strip()
|
||||
except Exception as exc:
|
||||
logger.error("Irrigation recommendation error for %s: %s", resolved_farm_uuid, exc)
|
||||
result = _build_irrigation_fallback(
|
||||
optimized_result=optimized_result,
|
||||
daily_water_needs=daily_water_needs,
|
||||
)
|
||||
result["error"] = f"خطا در دریافت توصیه: {exc}"
|
||||
result["raw_response"] = None
|
||||
_fail_audit_log(
|
||||
audit_log,
|
||||
str(exc),
|
||||
response_text=json.dumps(result, ensure_ascii=False, default=str),
|
||||
)
|
||||
return result
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
raise RuntimeError(
|
||||
f"Irrigation recommendation failed for farm {resolved_farm_uuid}."
|
||||
) from exc
|
||||
|
||||
try:
|
||||
cleaned = raw
|
||||
@@ -487,11 +259,7 @@ def get_irrigation_recommendation(
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
result = {}
|
||||
|
||||
result = _merge_irrigation_response(
|
||||
parsed_result=result,
|
||||
optimized_result=optimized_result,
|
||||
daily_water_needs=daily_water_needs,
|
||||
)
|
||||
result = _validate_irrigation_response(result)
|
||||
result["raw_response"] = raw
|
||||
result["water_balance"] = {
|
||||
"daily": daily_water_needs,
|
||||
|
||||
@@ -137,7 +137,7 @@ def _risk_level(score: float) -> str:
|
||||
return "low"
|
||||
|
||||
|
||||
def _build_risk_fallback(farm_details: dict[str, Any], plant_name: str | None, growth_stage: str | None) -> dict[str, Any]:
|
||||
def _build_risk_context(farm_details: dict[str, Any], plant_name: str | None, growth_stage: str | None) -> dict[str, Any]:
|
||||
risk = _weather_risk_summary(farm_details)
|
||||
disease_level = _risk_level(risk["fungal_score"])
|
||||
pest_level = _risk_level(risk["pest_score"])
|
||||
@@ -199,24 +199,44 @@ def _build_risk_fallback(farm_details: dict[str, Any], plant_name: str | None, g
|
||||
}
|
||||
|
||||
|
||||
def _build_detection_fallback(images: list[dict[str, str]], plant_name: str | None) -> dict[str, Any]:
|
||||
return {
|
||||
"has_issue": False,
|
||||
"category": "unknown",
|
||||
"confidence": 0.2,
|
||||
"severity": "low",
|
||||
"summary": "تحلیل خودکار تصویر انجام نشد یا برای نتیجه قطعی داده کافی نبود.",
|
||||
"detected_signs": [],
|
||||
"possible_causes": ["کیفیت یا زاویه تصویر برای تشخیص کافی نیست"],
|
||||
"immediate_actions": [
|
||||
"یک تصویر نزدیک تر از برگ و ساقه ارسال شود.",
|
||||
"در صورت مشاهده گسترش علائم، بازدید میدانی انجام شود.",
|
||||
],
|
||||
"reasoning": [
|
||||
f"تعداد تصاویر دریافتی: {len(images)}",
|
||||
f"نام گیاه: {plant_name or 'نامشخص'}",
|
||||
],
|
||||
def _validate_detection_result(parsed: dict[str, Any]) -> dict[str, Any]:
|
||||
required_keys = {
|
||||
"has_issue",
|
||||
"category",
|
||||
"confidence",
|
||||
"severity",
|
||||
"summary",
|
||||
"detected_signs",
|
||||
"possible_causes",
|
||||
"immediate_actions",
|
||||
"reasoning",
|
||||
}
|
||||
missing = [key for key in required_keys if key not in parsed]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Pest disease detection response is missing required fields: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
return parsed
|
||||
|
||||
|
||||
def _validate_risk_result(parsed: dict[str, Any]) -> dict[str, Any]:
|
||||
required_keys = {
|
||||
"summary",
|
||||
"forecast_window",
|
||||
"overall_risk",
|
||||
"disease_risk",
|
||||
"pest_risk",
|
||||
"key_drivers",
|
||||
"recommended_actions",
|
||||
}
|
||||
missing = [key for key in required_keys if key not in parsed]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Pest disease risk response is missing required fields: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
return parsed
|
||||
|
||||
|
||||
def _build_detection_messages(
|
||||
@@ -320,29 +340,11 @@ def get_pest_disease_detection(
|
||||
_complete_audit_log(audit_log, raw)
|
||||
except Exception as exc:
|
||||
logger.error("Pest disease detection failed for %s: %s", farm_uuid, exc)
|
||||
fallback = _build_detection_fallback(normalized_images, resolved_plant_name)
|
||||
_fail_audit_log(audit_log, str(exc), json.dumps(fallback, ensure_ascii=False))
|
||||
return {
|
||||
**fallback,
|
||||
"farm_uuid": farm_uuid,
|
||||
"knowledge_base": KB_NAME,
|
||||
"tone_file": service.tone_file,
|
||||
"raw_response": None,
|
||||
}
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
raise RuntimeError(f"Pest disease detection failed for farm {farm_uuid}.") from exc
|
||||
|
||||
if not parsed:
|
||||
parsed = _build_detection_fallback(normalized_images, resolved_plant_name)
|
||||
parsed.setdefault("has_issue", parsed.get("category") not in {"no_issue", "unknown"})
|
||||
parsed.setdefault("category", "unknown")
|
||||
parsed.setdefault("confidence", 0.4)
|
||||
parsed.setdefault("severity", "low")
|
||||
parsed.setdefault("detected_signs", [])
|
||||
parsed.setdefault("possible_causes", [])
|
||||
parsed.setdefault("immediate_actions", [])
|
||||
parsed.setdefault("reasoning", [])
|
||||
parsed = _validate_detection_result(parsed)
|
||||
parsed["farm_uuid"] = farm_uuid
|
||||
parsed["knowledge_base"] = KB_NAME
|
||||
parsed["tone_file"] = service.tone_file
|
||||
parsed["raw_response"] = raw
|
||||
return parsed
|
||||
|
||||
@@ -358,7 +360,7 @@ def get_pest_disease_risk(
|
||||
service, client, model = _build_service_client(cfg)
|
||||
farm_details = _load_farm_or_error(farm_uuid)
|
||||
resolved_plant_name = plant_name or (farm_details.get("plants") or [{}])[0].get("name")
|
||||
fallback = _build_risk_fallback(farm_details, resolved_plant_name, growth_stage)
|
||||
risk_context = _build_risk_context(farm_details, resolved_plant_name, growth_stage)
|
||||
user_query = query or "ریسک آفات و بیماری این مزرعه را برای چند روز آینده پیش بینی کن."
|
||||
plant_text = build_plant_text(resolved_plant_name, growth_stage or "") if resolved_plant_name else ""
|
||||
rag_context = build_rag_context(
|
||||
@@ -374,7 +376,7 @@ def get_pest_disease_risk(
|
||||
cfg=cfg,
|
||||
query=user_query,
|
||||
rag_context=rag_context,
|
||||
structured_context=fallback,
|
||||
structured_context=risk_context,
|
||||
plant_text=plant_text or "",
|
||||
)
|
||||
audit_log = _create_audit_log(
|
||||
@@ -392,24 +394,10 @@ def get_pest_disease_risk(
|
||||
_complete_audit_log(audit_log, raw)
|
||||
except Exception as exc:
|
||||
logger.error("Pest disease risk prediction failed for %s: %s", farm_uuid, exc)
|
||||
_fail_audit_log(audit_log, str(exc), json.dumps(fallback, ensure_ascii=False))
|
||||
fallback["farm_uuid"] = farm_uuid
|
||||
fallback["knowledge_base"] = KB_NAME
|
||||
fallback["tone_file"] = service.tone_file
|
||||
fallback["raw_response"] = None
|
||||
return fallback
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
raise RuntimeError(f"Pest disease risk prediction failed for farm {farm_uuid}.") from exc
|
||||
|
||||
if not parsed:
|
||||
parsed = fallback
|
||||
parsed.setdefault("summary", fallback["summary"])
|
||||
parsed.setdefault("forecast_window", fallback["forecast_window"])
|
||||
parsed.setdefault("overall_risk", fallback["overall_risk"])
|
||||
parsed.setdefault("disease_risk", fallback["disease_risk"])
|
||||
parsed.setdefault("pest_risk", fallback["pest_risk"])
|
||||
parsed.setdefault("key_drivers", fallback["key_drivers"])
|
||||
parsed.setdefault("recommended_actions", fallback["recommended_actions"])
|
||||
parsed = _validate_risk_result(parsed)
|
||||
parsed["farm_uuid"] = farm_uuid
|
||||
parsed["knowledge_base"] = KB_NAME
|
||||
parsed["tone_file"] = service.tone_file
|
||||
parsed["raw_response"] = raw
|
||||
return parsed
|
||||
|
||||
@@ -69,42 +69,22 @@ def _build_service_client(cfg: RAGConfig):
|
||||
return service, client, service.llm.model
|
||||
|
||||
|
||||
def _fallback_from_payload(anomaly_payload: dict[str, Any]) -> dict[str, Any]:
|
||||
interpretation = anomaly_payload.get("interpretation") or {}
|
||||
anomalies = anomaly_payload.get("anomalies") or []
|
||||
top_anomaly = anomalies[0] if anomalies else None
|
||||
|
||||
if top_anomaly is None:
|
||||
return {
|
||||
"summary": "در داده های اخیر ناهنجاری معناداری دیده نشد.",
|
||||
"explanation": interpretation.get("explanation")
|
||||
or "داده های فعلی با الگوی معمول مزرعه سازگار هستند و مورد غیرعادی برجسته ای دیده نمی شود.",
|
||||
"likely_cause": interpretation.get("likely_cause")
|
||||
or "شرایط فعلی مزرعه پایدار است یا داده کافی برای تشخیص رخداد غیرعادی وجود ندارد.",
|
||||
"recommended_action": interpretation.get("recommended_action")
|
||||
or "پایش عادی ادامه یابد و روندها در بازه بعدی دوباره بررسی شوند.",
|
||||
"monitoring_priority": "low",
|
||||
"confidence": 0.55,
|
||||
}
|
||||
|
||||
severity = str(top_anomaly.get("severity") or "medium")
|
||||
priority_map = {
|
||||
"low": "medium",
|
||||
"medium": "high",
|
||||
"high": "urgent",
|
||||
"critical": "urgent",
|
||||
}
|
||||
return {
|
||||
"summary": f"ناهنجاري در شاخص {top_anomaly.get('label', 'نامشخص')} شناسايي شد.",
|
||||
"explanation": interpretation.get("explanation")
|
||||
or f"مقدار {top_anomaly.get('label', 'اين شاخص')} از الگوي آماري معمول مزرعه فاصله گرفته است.",
|
||||
"likely_cause": interpretation.get("likely_cause")
|
||||
or "اين الگو مي تواند ناشي از تغيير شرايط محيطي، آبياري، شوري يا خطاي اندازه گيري سنسور باشد.",
|
||||
"recommended_action": interpretation.get("recommended_action")
|
||||
or "روند اين شاخص و شرايط مزرعه در کوتاه مدت بازبيني و در صورت تداوم، اقدام اصلاحي انجام شود.",
|
||||
"monitoring_priority": priority_map.get(severity, "high"),
|
||||
"confidence": 0.7 if severity in {"high", "critical"} else 0.6,
|
||||
def _validate_anomaly_insight(parsed: dict[str, Any]) -> dict[str, Any]:
|
||||
required_keys = {
|
||||
"summary",
|
||||
"explanation",
|
||||
"likely_cause",
|
||||
"recommended_action",
|
||||
"monitoring_priority",
|
||||
"confidence",
|
||||
}
|
||||
missing = [key for key in required_keys if key not in parsed]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Soil anomaly insight response is missing required fields: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
return parsed
|
||||
|
||||
|
||||
def _build_messages(
|
||||
@@ -143,12 +123,10 @@ def get_soil_anomaly_insight(
|
||||
cfg = load_rag_config()
|
||||
service, client, model = _build_service_client(cfg)
|
||||
farm_details = _load_farm_or_error(farm_uuid)
|
||||
fallback = _fallback_from_payload(anomaly_payload)
|
||||
user_query = query or "ناهنجاري هاي داده هاي خاک اين مزرعه را تفسير کن و اقدام مناسب پيشنهاد بده."
|
||||
structured_context = {
|
||||
"farm_uuid": farm_uuid,
|
||||
"anomaly_payload": anomaly_payload,
|
||||
"fallback_interpretation": fallback,
|
||||
}
|
||||
rag_context = build_rag_context(
|
||||
query=user_query,
|
||||
@@ -180,25 +158,10 @@ def get_soil_anomaly_insight(
|
||||
_complete_audit_log(audit_log, raw)
|
||||
except Exception as exc:
|
||||
logger.error("Soil anomaly insight failed for %s: %s", farm_uuid, exc)
|
||||
_fail_audit_log(audit_log, str(exc), json.dumps(fallback, ensure_ascii=False))
|
||||
return {
|
||||
**fallback,
|
||||
"farm_uuid": farm_uuid,
|
||||
"knowledge_base": KB_NAME,
|
||||
"tone_file": service.tone_file,
|
||||
"raw_response": None,
|
||||
}
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
raise RuntimeError(f"Soil anomaly insight failed for farm {farm_uuid}.") from exc
|
||||
|
||||
if not parsed:
|
||||
parsed = fallback
|
||||
parsed.setdefault("summary", fallback["summary"])
|
||||
parsed.setdefault("explanation", fallback["explanation"])
|
||||
parsed.setdefault("likely_cause", fallback["likely_cause"])
|
||||
parsed.setdefault("recommended_action", fallback["recommended_action"])
|
||||
parsed.setdefault("monitoring_priority", fallback["monitoring_priority"])
|
||||
parsed.setdefault("confidence", fallback["confidence"])
|
||||
parsed = _validate_anomaly_insight(parsed)
|
||||
parsed["farm_uuid"] = farm_uuid
|
||||
parsed["knowledge_base"] = KB_NAME
|
||||
parsed["tone_file"] = service.tone_file
|
||||
parsed["raw_response"] = raw
|
||||
return parsed
|
||||
|
||||
@@ -68,32 +68,21 @@ def _build_service_client(cfg: RAGConfig):
|
||||
return service, client, service.llm.model
|
||||
|
||||
|
||||
def _fallback_from_payload(prediction_payload: dict[str, Any]) -> dict[str, Any]:
|
||||
total = float(prediction_payload.get("totalNext7Days") or 0.0)
|
||||
daily = prediction_payload.get("dailyBreakdown") or []
|
||||
peak_day = max(daily, key=lambda item: float(item.get("gross_irrigation_mm", 0.0) or 0.0), default=None)
|
||||
if total <= 0:
|
||||
return {
|
||||
"summary": "براي چند روز آينده نياز آبي معناداري برآورد نشد.",
|
||||
"irrigation_outlook": "بارش موثر يا شرايط فعلي باعث شده نياز خالص آبياري پايين باشد.",
|
||||
"recommended_action": "پايش رطوبت خاک ادامه يابد و قبل از هر آبياري جديد forecast دوباره بررسي شود.",
|
||||
"risk_note": "اگر forecast تغيير کند يا بارش موثر رخ ندهد، برآورد بايد به روز شود.",
|
||||
"confidence": 0.58,
|
||||
}
|
||||
|
||||
peak_text = ""
|
||||
if peak_day:
|
||||
peak_text = (
|
||||
f" بيشترين فشار آبي در {peak_day.get('forecast_date')} "
|
||||
f"با حدود {peak_day.get('gross_irrigation_mm')} ميلي متر برآورد شده است."
|
||||
)
|
||||
return {
|
||||
"summary": f"جمع نياز آبي 7 روز آينده حدود {round(total, 2)} ميلي متر برآورد شده است.",
|
||||
"irrigation_outlook": "الگوي آبياري بايد در چند روز آينده بر اساس نياز روزانه و بارش موثر تنظيم شود." + peak_text,
|
||||
"recommended_action": "برنامه آبياري کوتاه مدت بر اساس روزهاي اوج نياز تنظيم و صبح زود يا نزديک غروب اجرا شود.",
|
||||
"risk_note": "در صورت تغيير دما، باد يا بارش، مقادير gross irrigation ممکن است تغيير کنند.",
|
||||
"confidence": 0.72,
|
||||
def _validate_prediction_insight(parsed: dict[str, Any]) -> dict[str, Any]:
|
||||
required_keys = {
|
||||
"summary",
|
||||
"irrigation_outlook",
|
||||
"recommended_action",
|
||||
"risk_note",
|
||||
"confidence",
|
||||
}
|
||||
missing = [key for key in required_keys if key not in parsed]
|
||||
if missing:
|
||||
raise ValueError(
|
||||
"Water need prediction insight response is missing required fields: "
|
||||
+ ", ".join(missing)
|
||||
)
|
||||
return parsed
|
||||
|
||||
|
||||
def _build_messages(
|
||||
@@ -132,12 +121,10 @@ def get_water_need_prediction_insight(
|
||||
cfg = load_rag_config()
|
||||
service, client, model = _build_service_client(cfg)
|
||||
farm_details = _load_farm_or_error(farm_uuid)
|
||||
fallback = _fallback_from_payload(prediction_payload)
|
||||
user_query = query or "نياز آبي کوتاه مدت اين مزرعه را تفسير کن و اقدام عملياتي پيشنهاد بده."
|
||||
structured_context = {
|
||||
"farm_uuid": farm_uuid,
|
||||
"prediction_payload": prediction_payload,
|
||||
"fallback_summary": fallback,
|
||||
}
|
||||
rag_context = build_rag_context(
|
||||
query=user_query,
|
||||
@@ -169,24 +156,10 @@ def get_water_need_prediction_insight(
|
||||
_complete_audit_log(audit_log, raw)
|
||||
except Exception as exc:
|
||||
logger.error("Water need prediction insight failed for %s: %s", farm_uuid, exc)
|
||||
_fail_audit_log(audit_log, str(exc), json.dumps(fallback, ensure_ascii=False))
|
||||
return {
|
||||
**fallback,
|
||||
"farm_uuid": farm_uuid,
|
||||
"knowledge_base": KB_NAME,
|
||||
"tone_file": service.tone_file,
|
||||
"raw_response": None,
|
||||
}
|
||||
_fail_audit_log(audit_log, str(exc))
|
||||
raise RuntimeError(f"Water need prediction insight failed for farm {farm_uuid}.") from exc
|
||||
|
||||
if not parsed:
|
||||
parsed = fallback
|
||||
parsed.setdefault("summary", fallback["summary"])
|
||||
parsed.setdefault("irrigation_outlook", fallback["irrigation_outlook"])
|
||||
parsed.setdefault("recommended_action", fallback["recommended_action"])
|
||||
parsed.setdefault("risk_note", fallback["risk_note"])
|
||||
parsed.setdefault("confidence", fallback["confidence"])
|
||||
parsed = _validate_prediction_insight(parsed)
|
||||
parsed["farm_uuid"] = farm_uuid
|
||||
parsed["knowledge_base"] = KB_NAME
|
||||
parsed["tone_file"] = service.tone_file
|
||||
parsed["raw_response"] = raw
|
||||
return parsed
|
||||
|
||||
@@ -126,7 +126,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
self.build_irrigation_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "list", "title": "custom", "icon": "list", "items": ["مورد سفارشی"]}]}'))]
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "recommendation", "title": "برنامه", "icon": "droplet", "content": "custom"}, {"type": "list", "title": "custom", "icon": "list", "items": ["مورد سفارشی"]}, {"type": "warning", "title": "هشدار", "icon": "alert-triangle", "content": "بررسی شود"}]}'))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_irrigation_recommendation(
|
||||
@@ -135,7 +135,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
)
|
||||
|
||||
self.assertEqual(result["sections"][0]["type"], "recommendation")
|
||||
self.assertEqual(result["sections"][0]["amount"], "8.0 میلی متر در هر نوبت (جمع کل 24.0 میلی متر)")
|
||||
self.assertEqual(result["sections"][0]["content"], "custom")
|
||||
mock_build_rag_context.assert_called_once()
|
||||
mock_build_plant_text.assert_called_once_with("گوجهفرنگی", "میوهدهی")
|
||||
mock_build_irrigation_method_text.assert_called_once_with("آبیاری قطرهای")
|
||||
@@ -144,10 +144,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
"آبیاری قطرهای",
|
||||
)
|
||||
self.assertEqual(result["simulation_optimizer"]["engine"], "crop_simulation_heuristic")
|
||||
self.assertEqual(result["mergeMetadata"]["source"], "llm_with_fallback_merge")
|
||||
self.assertEqual(result["sections"][1]["provenance"]["sectionType"], "list")
|
||||
self.assertEqual(result["sections"][1]["provenance"]["fieldSources"]["title"], "llm")
|
||||
self.assertEqual(result["sections"][0]["provenance"]["fieldSources"]["amount"], "fallback")
|
||||
self.assertEqual(result["sections"][1]["items"], ["مورد سفارشی"])
|
||||
|
||||
@patch("rag.services.irrigation.calculate_forecast_water_needs", return_value=[])
|
||||
@patch("rag.services.irrigation.resolve_kc", return_value=0.9)
|
||||
@@ -176,7 +173,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
self.build_irrigation_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "recommendation", "title": "test", "icon": "droplet", "content": "custom"}]}'))]
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "recommendation", "title": "test", "icon": "droplet", "content": "custom"}, {"type": "list", "title": "گام ها", "icon": "list", "items": ["مورد 1"]}, {"type": "warning", "title": "هشدار", "icon": "alert-triangle", "content": "بررسی شود"}]}'))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_irrigation_recommendation(
|
||||
@@ -189,7 +186,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
self.assertEqual(self.farm.irrigation_method_id, sprinkler.id)
|
||||
self.assertEqual(result["selected_irrigation_method"]["id"], sprinkler.id)
|
||||
mock_build_irrigation_method_text.assert_called_once_with("بارانی")
|
||||
self.assertEqual(result["sections"][0]["provenance"]["fieldSources"]["content"], "llm")
|
||||
self.assertEqual(result["sections"][0]["content"], "custom")
|
||||
|
||||
@patch("rag.services.fertilization.build_plant_text", return_value="plant text")
|
||||
@patch("rag.services.fertilization.build_rag_context", return_value="")
|
||||
@@ -206,7 +203,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
self.build_fertilization_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "warning", "title": "هشدار", "icon": "alert-triangle", "content": "از اختلاط نامناسب خودداری شود."}]}'))]
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "recommendation", "title": "برنامه", "icon": "leaf", "content": "مصرف انجام شود", "fertilizerType": "20-20-20", "amount": "45 کیلوگرم در هکتار", "applicationMethod": "کودآبیاری", "timing": "صبح", "validityPeriod": "5 روز", "expandableExplanation": "توضیح"}, {"type": "list", "title": "هشدار", "icon": "list", "items": ["مورد 1"]}, {"type": "warning", "title": "هشدار", "icon": "alert-triangle", "content": "از اختلاط نامناسب خودداری شود."}]}'))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_fertilization_recommendation(
|
||||
@@ -217,14 +214,13 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
self.assertEqual(result["sections"][0]["fertilizerType"], "20-20-20")
|
||||
mock_build_plant_text.assert_called_once_with("گوجهفرنگی", "رویشی")
|
||||
self.assertEqual(result["simulation_optimizer"]["engine"], "crop_simulation_heuristic")
|
||||
self.assertEqual(result["sections"][2]["provenance"]["fieldSources"]["content"], "llm")
|
||||
self.assertEqual(result["sections"][0]["provenance"]["fieldSources"]["fertilizerType"], "fallback")
|
||||
self.assertEqual(result["sections"][2]["content"], "از اختلاط نامناسب خودداری شود.")
|
||||
|
||||
@patch("rag.services.fertilization.build_plant_text", return_value="plant text")
|
||||
@patch("rag.services.fertilization.build_rag_context", return_value="")
|
||||
@patch("rag.services.fertilization._get_optimizer")
|
||||
@patch("rag.services.fertilization.get_chat_client")
|
||||
def test_fertilization_recommendation_falls_back_to_optimizer_json_when_llm_returns_invalid_payload(
|
||||
def test_fertilization_recommendation_raises_when_llm_returns_invalid_payload(
|
||||
self,
|
||||
mock_get_chat_client,
|
||||
mock_get_optimizer,
|
||||
@@ -238,12 +234,8 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
mock_response.choices = [Mock(message=Mock(content="not-json"))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_fertilization_recommendation(
|
||||
farm_uuid=str(self.farm_uuid),
|
||||
growth_stage="رویشی",
|
||||
)
|
||||
|
||||
self.assertEqual(result["sections"][0]["applicationMethod"], "کودآبیاری")
|
||||
self.assertEqual(result["sections"][2]["type"], "warning")
|
||||
self.assertEqual(result["mergeMetadata"]["source"], "fallback_only")
|
||||
self.assertFalse(result["sections"][0]["provenance"]["llmProvided"])
|
||||
with self.assertRaises(ValueError):
|
||||
get_fertilization_recommendation(
|
||||
farm_uuid=str(self.farm_uuid),
|
||||
growth_stage="رویشی",
|
||||
)
|
||||
|
||||
@@ -41,6 +41,4 @@ class SoilAnomalyDetectionResponseSerializer(serializers.Serializer):
|
||||
generated_at = serializers.CharField()
|
||||
anomalies = serializers.JSONField()
|
||||
interpretation = serializers.JSONField()
|
||||
knowledge_base = serializers.CharField(allow_null=True, required=False)
|
||||
tone_file = serializers.CharField(allow_null=True, required=False)
|
||||
raw_response = serializers.CharField(allow_null=True, required=False)
|
||||
|
||||
@@ -464,7 +464,6 @@ class SoilAnomalyDetectionService:
|
||||
rag_payload = get_soil_anomaly_insight(
|
||||
farm_uuid=farm_uuid,
|
||||
anomaly_payload=anomaly_payload,
|
||||
ai_bundle=None,
|
||||
)
|
||||
return {
|
||||
"farm_uuid": farm_uuid,
|
||||
|
||||
@@ -92,8 +92,6 @@ class SoilAnomalyDetectionApiTests(TestCase):
|
||||
"monitoring_priority": "urgent",
|
||||
"confidence": 0.84,
|
||||
},
|
||||
"knowledge_base": "soil_anomaly",
|
||||
"tone_file": "config/tones/soil_anomaly_tone.txt",
|
||||
"raw_response": "{\"summary\":\"ok\"}",
|
||||
}
|
||||
)
|
||||
@@ -110,7 +108,6 @@ class SoilAnomalyDetectionApiTests(TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
payload = response.json()["data"]
|
||||
self.assertEqual(payload["farm_uuid"], "550e8400-e29b-41d4-a716-446655440000")
|
||||
self.assertEqual(payload["knowledge_base"], "soil_anomaly")
|
||||
self.assertEqual(payload["interpretation"]["monitoring_priority"], "urgent")
|
||||
|
||||
@patch("soile.views.apps.get_app_config")
|
||||
|
||||
@@ -32,6 +32,4 @@ class WaterNeedPredictionResponseSerializer(serializers.Serializer):
|
||||
series = serializers.JSONField()
|
||||
dailyBreakdown = serializers.JSONField()
|
||||
insight = serializers.JSONField()
|
||||
knowledge_base = serializers.CharField(allow_null=True, required=False)
|
||||
tone_file = serializers.CharField(allow_null=True, required=False)
|
||||
raw_response = serializers.CharField(allow_null=True, required=False)
|
||||
|
||||
@@ -87,8 +87,6 @@ class WaterNeedPredictionApiTests(TestCase):
|
||||
"risk_note": "در صورت بارش موثر برنامه بازبيني شود.",
|
||||
"confidence": 0.82,
|
||||
},
|
||||
"knowledge_base": "water_need_prediction",
|
||||
"tone_file": "config/tones/water_need_prediction_tone.txt",
|
||||
"raw_response": "{\"summary\":\"ok\"}",
|
||||
}
|
||||
)
|
||||
@@ -105,7 +103,6 @@ class WaterNeedPredictionApiTests(TestCase):
|
||||
self.assertEqual(response.status_code, 200)
|
||||
payload = response.json()["data"]
|
||||
self.assertEqual(payload["farm_uuid"], "550e8400-e29b-41d4-a716-446655440000")
|
||||
self.assertEqual(payload["knowledge_base"], "water_need_prediction")
|
||||
self.assertEqual(payload["insight"]["confidence"], 0.82)
|
||||
|
||||
@patch("weather.views.apps.get_app_config")
|
||||
|
||||
@@ -77,7 +77,5 @@ class WaterNeedPredictionService:
|
||||
"risk_note": insight.get("risk_note"),
|
||||
"confidence": insight.get("confidence"),
|
||||
},
|
||||
"knowledge_base": insight.get("knowledge_base"),
|
||||
"tone_file": insight.get("tone_file"),
|
||||
"raw_response": insight.get("raw_response"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user