1 Star 0 Fork 308

koumis/ChatGPT-website

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
app.py 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
馆主阿牛 提交于 2023-04-21 21:48 +08:00 . 完成流式响应
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify, render_template, Response
import requests
import json
app = Flask(__name__)
# 从配置文件中settings加载配置
app.config.from_pyfile('settings.py')
@app.route("/", methods=["GET"])
def index():
return render_template("chat.html")
@app.route("/chat", methods=["POST"])
def chat():
messages = request.form.get("prompt", None)
apiKey = request.form.get("apiKey", None)
if messages is None:
return jsonify({"error": {"message": "请输入prompt!", "type": "invalid_request_error"}})
if apiKey is None:
apiKey = app.config['OPENAI_API_KEY']
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {apiKey}",
}
# json串转对象
prompt = json.loads(messages)
data = {
"messages": prompt,
"model": "gpt-3.5-turbo",
"max_tokens": 2048,
"temperature": 0.5,
"top_p": 1,
"n": 1,
"stream": True,
}
resp = requests.post(url=app.config["URL"], headers=headers, json=data, stream=True)
# 迭代器实现流式响应
def generate():
errorStr = ""
for chunk in resp.iter_lines():
if chunk:
streamStr = chunk.decode("utf-8").replace("data: ", "")
# print(streamStr)
try:
streamDict = json.loads(streamStr) # 说明出现返回信息不是正常数据,是接口返回的具体错误信息
except:
errorStr += streamStr.strip() # 错误流式数据累加
continue
delData = streamDict["choices"][0]
if delData["finish_reason"] == "stop":
break
else:
if "content" in delData["delta"]:
respStr = delData["delta"]["content"]
# print(respStr)
yield respStr
# 如果出现错误,此时错误信息迭代器已处理完,app_context已经出栈,要返回错误信息,需要将app_context手动入栈
if errorStr != "":
with app.app_context():
yield errorStr
return Response(generate(), content_type='application/octet-stream')
if __name__ == '__main__':
app.run(port=5000, debug=True)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/koumis/chat-gpt-website.git
git@gitee.com:koumis/chat-gpt-website.git
koumis
chat-gpt-website
ChatGPT-website
master

搜索帮助