目录
代码
required | version |
---|---|
django | 3.2 |
注意:使用daphne等异步服务
启动时SSE实时推送失效,完全处理后把所有数据一次返回
from django.http.response import StreamingHttpResponse
@swagger_auto_schema(operation_description="知识问答(LLM)",
operation_summary='知识问答LLM',
tags=['知识'],
responses={**SWAGGER_RESPONSE_MESSAGE, 200: 'ok'})
@action(methods=['POST'], detail=False, url_path='qa', url_name='qa')
def qa(self, request, *args, **kwargs):
def event_stream():
for i in range(300):
message = f'好东西{i}\n\n'
yield f'data: {message}'
response = StreamingHttpResponse(event_stream(), content_type='text/event-stream')
response['Cache-Control'] = 'no-cache'
return response