from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import asyncio
import uvicorn
app = FastAPI()
#流式输出的接口StreamingResponse
async def generate_data():
res_list = ["我", "特别", "喜欢", "玩儿", "大模型"]
for res in res_list:
await asyncio.sleep(0.5)
yield res
@app.post("/stream")
async def stream_data():
# 创建一个流式响应对象 ,他的第一个参数的是一个生成器函数 或者迭代器,生成器或者迭代器的返回值要用yield返回
return StreamingResponse(generate_data(), media_type="text/plain")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)