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
+25 -9
View File
@@ -111,6 +111,29 @@ def _create_audit_log(
return log
def _complete_audit_log(audit_log: "ChatAuditLog", response_text: str) -> None:
from .models import ChatAuditLog
audit_log.response_text = response_text
audit_log.status = ChatAuditLog.STATUS_COMPLETED
audit_log.save(update_fields=["response_text", "status", "updated_at"])
def _fail_audit_log(
audit_log: "ChatAuditLog",
error_message: str,
response_text: str = "",
) -> None:
from .models import ChatAuditLog
audit_log.response_text = response_text
audit_log.error_message = error_message
audit_log.status = ChatAuditLog.STATUS_FAILED
audit_log.save(
update_fields=["response_text", "error_message", "status", "updated_at"]
)
def build_rag_context(
query: str,
sensor_uuid: str | None = None,
@@ -267,9 +290,7 @@ def chat_rag_stream(
yield content
full_response = "".join(response_chunks)
audit_log.response_text = full_response
audit_log.status = ChatAuditLog.STATUS_COMPLETED
audit_log.save(update_fields=["response_text", "status", "updated_at"])
_complete_audit_log(audit_log, full_response)
logger.info(
"Completed chat response id=%s farm_uuid=%s response_len=%s response=\n%s",
audit_log.id,
@@ -279,12 +300,7 @@ def chat_rag_stream(
)
except Exception as exc:
partial_response = "".join(response_chunks)
audit_log.response_text = partial_response
audit_log.error_message = str(exc)
audit_log.status = ChatAuditLog.STATUS_FAILED
audit_log.save(
update_fields=["response_text", "error_message", "status", "updated_at"]
)
_fail_audit_log(audit_log, str(exc), partial_response)
logger.exception(
"Chat request failed id=%s service_id=%s farm_uuid=%s partial_response_len=%s",
audit_log.id,
+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
+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
+76
View File
@@ -0,0 +1,76 @@
from unittest.mock import patch
from django.test import TestCase
from rest_framework.test import APIClient
class RagRecommendationApiTests(TestCase):
def setUp(self):
self.client = APIClient()
@patch("rag.services.irrigation.get_irrigation_recommendation")
def test_irrigation_recommendation_returns_direct_result(self, mock_get_irrigation_recommendation):
mock_get_irrigation_recommendation.return_value = {
"plan": {
"frequencyPerWeek": 3,
"durationMinutes": 25,
},
"raw_response": "{\"plan\": {\"frequencyPerWeek\": 3, \"durationMinutes\": 25}}",
}
response = self.client.post(
"/api/rag/recommend/irrigation/",
data={
"sensor_uuid": "sensor-123",
"plant_name": "گوجه‌فرنگی",
"growth_stage": "میوه‌دهی",
"irrigation_method_name": "قطره‌ای",
},
format="json",
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["plan"]["frequencyPerWeek"], 3)
mock_get_irrigation_recommendation.assert_called_once_with(
sensor_uuid="sensor-123",
plant_name="گوجه‌فرنگی",
growth_stage="میوه‌دهی",
irrigation_method_name="قطره‌ای",
query=None,
)
@patch("rag.services.fertilization.get_fertilization_recommendation")
def test_fertilization_recommendation_returns_direct_result(self, mock_get_fertilization_recommendation):
mock_get_fertilization_recommendation.return_value = {
"plan": {
"npkRatio": "20-20-20",
"amountPerHectare": "80 kg",
},
"raw_response": "{\"plan\": {\"npkRatio\": \"20-20-20\", \"amountPerHectare\": \"80 kg\"}}",
}
response = self.client.post(
"/api/rag/recommend/fertilization/",
data={
"sensor_uuid": "sensor-456",
"plant_name": "گندم",
"growth_stage": "رویشی",
},
format="json",
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["plan"]["npkRatio"], "20-20-20")
mock_get_fertilization_recommendation.assert_called_once_with(
sensor_uuid="sensor-456",
plant_name="گندم",
growth_stage="رویشی",
query=None,
)
def test_removed_status_endpoints_return_404(self):
irrigation_response = self.client.get("/api/rag/recommend/irrigation/sample-task/status/")
fertilization_response = self.client.get("/api/rag/recommend/fertilization/sample-task/status/")
self.assertEqual(irrigation_response.status_code, 404)
self.assertEqual(fertilization_response.status_code, 404)
-4
View File
@@ -3,15 +3,11 @@ from django.urls import path
from .views import (
ChatView,
IrrigationRecommendationView,
IrrigationRecommendationStatusView,
FertilizationRecommendationView,
FertilizationRecommendationStatusView,
)
urlpatterns = [
path("chat/", ChatView.as_view()),
path("recommend/irrigation/", IrrigationRecommendationView.as_view(), name="recommend-irrigation"),
path("recommend/irrigation/<str:task_id>/status/", IrrigationRecommendationStatusView.as_view(), name="recommend-irrigation-status"),
path("recommend/fertilization/", FertilizationRecommendationView.as_view(), name="recommend-fertilization"),
path("recommend/fertilization/<str:task_id>/status/", FertilizationRecommendationStatusView.as_view(), name="recommend-fertilization-status"),
]
+63 -129
View File
@@ -21,8 +21,6 @@ from config.openapi import (
build_envelope_serializer,
build_message_response_serializer,
build_response,
build_task_queue_data_serializer,
build_task_status_data_serializer,
)
from .chat import chat_rag_stream
@@ -33,27 +31,19 @@ logger = logging.getLogger(__name__)
RagChatErrorResponseSerializer = build_message_response_serializer(
"RagChatErrorResponseSerializer"
)
RagIrrigationQueueResponseSerializer = build_envelope_serializer(
"RagIrrigationQueueResponseSerializer",
build_task_queue_data_serializer("RagIrrigationQueueDataSerializer"),
)
RagIrrigationStatusResponseSerializer = build_envelope_serializer(
"RagIrrigationStatusResponseSerializer",
build_task_status_data_serializer("RagIrrigationStatusDataSerializer"),
)
RagFertilizationQueueResponseSerializer = build_envelope_serializer(
"RagFertilizationQueueResponseSerializer",
build_task_queue_data_serializer("RagFertilizationQueueDataSerializer"),
)
RagFertilizationStatusResponseSerializer = build_envelope_serializer(
"RagFertilizationStatusResponseSerializer",
build_task_status_data_serializer("RagFertilizationStatusDataSerializer"),
)
RagValidationErrorResponseSerializer = build_envelope_serializer(
"RagValidationErrorResponseSerializer",
data_required=False,
allow_null=True,
)
RagIrrigationResponseSerializer = build_envelope_serializer(
"RagIrrigationResponseSerializer",
drf_serializers.JSONField(),
)
RagFertilizationResponseSerializer = build_envelope_serializer(
"RagFertilizationResponseSerializer",
drf_serializers.JSONField(),
)
class ChatView(APIView):
@@ -157,17 +147,17 @@ class ChatView(APIView):
class IrrigationRecommendationView(APIView):
"""
توصیه آبیاری با Celery.
توصیه آبیاری به صورت مستقیم.
POST با sensor_uuid، plant_name، growth_stage، irrigation_method_name.
تسک در صف قرار می‌گیرد و task_id برگشت داده می‌شود.
نتیجه همان لحظه برگشت داده می‌شود.
"""
@extend_schema(
tags=["RAG Recommendations"],
summary="درخواست توصیه آبیاری",
description=(
"داده‌های سنسور، گیاه و روش آبیاری را دریافت کرده و یک تسک Celery "
"برای تولید توصیه آبیاری در صف قرار می‌دهد."
"داده‌های سنسور، گیاه و روش آبیاری را دریافت کرده و "
"توصیه آبیاری را به صورت مستقیم برمی‌گرداند."
),
request=inline_serializer(
name="IrrigationRecommendationRequest",
@@ -180,14 +170,18 @@ class IrrigationRecommendationView(APIView):
},
),
responses={
202: build_response(
RagIrrigationQueueResponseSerializer,
"تسک توصیه آبیاری در صف قرار گرفت.",
200: build_response(
RagIrrigationResponseSerializer,
"توصیه آبیاری با موفقیت تولید شد.",
),
400: build_response(
RagValidationErrorResponseSerializer,
"پارامتر ورودی نامعتبر است.",
),
500: build_response(
RagValidationErrorResponseSerializer,
"خطا در تولید توصیه آبیاری.",
),
},
examples=[
OpenApiExample(
@@ -203,7 +197,7 @@ class IrrigationRecommendationView(APIView):
],
)
def post(self, request: Request):
from rag.tasks import irrigation_recommendation_task
from rag.services.irrigation import get_irrigation_recommendation
sensor_uuid = request.data.get("sensor_uuid")
if not sensor_uuid:
@@ -212,72 +206,40 @@ class IrrigationRecommendationView(APIView):
status=status.HTTP_400_BAD_REQUEST,
)
task = irrigation_recommendation_task.delay(
sensor_uuid=str(sensor_uuid),
plant_name=request.data.get("plant_name"),
growth_stage=request.data.get("growth_stage"),
irrigation_method_name=request.data.get("irrigation_method_name"),
query=request.data.get("query"),
)
try:
result = get_irrigation_recommendation(
sensor_uuid=str(sensor_uuid),
plant_name=request.data.get("plant_name"),
growth_stage=request.data.get("growth_stage"),
irrigation_method_name=request.data.get("irrigation_method_name"),
query=request.data.get("query"),
)
except Exception:
logger.exception("Direct irrigation recommendation failed for sensor %s", sensor_uuid)
return Response(
{"code": 500, "msg": "خطا در تولید توصیه آبیاری.", "data": None},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
return Response(
{
"code": 202,
"msg": "تسک توصیه آبیاری در صف قرار گرفت.",
"data": {
"task_id": task.id,
"status_url": f"/api/rag/recommend/irrigation/{task.id}/status/",
},
},
status=status.HTTP_202_ACCEPTED,
)
class IrrigationRecommendationStatusView(APIView):
"""وضعیت تسک توصیه آبیاری."""
@extend_schema(
tags=["RAG Recommendations"],
summary="وضعیت تسک توصیه آبیاری",
description="وضعیت تسک Celery توصیه آبیاری را برمی‌گرداند.",
responses={
200: build_response(
RagIrrigationStatusResponseSerializer,
"وضعیت فعلی تسک توصیه آبیاری.",
),
},
)
def get(self, request, task_id):
from celery.result import AsyncResult
result = AsyncResult(task_id)
data = {"task_id": task_id, "status": result.state}
if result.state == "PENDING":
data["message"] = "تسک در صف یا یافت نشد."
elif result.state == "PROGRESS":
data["progress"] = result.info
elif result.state == "SUCCESS":
data["result"] = result.result
elif result.state == "FAILURE":
data["error"] = str(result.result)
return Response(
{"code": 200, "msg": "success", "data": data},
{"code": 200, "msg": "success", "data": result},
status=status.HTTP_200_OK,
)
class FertilizationRecommendationView(APIView):
"""
توصیه کودهی با Celery.
توصیه کودهی به صورت مستقیم.
POST با sensor_uuid، plant_name، growth_stage.
تسک در صف قرار می‌گیرد و task_id برگشت داده می‌شود.
نتیجه همان لحظه برگشت داده می‌شود.
"""
@extend_schema(
tags=["RAG Recommendations"],
summary="درخواست توصیه کودهی",
description=(
"داده‌های سنسور و گیاه را دریافت کرده و یک تسک Celery "
"برای تولید توصیه کودهی در صف قرار می‌دهد."
"داده‌های سنسور و گیاه را دریافت کرده و "
"توصیه کودهی را به صورت مستقیم برمی‌گرداند."
),
request=inline_serializer(
name="FertilizationRecommendationRequest",
@@ -289,14 +251,18 @@ class FertilizationRecommendationView(APIView):
},
),
responses={
202: build_response(
RagFertilizationQueueResponseSerializer,
"تسک توصیه کودهی در صف قرار گرفت.",
200: build_response(
RagFertilizationResponseSerializer,
"توصیه کودهی با موفقیت تولید شد.",
),
400: build_response(
RagValidationErrorResponseSerializer,
"پارامتر ورودی نامعتبر است.",
),
500: build_response(
RagValidationErrorResponseSerializer,
"خطا در تولید توصیه کودهی.",
),
},
examples=[
OpenApiExample(
@@ -311,7 +277,7 @@ class FertilizationRecommendationView(APIView):
],
)
def post(self, request: Request):
from rag.tasks import fertilization_recommendation_task
from rag.services.fertilization import get_fertilization_recommendation
sensor_uuid = request.data.get("sensor_uuid")
if not sensor_uuid:
@@ -320,53 +286,21 @@ class FertilizationRecommendationView(APIView):
status=status.HTTP_400_BAD_REQUEST,
)
task = fertilization_recommendation_task.delay(
sensor_uuid=str(sensor_uuid),
plant_name=request.data.get("plant_name"),
growth_stage=request.data.get("growth_stage"),
query=request.data.get("query"),
)
try:
result = get_fertilization_recommendation(
sensor_uuid=str(sensor_uuid),
plant_name=request.data.get("plant_name"),
growth_stage=request.data.get("growth_stage"),
query=request.data.get("query"),
)
except Exception:
logger.exception("Direct fertilization recommendation failed for sensor %s", sensor_uuid)
return Response(
{"code": 500, "msg": "خطا در تولید توصیه کودهی.", "data": None},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
return Response(
{
"code": 202,
"msg": "تسک توصیه کودهی در صف قرار گرفت.",
"data": {
"task_id": task.id,
"status_url": f"/api/rag/recommend/fertilization/{task.id}/status/",
},
},
status=status.HTTP_202_ACCEPTED,
)
class FertilizationRecommendationStatusView(APIView):
"""وضعیت تسک توصیه کودهی."""
@extend_schema(
tags=["RAG Recommendations"],
summary="وضعیت تسک توصیه کودهی",
description="وضعیت تسک Celery توصیه کودهی را برمی‌گرداند.",
responses={
200: build_response(
RagFertilizationStatusResponseSerializer,
"وضعیت فعلی تسک توصیه کودهی.",
),
},
)
def get(self, request, task_id):
from celery.result import AsyncResult
result = AsyncResult(task_id)
data = {"task_id": task_id, "status": result.state}
if result.state == "PENDING":
data["message"] = "تسک در صف یا یافت نشد."
elif result.state == "PROGRESS":
data["progress"] = result.info
elif result.state == "SUCCESS":
data["result"] = result.result
elif result.state == "FAILURE":
data["error"] = str(result.result)
return Response(
{"code": 200, "msg": "success", "data": data},
{"code": 200, "msg": "success", "data": result},
status=status.HTTP_200_OK,
)