UPDATE
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user