This commit is contained in:
2026-04-27 18:02:26 +03:30
parent 7c2ec2144d
commit 190a668355
19 changed files with 193 additions and 825 deletions
+18 -228
View File
@@ -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
View File
@@ -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,
+46 -58
View File
@@ -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
+18 -55
View File
@@ -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
+17 -44
View File
@@ -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