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"))
相关推荐
chushiyunen6 小时前
python中的@Property和@Setter
java·开发语言·python
禾小西6 小时前
Java中使用正则表达式核心解析
java·python·正则表达式
2401_895521346 小时前
SpringBoot Maven快速上手
spring boot·后端·maven
yoyo_zzm6 小时前
JAVA (Springboot) i18n国际化语言配置
java·spring boot·python
disgare7 小时前
关于 spring 工程中添加 traceID 实践
java·后端·spring
ictI CABL7 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
weixin_408099678 小时前
图片去水印 API 接口实战:网站如何实现自动去水印(Python / PHP / C#)
图像处理·人工智能·python·c#·php·api·图片去水印
yyk的萌8 小时前
AI 应用开发工程师基础学习计划
开发语言·python·学习·ai·lua
小江的记录本9 小时前
【Linux】《Linux常用命令汇总表》
linux·运维·服务器·前端·windows·后端·macos