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
@@ -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