代码拉取完成,页面将自动刷新
同步操作将从 馆主阿牛/ChatGPT-website 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -*- 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("prompts", None)
apiKey = request.form.get("apiKey", None)
model = request.form.get("model", "gpt-3.5-turbo")
if messages is None:
return jsonify({"error": {"message": "请输入prompts!", "type": "invalid_request_error", "code": ""}})
if apiKey is None:
apiKey = app.config['OPENAI_API_KEY']
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {apiKey}",
}
# json串转对象
prompts = json.loads(messages)
data = {
"messages": prompts,
"model": model,
"max_tokens": 1024,
"temperature": 0.5,
"top_p": 1,
"n": 1,
"stream": True,
}
try:
resp = requests.post(
url=app.config["URL"],
headers=headers,
json=data,
stream=True,
timeout=(10, 10) # 连接超时时间为10秒,读取超时时间为10秒
)
except requests.exceptions.Timeout:
return jsonify({"error": {"message": "请求超时,请稍后再试!", "type": "timeout_error", "code": ""}})
# 迭代器实现流式响应
def generate():
errorStr = ""
for chunk in resp.iter_lines():
if chunk:
streamStr = chunk.decode("utf-8").replace("data: ", "")
try:
streamDict = json.loads(streamStr) # 说明出现返回信息不是正常数据,是接口返回的具体错误信息
except:
errorStr += streamStr.strip() # 错误流式数据累加
continue
delData = streamDict["choices"][0]
if delData["finish_reason"] != None :
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。