UPDATE
This commit is contained in:
+41
-7
@@ -29,7 +29,7 @@ KB_NAME = "farm_alerts"
|
||||
SERVICE_ID = "farm_alerts"
|
||||
|
||||
TRACKER_PROMPT = (
|
||||
"وضعیت هشدارهای مزرعه را فقط بر اساس داده های ساختاریافته، اطلاعات مزرعه، و متون بازیابی شده از پایگاه دانش تحلیل کن. "
|
||||
"وضعیت هشدارهای مزرعه را فقط بر اساس داده های ساختاریافته، اطلاعات مزرعه، alertهاي ورودي، و متون بازیابی شده از پایگاه دانش تحلیل کن. "
|
||||
"پاسخ فقط JSON معتبر باشد و این کلیدها را داشته باشد: headline, overview, status_level, notifications. "
|
||||
"status_level فقط یکی از danger, warning, info باشد. "
|
||||
"notifications باید آرایه ای از آبجکت ها با کلیدهای level, title, message, suggested_action, source_alert_id, source_metric_type باشد. "
|
||||
@@ -38,7 +38,7 @@ TRACKER_PROMPT = (
|
||||
)
|
||||
|
||||
TIMELINE_PROMPT = (
|
||||
"بر اساس داده های هشدار مزرعه، یک timeline عملیاتی بساز. "
|
||||
"بر اساس داده های هشدار مزرعه و alertهاي ورودي، یک timeline عملیاتی بساز. "
|
||||
"پاسخ فقط JSON معتبر باشد و این کلیدها را داشته باشد: headline, overview, timeline, notifications. "
|
||||
"timeline باید آرایه ای از آبجکت ها با کلیدهای timestamp, level, title, description, source_alert_id, source_metric_type باشد. "
|
||||
"level فقط danger, warning, info باشد. "
|
||||
@@ -128,7 +128,30 @@ def _farm_profile(context: dict[str, Any], farm_uuid: str) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _build_structured_context(farm_uuid: str) -> tuple[dict[str, Any], dict[str, Any]]:
|
||||
def _normalize_incoming_alerts(alerts: list[dict[str, Any]] | None) -> list[dict[str, Any]]:
|
||||
normalized: list[dict[str, Any]] = []
|
||||
for item in alerts or []:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
normalized.append(
|
||||
{
|
||||
"alert_id": item.get("alert_id") or None,
|
||||
"level": item.get("level") or None,
|
||||
"title": item.get("title") or None,
|
||||
"message": item.get("message") or None,
|
||||
"suggested_action": item.get("suggested_action") or None,
|
||||
"source_metric_type": item.get("source_metric_type") or None,
|
||||
"timestamp": item.get("timestamp"),
|
||||
"payload": item.get("payload") if isinstance(item.get("payload"), dict) else {},
|
||||
}
|
||||
)
|
||||
return normalized
|
||||
|
||||
|
||||
def _build_structured_context(
|
||||
farm_uuid: str,
|
||||
incoming_alerts: list[dict[str, Any]] | None = None,
|
||||
) -> tuple[dict[str, Any], dict[str, Any]]:
|
||||
context = load_farm_context(farm_uuid)
|
||||
if context is None:
|
||||
raise ValueError("farm_uuid نامعتبر است یا اطلاعات هشدار مزرعه پیدا نشد.")
|
||||
@@ -138,6 +161,7 @@ def _build_structured_context(farm_uuid: str) -> tuple[dict[str, Any], dict[str,
|
||||
"farm_profile": _farm_profile(context, farm_uuid),
|
||||
"tracker": tracker,
|
||||
"forecasts": _forecast_summary(context),
|
||||
"incoming_alerts": _normalize_incoming_alerts(incoming_alerts),
|
||||
}
|
||||
return context, structured
|
||||
|
||||
@@ -336,8 +360,13 @@ def _llm_response(
|
||||
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]:
|
||||
_, structured_context = _build_structured_context(farm_uuid)
|
||||
def get_farm_alerts_tracker(
|
||||
*,
|
||||
farm_uuid: str,
|
||||
query: str | None = None,
|
||||
alerts: list[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
_, structured_context = _build_structured_context(farm_uuid, incoming_alerts=alerts)
|
||||
tracker = structured_context["tracker"]
|
||||
user_query = query or "وضعیت فعلی هشدارهای مزرعه را ارزیابی کن و اگر لازم است notification بساز."
|
||||
llm_result, raw_response, tone_file = _llm_response(
|
||||
@@ -368,8 +397,13 @@ def get_farm_alerts_tracker(*, farm_uuid: str, query: str | None = None) -> dict
|
||||
}
|
||||
|
||||
|
||||
def get_farm_alerts_timeline(*, farm_uuid: str, query: str | None = None) -> dict[str, Any]:
|
||||
_, structured_context = _build_structured_context(farm_uuid)
|
||||
def get_farm_alerts_timeline(
|
||||
*,
|
||||
farm_uuid: str,
|
||||
query: str | None = None,
|
||||
alerts: list[dict[str, Any]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
_, structured_context = _build_structured_context(farm_uuid, incoming_alerts=alerts)
|
||||
tracker = structured_context["tracker"]
|
||||
user_query = query or "برای هشدارهای مزرعه یک timeline عملیاتی بساز و اگر لازم است notification ثبت کن."
|
||||
llm_result, raw_response, tone_file = _llm_response(
|
||||
|
||||
Reference in New Issue
Block a user