UPDATE
This commit is contained in:
+63
-129
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user