flask流式接口

一、接口封装

复制代码
from flask import Flask, request, Response, stream_with_context
app = Flask(__name__)
app.logger.disabled = True

def chat_stream_(prompt):
    for new_text in ['1','2','3']:
        yield new_text

@app.route('/chat_stream', methods=['POST'])
def chat_stream():
    prompt = request.json['prompt']
    return app.response_class(stream_with_context(chat_stream_(prompt)))


if __name__ == '__main__':
    app.run('0.0.0.0', 1000)

二、接收流式返回

复制代码
import requests
url = "http://127.0.0.1:1000/chat_stream"
data = {"prompt":"怎么编研文档"}
res = requests.post(url, json=data, stream=True)
for token in res:
    print(token.decode("utf-8"))
相关推荐
黑客笔记9 小时前
python小工具win7翻车事故现场
python
Csvn10 小时前
Nginx 静态站点与 location 匹配:优先级、alias、缓存头与 gzip
后端·nginx
小满zs11 小时前
Go语言第二十章(初探Gin)
后端·go
学长毕设11 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
小满zs11 小时前
Go语言第十九章(http标准库)
后端·go
岁月宁静11 小时前
从零拆解 AI 语音模拟面试系统
人工智能·python·agent
benchmark_cc11 小时前
个人开发者接入五档盘口:用 QuantDash 读取单只股票的验收清单与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
ZhengEnCi13 小时前
项目越写越乱?先别改架构:用一次目录重排 + 一份可复算的体检脚本把乱治住
python·架构·ai编程
考虑考虑13 小时前
nginx拦截url
运维·后端·nginx
Dmz丶13 小时前
前端转 AI 100 天 Day 45:阶段复盘——从“会调 API”到“做出 AI 产品”
python