This commit is contained in:
2026-04-24 02:12:06 +03:30
parent 31f4bf5d38
commit 302124aa87
18 changed files with 392 additions and 406 deletions
+26 -2
View File
@@ -8,7 +8,13 @@ import logging
from irrigation.evapotranspiration import calculate_forecast_water_needs, resolve_crop_profile, resolve_kc
from farm_data.models import SensorData
from rag.api_provider import get_chat_client
from rag.chat import build_rag_context, _load_service_tone
from rag.chat import (
_complete_audit_log,
_create_audit_log,
_fail_audit_log,
_load_service_tone,
build_rag_context,
)
from rag.config import load_rag_config, RAGConfig, get_service_config
from rag.user_data import build_plant_text, build_irrigation_method_text
from weather.models import WeatherForecast
@@ -149,6 +155,14 @@ def get_irrigation_recommendation(
{"role": "system", "content": system_content},
{"role": "user", "content": user_query},
]
audit_log = _create_audit_log(
farm_uuid=sensor_uuid,
service_id=SERVICE_ID,
model=model,
query=user_query,
system_prompt=system_content,
messages=messages,
)
try:
response = client.chat.completions.create(
@@ -158,13 +172,19 @@ 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)
return {
result = {
"irrigation_needed": None,
"amount_mm": None,
"reason": f"خطا در دریافت توصیه: {exc}",
"next_check_date": None,
"raw_response": None,
}
_fail_audit_log(
audit_log,
str(exc),
response_text=json.dumps(result, ensure_ascii=False, default=str),
)
return result
try:
cleaned = raw
@@ -184,4 +204,8 @@ def get_irrigation_recommendation(
"crop_profile": crop_profile,
"active_kc": active_kc,
}
_complete_audit_log(
audit_log,
json.dumps(result, ensure_ascii=False, default=str),
)
return result