UPDATE
This commit is contained in:
@@ -6,7 +6,13 @@ import json
|
||||
import logging
|
||||
|
||||
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
|
||||
|
||||
@@ -97,6 +103,14 @@ def get_fertilization_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(
|
||||
@@ -106,7 +120,7 @@ def get_fertilization_recommendation(
|
||||
raw = response.choices[0].message.content.strip()
|
||||
except Exception as exc:
|
||||
logger.error("Fertilization recommendation error for %s: %s", sensor_uuid, exc)
|
||||
return {
|
||||
result = {
|
||||
"fertilizer_needed": None,
|
||||
"fertilizer_type": None,
|
||||
"amount_kg_per_hectare": None,
|
||||
@@ -114,6 +128,12 @@ def get_fertilization_recommendation(
|
||||
"npk_status": 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
|
||||
@@ -128,4 +148,8 @@ def get_fertilization_recommendation(
|
||||
}
|
||||
|
||||
result["raw_response"] = raw
|
||||
_complete_audit_log(
|
||||
audit_log,
|
||||
json.dumps(result, ensure_ascii=False, default=str),
|
||||
)
|
||||
return result
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user