@@ -1113,3 +1113,6 @@ msgid "Model is blocked by outdating or temporary block, please retry later" msgstr "" "Модель заблокирована, т.к перестала обновляться или временно, попробуйте " "позже" + +msgid "The model is currently disabled. Please try again later." +msgstr "Модель в настоящее время неактивна. Пожалуйста, повторите попытку позже." @@ -1,6 +1,6 @@ +from django.utils.translation import gettext as _ # накинуть перевод через gettext_lazy - class GenerationException(Exception): def __str__(self): return 'Случилась ошибка во время генерации у этой модели, пожалуйста повторите попытку позже' @@ -10,3 +10,8 @@ class NSFWDetectedException(Exception): ... class LargeResourceConsumptionException(Exception): ... + + +class DeploymentDisabled(Exception): + def __str__(self): + return _('The model is currently disabled. Please try again later.') @@ -16,6 +16,7 @@ from deepl.translator import TextResult from requests import Response from backend import settings +from ml_model.exceptions import DeploymentDisabled from ml_model.utils import count_openrouter_tokens from poller.models import Proxy @@ -144,12 +145,14 @@ def openrouter_run(version: str, messages: list, callback_data: dict, model_name answer = f'**Рассуждение:**\n\n{reasoning}\n\n**Основная мысль:**\n\n{content}' elif content: answer = content - if data.get('choices')[0].get('error') is not None: - error = json.loads( - data['choices'][0]['error']['metadata'].replace("'", '"') - ).get('raw', {}).get('type') - if error in ('overloaded_error',): + if int(data.get('choices')[0].get('error', {}).get('code', 0)) == 502: + error_type = re.sub(r'["\']', '', str(data['choices'][0]['error']['message'])) + if error_type == 'Overloaded': + logger.warning(f"Model {model_name} overloaded") input_tokens, output_tokens = count_openrouter_tokens(model_name, messages, content + reasoning) + else: + logger.error(f"Model {model_name} disabled") + raise DeploymentDisabled else: input_tokens = data['usage']['prompt_tokens'] output_tokens = data['usage']['completion_tokens'] @@ -12,11 +12,13 @@ from rest_framework.response import Response from rest_framework.status import ( HTTP_402_PAYMENT_REQUIRED, HTTP_500_INTERNAL_SERVER_ERROR, + HTTP_503_SERVICE_UNAVAILABLE ) from rest_framework.views import APIView from messages.models import Message from messages.serializers import MessageSerializer +from ml_model.exceptions import DeploymentDisabled from ml_model.services.base import SimpleService from payments.exceptions.insufficient_balance import InsufficientBalance from tools.chats.models import Chat @@ -148,6 +150,13 @@ class MessagesAPIView(APIView): ) try: output_messages = service(chat).make(input_message) + except DeploymentDisabled as exc: + return Response( + { + 'detail': f'{exc}' + }, + status=HTTP_503_SERVICE_UNAVAILABLE, + ) except Exception as exc: input_message.is_sent = False input_message.save()