This commit is contained in:
2026-04-24 22:20:15 +03:30
parent f7dc05dc9e
commit 569d520a5c
24 changed files with 687 additions and 152 deletions
+13 -8
View File
@@ -219,20 +219,21 @@ def _persist_irrigation_method_on_farm(
def get_irrigation_recommendation(
sensor_uuid: str,
farm_uuid: str | None = None,
plant_name: str | None = None,
growth_stage: str | None = None,
irrigation_method_name: str | None = None,
query: str | None = None,
config: RAGConfig | None = None,
limit: int = 8,
sensor_uuid: str | None = None,
) -> dict:
"""
توصیه آبیاری برای یک سنسور (کاربر).
توصیه آبیاری برای یک مزرعه.
از RAG با پایگاه دانش irrigation استفاده می‌کند.
Args:
sensor_uuid: شناسه سنسور کاربر
farm_uuid: شناسه مزرعه
plant_name: نام گیاه (برای بارگذاری مشخصات از جدول Plant)
growth_stage: مرحله رشد گیاه
irrigation_method_name: نام روش آبیاری (برای بارگذاری از جدول IrrigationMethod)
@@ -241,7 +242,7 @@ def get_irrigation_recommendation(
limit: تعداد چانک‌های بازیابی‌شده
Returns:
dict با کلیدهای irrigation_needed, amount_mm, reason, next_check_date, raw_response
dict ساختاریافته برای توصیه آبیاری
"""
cfg = config or load_rag_config()
service = get_service_config(SERVICE_ID, cfg)
@@ -257,12 +258,16 @@ def get_irrigation_recommendation(
client = get_chat_client(service_cfg)
model = service.llm.model
resolved_farm_uuid = str(farm_uuid or sensor_uuid or "").strip()
if not resolved_farm_uuid:
raise ValueError("farm_uuid is required.")
user_query = query or "توصیه آبیاری برای مزرعه من چیست؟"
sensor = (
SensorData.objects.select_related("center_location", "irrigation_method")
.prefetch_related("plants")
.filter(farm_uuid=sensor_uuid)
.filter(farm_uuid=resolved_farm_uuid)
.first()
)
irrigation_method = _resolve_irrigation_method(sensor, irrigation_method_name)
@@ -309,7 +314,7 @@ def get_irrigation_recommendation(
)
context = build_rag_context(
user_query, sensor_uuid, config=cfg, limit=limit, kb_name=KB_NAME, service_id=SERVICE_ID,
user_query, resolved_farm_uuid, config=cfg, limit=limit, kb_name=KB_NAME, service_id=SERVICE_ID,
)
extra_parts: list[str] = []
@@ -360,7 +365,7 @@ def get_irrigation_recommendation(
{"role": "user", "content": user_query},
]
audit_log = _create_audit_log(
farm_uuid=sensor_uuid,
farm_uuid=resolved_farm_uuid,
service_id=SERVICE_ID,
model=model,
query=user_query,
@@ -375,7 +380,7 @@ 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)
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,