最近在做大模型对话相关功能,需要将对话内容流式返回给前端页面(类似GPT的效果)。下面直接说下如何实现:
from fastapi import APIRouter, Response, status
from sse_starlette.sse import EventSourceResponse
@api_router.post("/stream-chat")
def stream_chat(ask_form: ChatParam, response: Response):
# 设置响应头部信息
response.headers["Content-Type"] = "text/event-stream"
response.headers["Cache-Control"] = "no-cache"
return EventSourceResponse(stream_generate_text(ask_form, stream=True))
def stream_generate_text(ask_form: ChatParam, stream: bool = False):
for res in model.chat(query=ask_form.prompt, stream=stream, historys=history, temperature=temperature):
yield json.dumps(
{
"answer": res
},
ensure_ascii=False,
)