76 lines
3.1 KiB
Python
76 lines
3.1 KiB
Python
from django.db import transaction
|
|
from rest_framework.exceptions import ValidationError
|
|
from rest_framework.serializers import UUIDField
|
|
from rest_framework.views import APIView
|
|
|
|
from common.auth import TokenAuth
|
|
from common.auth.authentication import has_permissions
|
|
from common.constants.permission_constants import RoleConstants
|
|
from common.model_runtime import loading_policy
|
|
from common.model_runtime_client import runtime_request
|
|
from common.result import result
|
|
from models_provider.models import Model
|
|
|
|
|
|
def managed_model(model):
|
|
return ((model.provider == 'model_local_provider' and model.model_type in ('EMBEDDING', 'RERANKER')) or
|
|
(model.provider == 'model_ollama_provider' and model.model_type in ('EMBEDDING', 'LLM')))
|
|
|
|
|
|
class ModelLoading(APIView):
|
|
authentication_classes = [TokenAuth]
|
|
|
|
@staticmethod
|
|
def model(workspace_id, model_id):
|
|
model_id = UUIDField().run_validation(model_id)
|
|
model = Model.objects.filter(id=model_id, workspace_id=workspace_id).first()
|
|
if model is None:
|
|
raise ValidationError('模型不存在')
|
|
return model
|
|
|
|
@staticmethod
|
|
def state(model):
|
|
policy = loading_policy((model.meta or {}).get('loading_policy'))
|
|
if not managed_model(model):
|
|
return {**policy, 'supported': False, 'status': 'EXTERNAL'}
|
|
try:
|
|
return {**runtime_request('status', model.id), 'supported': True}
|
|
except ValueError:
|
|
return {**policy, 'supported': True, 'status': 'UNAVAILABLE', 'error': '本地模型服务不可用'}
|
|
|
|
@has_permissions(RoleConstants.ADMIN)
|
|
def get(self, request, workspace_id, model_id):
|
|
return result.success(self.state(self.model(workspace_id, model_id)))
|
|
|
|
@has_permissions(RoleConstants.ADMIN)
|
|
def put(self, request, workspace_id, model_id):
|
|
model = self.model(workspace_id, model_id)
|
|
if not managed_model(model):
|
|
raise ValidationError('该模型由外部服务运行')
|
|
try:
|
|
policy = loading_policy(request.data)
|
|
except ValueError as exc:
|
|
raise ValidationError(str(exc))
|
|
with transaction.atomic():
|
|
model = Model.objects.select_for_update().get(id=model.id)
|
|
model.meta = {**(model.meta or {}), 'loading_policy': policy}
|
|
model.save(update_fields=['meta', 'update_time'])
|
|
try:
|
|
runtime_request('configure', model.id)
|
|
except ValueError:
|
|
raise ValidationError('策略已保存,本地模型服务暂不可用,请恢复服务后重试')
|
|
return result.success(self.state(model))
|
|
|
|
@has_permissions(RoleConstants.ADMIN)
|
|
def post(self, request, workspace_id, model_id):
|
|
model = self.model(workspace_id, model_id)
|
|
if not managed_model(model):
|
|
raise ValidationError('该模型由外部服务运行')
|
|
operation = request.data.get('action')
|
|
if operation not in ('load', 'unload'):
|
|
raise ValidationError('无效操作')
|
|
try:
|
|
return result.success({**runtime_request(operation, model.id), 'supported': True})
|
|
except ValueError as exc:
|
|
raise ValidationError(str(exc))
|