model_apply.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎
  5. @file: model_apply.py
  6. @date:2024/8/20 20:38
  7. @desc:
  8. """
  9. from urllib.request import Request
  10. from django.utils.translation import gettext_lazy as _
  11. from drf_spectacular.utils import extend_schema
  12. from rest_framework.views import APIView
  13. from common.auth.authentication import has_permissions
  14. from common.constants.permission_constants import PermissionConstants
  15. from common.result import result
  16. from models_provider.api.model import DefaultModelResponse
  17. from models_provider.serializers.model_apply_serializers import ModelApplySerializers
  18. class ModelApply(APIView):
  19. class EmbedDocuments(APIView):
  20. @extend_schema(methods=['POST'],
  21. summary=_('Vectorization documentation'),
  22. description=_('Vectorization documentation'),
  23. operation_id=_('Vectorization documentation'), # type: ignore
  24. responses=DefaultModelResponse.get_response(),
  25. tags=[_('Model')] # type: ignore
  26. )
  27. def post(self, request: Request, model_id):
  28. return result.success(
  29. ModelApplySerializers(data={'model_id': model_id}).embed_documents(request.data))
  30. class EmbedQuery(APIView):
  31. @extend_schema(methods=['POST'],
  32. summary=_('Vectorization documentation'),
  33. description=_('Vectorization documentation'),
  34. operation_id=_('Vectorization documentation'), # type: ignore
  35. responses=DefaultModelResponse.get_response(),
  36. tags=[_('Model')] # type: ignore
  37. )
  38. def post(self, request: Request, model_id):
  39. return result.success(
  40. ModelApplySerializers(data={'model_id': model_id}).embed_query(request.data))
  41. class CompressDocuments(APIView):
  42. @extend_schema(methods=['POST'],
  43. summary=_('Reorder documents'),
  44. description=_('Reorder documents'),
  45. operation_id=_('Reorder documents'), # type: ignore
  46. responses=DefaultModelResponse.get_response(),
  47. tags=[_('Model')] # type: ignore
  48. )
  49. def post(self, request: Request, model_id):
  50. return result.success(
  51. ModelApplySerializers(data={'model_id': model_id}).compress_documents(request.data))