@@ -1399,5 +1399,5 @@ msgstr "Невозможно получить данные модели" msgid "The payer does not exist" msgstr "Плательщик не существует" -msgid "The request must not be empty" -msgstr "Запрос не должен быть пустым" +msgid "The content must not be empty" +msgstr "Контент не должен быть пустым" @@ -1,11 +1,13 @@ import logging +from asgiref.sync import async_to_sync from django.core.cache import cache from django.http import StreamingHttpResponse from django.utils.translation import gettext_lazy as _ from rest_framework.response import Response from rest_framework.status import ( HTTP_403_FORBIDDEN, + HTTP_400_BAD_REQUEST, ) from rest_framework.views import APIView @@ -58,6 +60,11 @@ class BaseGenerationView(APIView): serializer = MessageSerializer(data=request.data) try: serializer.is_valid(raise_exception=True) + if not serializer.validated_data.get('content', ''): + return Response( + {'detail': _('The content must not be empty')}, + status=HTTP_400_BAD_REQUEST, + ) info = serializer.validated_data.pop('info') inference_slug = info.pop('inference') if not any( @@ -83,13 +90,14 @@ class BaseGenerationView(APIView): yield f'id: {output_slot_id}\nevent: start\ndata: [START]\n\n' try: - cache_key = f'messages:{output_slot_id}' + cache_key = f'apistores:{store.uid}' content = await cache.aget(cache_key, default=[]) while cache.has_key(cache_key): chunk = (await cache.aget(cache_key, default=[]))[len(content) :] if chunk: content += chunk - yield f'id: {output_slot_id}\nevent: output\ndata: {"".join(chunk).replace("\n", "\\n")}\n\n' + yield (f'id: {output_slot_id}\nevent: output\ndata: ' + f'{"".join(map(lambda x: x['content'], chunk)).replace("\n", "\\n")}\n\n') yield f'id: {output_slot_id}\nevent: done\ndata: [DONE]\n\n' except Exception as exc: @@ -110,10 +118,13 @@ class BaseGenerationView(APIView): output_message.from_public_api = True await output_message.asave() + async def collect_chunks(): + async for chunk in message_stream(): + pass + if request.query_params.get('stream', 'false') == 'true': return StreamingHttpResponse(message_stream(), content_type='text/event-stream') - for chunk in message_stream(): - pass + async_to_sync(collect_chunks)() return Response(MessageSerializer(output_message).data) @@ -57,6 +57,7 @@ services: - /bin/sh - -c - | + python manage.py compilemessages uvicorn backend.asgi:application --host 0.0.0.0 --ws wsproto --http httptools --lifespan off --log-level ${LOG_LEVEL:-debug} $(case ${DEBUG:-true} in ('true') echo '--reload' ;; esac) deploy: replicas: 1