UPDATE
This commit is contained in:
+184
-28
@@ -5,6 +5,8 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from django.apps import apps
|
||||
|
||||
from farm_data.models import SensorData
|
||||
from rag.api_provider import get_chat_client
|
||||
from rag.chat import (
|
||||
@@ -23,23 +25,153 @@ KB_NAME = "fertilization"
|
||||
SERVICE_ID = "fertilization"
|
||||
|
||||
DEFAULT_FERTILIZATION_PROMPT = (
|
||||
"بر اساس دادههای خاک (NPK، pH)، مشخصات گیاه، مرحله رشد و پایگاه دانش کودهی، "
|
||||
"یک توصیه کودهی دقیق بده. "
|
||||
"پاسخ حتماً به فرمت JSON با ساختار زیر باشد:\n"
|
||||
'{\n'
|
||||
' "plan": {\n'
|
||||
' "npkRatio": "<str>",\n'
|
||||
' "amountPerHectare": "<str>",\n'
|
||||
' "applicationMethod": "<str>",\n'
|
||||
' "applicationInterval": "<str>",\n'
|
||||
' "reasoning": "<str>"\n'
|
||||
' }\n'
|
||||
'}\n'
|
||||
"فقط JSON خروجی بده، بدون توضیح اضافی. "
|
||||
"مقادیر را بر اساس شرایط واقعی خاک و گیاه محاسبه کن."
|
||||
"از داده های خاک، مرحله رشد و خروجی بهینه ساز شبیه سازی برای ساخت توصیه کودهی استفاده کن. "
|
||||
"اگر بلوک [خروجی بهینه ساز شبیه سازی] وجود داشت، همان را مرجع اصلی فرمول، مقدار، روش مصرف و اعتبار قرار بده. "
|
||||
"پاسخ فقط JSON معتبر با کلید sections باشد."
|
||||
)
|
||||
|
||||
|
||||
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 _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)
|
||||
if not isinstance(parsed_result, dict):
|
||||
return fallback
|
||||
|
||||
sections = parsed_result.get("sections")
|
||||
if not isinstance(sections, list):
|
||||
return fallback
|
||||
|
||||
recommendation = _find_section(sections, "recommendation") or {}
|
||||
list_section = _find_section(sections, "list") or {}
|
||||
warning_section = _find_section(sections, "warning") or {}
|
||||
|
||||
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"],
|
||||
}
|
||||
|
||||
return {"sections": [merged_recommendation, merged_list, merged_warning]}
|
||||
|
||||
|
||||
def get_fertilization_recommendation(
|
||||
sensor_uuid: str,
|
||||
plant_name: str | None = None,
|
||||
@@ -80,15 +212,38 @@ def get_fertilization_recommendation(
|
||||
user_query = query or "توصیه کودهی برای مزرعه من چیست؟"
|
||||
|
||||
sensor = (
|
||||
SensorData.objects.prefetch_related("plants")
|
||||
SensorData.objects.select_related("center_location")
|
||||
.prefetch_related("plants")
|
||||
.filter(farm_uuid=sensor_uuid)
|
||||
.first()
|
||||
)
|
||||
resolved_plant_name = plant_name
|
||||
plant = None
|
||||
if not resolved_plant_name and sensor is not None:
|
||||
plant = sensor.plants.first()
|
||||
if plant is not None:
|
||||
resolved_plant_name = plant.name
|
||||
elif sensor is not None and plant_name:
|
||||
plant = sensor.plants.filter(name=plant_name).first() or sensor.plants.first()
|
||||
|
||||
forecasts = []
|
||||
optimized_result = None
|
||||
if sensor is not None and getattr(sensor, "center_location", None) is not None:
|
||||
from weather.models import WeatherForecast
|
||||
|
||||
forecasts = list(
|
||||
WeatherForecast.objects.filter(
|
||||
location=sensor.center_location,
|
||||
forecast_date__isnull=False,
|
||||
).order_by("forecast_date")[:7]
|
||||
)
|
||||
if sensor is not None and plant is not None:
|
||||
optimized_result = _get_optimizer().optimize_fertilization(
|
||||
sensor=sensor,
|
||||
plant=plant,
|
||||
forecasts=forecasts,
|
||||
growth_stage=growth_stage,
|
||||
)
|
||||
|
||||
context = build_rag_context(
|
||||
user_query, sensor_uuid, config=cfg, limit=limit, kb_name=KB_NAME, service_id=SERVICE_ID,
|
||||
@@ -99,6 +254,11 @@ def get_fertilization_recommendation(
|
||||
plant_text = build_plant_text(resolved_plant_name, growth_stage)
|
||||
if plant_text:
|
||||
extra_parts.append("[اطلاعات گیاه]\n" + plant_text)
|
||||
if optimized_result is not None:
|
||||
extra_parts.append(
|
||||
"[خروجی بهینه ساز شبیه سازی]\n"
|
||||
+ optimized_result["context_text"]
|
||||
)
|
||||
if extra_parts:
|
||||
context = "\n\n---\n\n".join(extra_parts) + ("\n\n---\n\n" + context if context else "")
|
||||
|
||||
@@ -132,14 +292,9 @@ def get_fertilization_recommendation(
|
||||
raw = response.choices[0].message.content.strip()
|
||||
except Exception as exc:
|
||||
logger.error("Fertilization recommendation error for %s: %s", sensor_uuid, exc)
|
||||
result = {
|
||||
"fertilizer_needed": None,
|
||||
"fertilizer_type": None,
|
||||
"amount_kg_per_hectare": None,
|
||||
"reason": f"خطا در دریافت توصیه: {exc}",
|
||||
"npk_status": None,
|
||||
"raw_response": None,
|
||||
}
|
||||
result = _build_fertilization_fallback(optimized_result=optimized_result)
|
||||
result["error"] = f"خطا در دریافت توصیه: {exc}"
|
||||
result["raw_response"] = None
|
||||
_fail_audit_log(
|
||||
audit_log,
|
||||
str(exc),
|
||||
@@ -153,13 +308,14 @@ def get_fertilization_recommendation(
|
||||
cleaned = cleaned.strip("`").removeprefix("json").strip()
|
||||
result = json.loads(cleaned)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
result = {
|
||||
"plan": {
|
||||
"reasoning": raw,
|
||||
},
|
||||
}
|
||||
result = {}
|
||||
|
||||
result = _merge_fertilization_response(
|
||||
parsed_result=result,
|
||||
optimized_result=optimized_result,
|
||||
)
|
||||
result["raw_response"] = raw
|
||||
result["simulation_optimizer"] = optimized_result
|
||||
_complete_audit_log(
|
||||
audit_log,
|
||||
json.dumps(result, ensure_ascii=False, default=str),
|
||||
|
||||
+191
-26
@@ -5,6 +5,7 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from django.apps import apps
|
||||
from django.db import transaction
|
||||
from irrigation.models import IrrigationMethod
|
||||
from irrigation.evapotranspiration import calculate_forecast_water_needs, resolve_crop_profile, resolve_kc
|
||||
@@ -27,23 +28,171 @@ KB_NAME = "irrigation"
|
||||
SERVICE_ID = "irrigation"
|
||||
|
||||
DEFAULT_IRRIGATION_PROMPT = (
|
||||
"بر اساس محاسبات نهایی تبخیر-تعرق و نیاز آبی که در ورودی آمده، "
|
||||
"یک برنامه آبیاری قابلفهم برای کشاورز تولید کن. "
|
||||
"پاسخ حتماً به فرمت JSON با ساختار زیر باشد:\n"
|
||||
'{\n'
|
||||
' "plan": {\n'
|
||||
' "frequencyPerWeek": <int>,\n'
|
||||
' "durationMinutes": <int>,\n'
|
||||
' "bestTimeOfDay": "<str>",\n'
|
||||
' "moistureLevel": <int>,\n'
|
||||
' "warning": "<str>"\n'
|
||||
' }\n'
|
||||
'}\n'
|
||||
"فقط JSON خروجی بده، بدون توضیح اضافی. "
|
||||
"از انجام هرگونه محاسبه عددی جدید خودداری کن و فقط از دادههای ساختاریافته ورودی استفاده کن."
|
||||
"از محاسبات FAO-56 و خروجی بهینه ساز شبیه سازی برای ساخت توصیه آبیاری استفاده کن. "
|
||||
"اگر بلوک [خروجی بهینه ساز شبیه سازی] وجود داشت، همان را مرجع اصلی اعداد قرار بده. "
|
||||
"پاسخ فقط JSON معتبر با کلید sections باشد و عدد جدید متناقض نساز."
|
||||
)
|
||||
|
||||
|
||||
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 _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,
|
||||
)
|
||||
if not isinstance(parsed_result, dict):
|
||||
return fallback
|
||||
|
||||
sections = parsed_result.get("sections")
|
||||
if not isinstance(sections, list):
|
||||
return fallback
|
||||
|
||||
recommendation = _find_section(sections, "recommendation") or {}
|
||||
list_section = _find_section(sections, "list") or {}
|
||||
warning_section = _find_section(sections, "warning") or {}
|
||||
|
||||
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"],
|
||||
}
|
||||
|
||||
return {"sections": [merged_recommendation, merged_list, merged_warning]}
|
||||
|
||||
|
||||
def _resolve_irrigation_method(
|
||||
sensor: SensorData | None,
|
||||
irrigation_method_name: str | None,
|
||||
@@ -131,6 +280,7 @@ def get_irrigation_recommendation(
|
||||
active_kc = resolve_kc(crop_profile, growth_stage=growth_stage)
|
||||
forecasts = []
|
||||
daily_water_needs = []
|
||||
optimized_result = None
|
||||
if sensor is not None:
|
||||
forecasts = list(
|
||||
WeatherForecast.objects.filter(location=sensor.center_location, forecast_date__isnull=False)
|
||||
@@ -148,6 +298,15 @@ def get_irrigation_recommendation(
|
||||
growth_stage=growth_stage,
|
||||
irrigation_efficiency_percent=efficiency_percent,
|
||||
)
|
||||
if plant is not None and forecasts:
|
||||
optimized_result = _get_optimizer().optimize_irrigation(
|
||||
sensor=sensor,
|
||||
plant=plant,
|
||||
forecasts=forecasts,
|
||||
daily_water_needs=daily_water_needs,
|
||||
growth_stage=growth_stage,
|
||||
irrigation_method=irrigation_method,
|
||||
)
|
||||
|
||||
context = build_rag_context(
|
||||
user_query, sensor_uuid, config=cfg, limit=limit, kb_name=KB_NAME, service_id=SERVICE_ID,
|
||||
@@ -179,6 +338,11 @@ def get_irrigation_recommendation(
|
||||
f"Kc مورد استفاده: {active_kc}\n"
|
||||
+ "\n".join(schedule_lines)
|
||||
)
|
||||
if optimized_result is not None:
|
||||
extra_parts.append(
|
||||
"[خروجی بهینه ساز شبیه سازی]\n"
|
||||
+ optimized_result["context_text"]
|
||||
)
|
||||
if extra_parts:
|
||||
context = "\n\n---\n\n".join(extra_parts) + ("\n\n---\n\n" + context if context else "")
|
||||
|
||||
@@ -212,13 +376,12 @@ def get_irrigation_recommendation(
|
||||
raw = response.choices[0].message.content.strip()
|
||||
except Exception as exc:
|
||||
logger.error("Irrigation recommendation error for %s: %s", sensor_uuid, exc)
|
||||
result = {
|
||||
"irrigation_needed": None,
|
||||
"amount_mm": None,
|
||||
"reason": f"خطا در دریافت توصیه: {exc}",
|
||||
"next_check_date": None,
|
||||
"raw_response": None,
|
||||
}
|
||||
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),
|
||||
@@ -232,18 +395,20 @@ def get_irrigation_recommendation(
|
||||
cleaned = cleaned.strip("`").removeprefix("json").strip()
|
||||
result = json.loads(cleaned)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
result = {
|
||||
"plan": {
|
||||
"warning": raw,
|
||||
},
|
||||
}
|
||||
result = {}
|
||||
|
||||
result = _merge_irrigation_response(
|
||||
parsed_result=result,
|
||||
optimized_result=optimized_result,
|
||||
daily_water_needs=daily_water_needs,
|
||||
)
|
||||
result["raw_response"] = raw
|
||||
result["water_balance"] = {
|
||||
"daily": daily_water_needs,
|
||||
"crop_profile": crop_profile,
|
||||
"active_kc": active_kc,
|
||||
}
|
||||
result["simulation_optimizer"] = optimized_result
|
||||
result["selected_irrigation_method"] = (
|
||||
{
|
||||
"id": irrigation_method.id,
|
||||
|
||||
@@ -34,20 +34,87 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
farm_uuid=self.farm_uuid,
|
||||
center_location=self.location,
|
||||
irrigation_method=self.irrigation_method,
|
||||
sensor_payload={"sensor-7-1": {"soil_moisture": 30.0}},
|
||||
sensor_payload={
|
||||
"sensor-7-1": {
|
||||
"soil_moisture": 30.0,
|
||||
"nitrogen": 18.0,
|
||||
"phosphorus": 12.0,
|
||||
"potassium": 14.0,
|
||||
"soil_ph": 6.9,
|
||||
}
|
||||
},
|
||||
)
|
||||
self.farm.plants.set([self.plant])
|
||||
|
||||
def build_irrigation_optimizer_result(self):
|
||||
return {
|
||||
"engine": "crop_simulation_heuristic",
|
||||
"context_text": "optimizer irrigation context",
|
||||
"recommended_strategy": {
|
||||
"code": "balanced",
|
||||
"label": "آبیاری متعادل",
|
||||
"score": 88.0,
|
||||
"expected_yield_index": 91.0,
|
||||
"total_irrigation_mm": 24.0,
|
||||
"amount_per_event_mm": 8.0,
|
||||
"events": 3,
|
||||
"frequency_per_week": 3,
|
||||
"event_dates": ["2026-04-10"],
|
||||
"timing": "اوایل صبح",
|
||||
"moisture_target_percent": 70,
|
||||
"validity_period": "معتبر برای 3 روز آینده",
|
||||
"reasoning": ["شبیه ساز این سناریو را برتر ارزیابی کرد."],
|
||||
},
|
||||
"alternatives": [
|
||||
{
|
||||
"code": "protective",
|
||||
"label": "آبیاری حمایتی",
|
||||
"score": 80.0,
|
||||
"expected_yield_index": 85.0,
|
||||
"total_irrigation_mm": 28.0,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
def build_fertilization_optimizer_result(self):
|
||||
return {
|
||||
"engine": "crop_simulation_heuristic",
|
||||
"context_text": "optimizer fertilization context",
|
||||
"recommended_strategy": {
|
||||
"code": "balanced",
|
||||
"label": "تغذیه متعادل",
|
||||
"score": 84.0,
|
||||
"expected_yield_index": 88.0,
|
||||
"fertilizer_type": "20-20-20",
|
||||
"amount_kg_per_ha": 65.0,
|
||||
"application_method": "کودآبیاری",
|
||||
"timing": "صبح زود",
|
||||
"validity_period": "معتبر برای 7 روز آینده",
|
||||
"reasoning": ["کسری عناصر با این سناریو بهتر پوشش داده می شود."],
|
||||
},
|
||||
"alternatives": [
|
||||
{
|
||||
"code": "maintenance",
|
||||
"label": "تغذیه نگهدارنده",
|
||||
"score": 72.0,
|
||||
"expected_yield_index": 78.0,
|
||||
"amount_kg_per_ha": 45.0,
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
@patch("rag.services.irrigation.calculate_forecast_water_needs", return_value=[])
|
||||
@patch("rag.services.irrigation.resolve_kc", return_value=0.9)
|
||||
@patch("rag.services.irrigation.resolve_crop_profile", return_value={})
|
||||
@patch("rag.services.irrigation.build_irrigation_method_text", return_value="method text")
|
||||
@patch("rag.services.irrigation.build_plant_text", return_value="plant text")
|
||||
@patch("rag.services.irrigation.build_rag_context", return_value="")
|
||||
@patch("rag.services.irrigation._get_optimizer")
|
||||
@patch("rag.services.irrigation.get_chat_client")
|
||||
def test_irrigation_recommendation_uses_farm_relations_when_request_omits_names(
|
||||
self,
|
||||
mock_get_chat_client,
|
||||
mock_get_optimizer,
|
||||
mock_build_rag_context,
|
||||
mock_build_plant_text,
|
||||
mock_build_irrigation_method_text,
|
||||
@@ -55,8 +122,11 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
_mock_resolve_kc,
|
||||
_mock_calculate_forecast_water_needs,
|
||||
):
|
||||
mock_get_optimizer.return_value.optimize_irrigation.return_value = (
|
||||
self.build_irrigation_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
mock_response.choices = [Mock(message=Mock(content='{"plan": {"frequencyPerWeek": 2}}'))]
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "list", "title": "custom", "icon": "list", "items": ["مورد سفارشی"]}]}'))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_irrigation_recommendation(
|
||||
@@ -64,7 +134,8 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
growth_stage="میوهدهی",
|
||||
)
|
||||
|
||||
self.assertEqual(result["plan"]["frequencyPerWeek"], 2)
|
||||
self.assertEqual(result["sections"][0]["type"], "recommendation")
|
||||
self.assertEqual(result["sections"][0]["amount"], "8.0 میلی متر در هر نوبت (جمع کل 24.0 میلی متر)")
|
||||
mock_build_rag_context.assert_called_once()
|
||||
mock_build_plant_text.assert_called_once_with("گوجهفرنگی", "میوهدهی")
|
||||
mock_build_irrigation_method_text.assert_called_once_with("آبیاری قطرهای")
|
||||
@@ -72,6 +143,7 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
result["selected_irrigation_method"]["name"],
|
||||
"آبیاری قطرهای",
|
||||
)
|
||||
self.assertEqual(result["simulation_optimizer"]["engine"], "crop_simulation_heuristic")
|
||||
|
||||
@patch("rag.services.irrigation.calculate_forecast_water_needs", return_value=[])
|
||||
@patch("rag.services.irrigation.resolve_kc", return_value=0.9)
|
||||
@@ -79,10 +151,12 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
@patch("rag.services.irrigation.build_irrigation_method_text", return_value="method text")
|
||||
@patch("rag.services.irrigation.build_plant_text", return_value="plant text")
|
||||
@patch("rag.services.irrigation.build_rag_context", return_value="")
|
||||
@patch("rag.services.irrigation._get_optimizer")
|
||||
@patch("rag.services.irrigation.get_chat_client")
|
||||
def test_irrigation_recommendation_persists_selected_method_on_farm(
|
||||
self,
|
||||
mock_get_chat_client,
|
||||
mock_get_optimizer,
|
||||
_mock_build_rag_context,
|
||||
_mock_build_plant_text,
|
||||
mock_build_irrigation_method_text,
|
||||
@@ -94,8 +168,11 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
self.farm.irrigation_method = None
|
||||
self.farm.save(update_fields=["irrigation_method", "updated_at"])
|
||||
|
||||
mock_get_optimizer.return_value.optimize_irrigation.return_value = (
|
||||
self.build_irrigation_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
mock_response.choices = [Mock(message=Mock(content='{"plan": {"frequencyPerWeek": 4}}'))]
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "recommendation", "title": "test", "icon": "droplet", "content": "custom"}]}'))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_irrigation_recommendation(
|
||||
@@ -111,15 +188,20 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
|
||||
@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_uses_farm_plant_when_request_omits_name(
|
||||
self,
|
||||
mock_get_chat_client,
|
||||
mock_get_optimizer,
|
||||
_mock_build_rag_context,
|
||||
mock_build_plant_text,
|
||||
):
|
||||
mock_get_optimizer.return_value.optimize_fertilization.return_value = (
|
||||
self.build_fertilization_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
mock_response.choices = [Mock(message=Mock(content='{"plan": {"npkRatio": "20-20-20"}}'))]
|
||||
mock_response.choices = [Mock(message=Mock(content='{"sections": [{"type": "warning", "title": "هشدار", "icon": "alert-triangle", "content": "از اختلاط نامناسب خودداری شود."}]}'))]
|
||||
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
|
||||
|
||||
result = get_fertilization_recommendation(
|
||||
@@ -127,5 +209,32 @@ class RecommendationServiceDefaultsTests(TestCase):
|
||||
growth_stage="رویشی",
|
||||
)
|
||||
|
||||
self.assertEqual(result["plan"]["npkRatio"], "20-20-20")
|
||||
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")
|
||||
|
||||
@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(
|
||||
self,
|
||||
mock_get_chat_client,
|
||||
mock_get_optimizer,
|
||||
_mock_build_rag_context,
|
||||
_mock_build_plant_text,
|
||||
):
|
||||
mock_get_optimizer.return_value.optimize_fertilization.return_value = (
|
||||
self.build_fertilization_optimizer_result()
|
||||
)
|
||||
mock_response = Mock()
|
||||
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(
|
||||
sensor_uuid=str(self.farm_uuid),
|
||||
growth_stage="رویشی",
|
||||
)
|
||||
|
||||
self.assertEqual(result["sections"][0]["applicationMethod"], "کودآبیاری")
|
||||
self.assertEqual(result["sections"][2]["type"], "warning")
|
||||
|
||||
Reference in New Issue
Block a user