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"))
相关推荐
念何架构之路16 分钟前
Go Socket编程
开发语言·后端·golang
ffqws_30 分钟前
Spring Boot 接收前端请求的四种参数方式
前端·spring boot·后端
时空系32 分钟前
第13篇:综合实战——制作我的小游戏 Rust中文编程
开发语言·后端·rust
咸鱼咸鱼36 分钟前
RustDesk 自建服务端教程:开源远程桌面,完全掌控你的数据
后端
0xDevNull44 分钟前
JDK多版本切换安装与配置
java·后端
开源情报局1 小时前
79%的企业在用AI Agent,但只有2%规模化落地——问题出在哪?
人工智能·python
算法与双吉汉堡1 小时前
【Nanobot项目笔记】项目架构
python·ai·agent·智能体
Java编程爱好者1 小时前
1-5 线程池:Thread+阻塞队列+循环
后端
jnrjian2 小时前
Library Cache Load Lock library cache pins are replaced by mutexes
java·后端·spring
knight_9___2 小时前
LLM工具调用面试篇6
人工智能·python·面试·职场和发展·llm·agent