一、接口封装
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"))