UPDATE
This commit is contained in:
-171
@@ -38,14 +38,6 @@ RagValidationErrorResponseSerializer = build_envelope_serializer(
|
||||
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):
|
||||
@@ -211,166 +203,3 @@ class ChatView(APIView):
|
||||
generate(),
|
||||
content_type="text/plain; charset=utf-8",
|
||||
)
|
||||
|
||||
|
||||
class IrrigationRecommendationView(APIView):
|
||||
"""
|
||||
توصیه آبیاری به صورت مستقیم.
|
||||
POST با farm_uuid، plant_name، growth_stage، irrigation_method_name.
|
||||
نتیجه همان لحظه برگشت داده میشود.
|
||||
"""
|
||||
|
||||
@extend_schema(
|
||||
tags=["RAG Recommendations"],
|
||||
summary="درخواست توصیه آبیاری",
|
||||
description=(
|
||||
"دادههای سنسور، گیاه و روش آبیاری را دریافت کرده و "
|
||||
"توصیه آبیاری را به صورت مستقیم برمیگرداند."
|
||||
),
|
||||
request=inline_serializer(
|
||||
name="IrrigationRecommendationRequest",
|
||||
fields={
|
||||
"farm_uuid": drf_serializers.CharField(help_text="شناسه یکتای مزرعه (اجباری)"),
|
||||
"sensor_uuid": drf_serializers.CharField(required=False, help_text="نام قدیمی برای farm_uuid"),
|
||||
"plant_name": drf_serializers.CharField(required=False, help_text="نام گیاه"),
|
||||
"growth_stage": drf_serializers.CharField(required=False, help_text="مرحله رشد گیاه"),
|
||||
"irrigation_method_name": drf_serializers.CharField(required=False, help_text="نام روش آبیاری"),
|
||||
"query": drf_serializers.CharField(required=False, help_text="سوال اختیاری"),
|
||||
},
|
||||
),
|
||||
responses={
|
||||
200: build_response(
|
||||
RagIrrigationResponseSerializer,
|
||||
"توصیه آبیاری با موفقیت تولید شد.",
|
||||
),
|
||||
400: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"پارامتر ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"خطا در تولید توصیه آبیاری.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست",
|
||||
value={
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
"plant_name": "گوجهفرنگی",
|
||||
"growth_stage": "میوهدهی",
|
||||
"irrigation_method_name": "آبیاری قطرهای",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request: Request):
|
||||
from rag.services.irrigation import get_irrigation_recommendation
|
||||
|
||||
farm_uuid = request.data.get("farm_uuid") or request.data.get("sensor_uuid")
|
||||
if not farm_uuid:
|
||||
return Response(
|
||||
{"code": 400, "msg": "پارامتر farm_uuid الزامی است.", "data": None},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
try:
|
||||
result = get_irrigation_recommendation(
|
||||
farm_uuid=str(farm_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 farm %s", farm_uuid)
|
||||
return Response(
|
||||
{"code": 500, "msg": "خطا در تولید توصیه آبیاری.", "data": None},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class FertilizationRecommendationView(APIView):
|
||||
"""
|
||||
توصیه کودهی به صورت مستقیم.
|
||||
POST با farm_uuid، plant_name، growth_stage.
|
||||
نتیجه همان لحظه برگشت داده میشود.
|
||||
"""
|
||||
|
||||
@extend_schema(
|
||||
tags=["RAG Recommendations"],
|
||||
summary="درخواست توصیه کودهی",
|
||||
description=(
|
||||
"دادههای سنسور و گیاه را دریافت کرده و "
|
||||
"توصیه کودهی را به صورت مستقیم برمیگرداند."
|
||||
),
|
||||
request=inline_serializer(
|
||||
name="FertilizationRecommendationRequest",
|
||||
fields={
|
||||
"farm_uuid": drf_serializers.CharField(help_text="شناسه یکتای مزرعه (اجباری)"),
|
||||
"sensor_uuid": drf_serializers.CharField(required=False, help_text="نام قدیمی برای farm_uuid"),
|
||||
"plant_name": drf_serializers.CharField(required=False, help_text="نام گیاه"),
|
||||
"growth_stage": drf_serializers.CharField(required=False, help_text="مرحله رشد گیاه"),
|
||||
"query": drf_serializers.CharField(required=False, help_text="سوال اختیاری"),
|
||||
},
|
||||
),
|
||||
responses={
|
||||
200: build_response(
|
||||
RagFertilizationResponseSerializer,
|
||||
"توصیه کودهی با موفقیت تولید شد.",
|
||||
),
|
||||
400: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"پارامتر ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"خطا در تولید توصیه کودهی.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست",
|
||||
value={
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
"plant_name": "گوجهفرنگی",
|
||||
"growth_stage": "رویشی",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request: Request):
|
||||
from rag.services.fertilization import get_fertilization_recommendation
|
||||
|
||||
farm_uuid = request.data.get("farm_uuid") or request.data.get("sensor_uuid")
|
||||
if not farm_uuid:
|
||||
return Response(
|
||||
{"code": 400, "msg": "پارامتر farm_uuid الزامی است.", "data": None},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
try:
|
||||
result = get_fertilization_recommendation(
|
||||
farm_uuid=str(farm_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 farm %s", farm_uuid)
|
||||
return Response(
|
||||
{"code": 500, "msg": "خطا در تولید توصیه کودهی.", "data": None},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user