Update app.py
Browse files
app.py
CHANGED
|
@@ -56,54 +56,63 @@ def getModels():
|
|
| 56 |
logging.error("An unexpected error occurred in getModels: %s", e)
|
| 57 |
return jsonify({"success": False, "message": str(e)}), 500
|
| 58 |
|
| 59 |
-
@app.route("/v1/chat/completions",
|
| 60 |
def chat():
|
| 61 |
-
api_key = getAPI_KEY()
|
| 62 |
-
if not api_key:
|
| 63 |
-
return jsonify({"success": False, "message": "API Key not available"}), 500
|
| 64 |
-
|
| 65 |
headers = {
|
| 66 |
-
"Authorization":
|
| 67 |
-
"Content-Type":
|
| 68 |
}
|
| 69 |
data = request.get_json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
# 重点在这里:根据客户端请求中的 'stream' 字段决定是否进行流式转发
|
| 72 |
-
# 如果客户端没有提供 'stream' 字段,我们假设它需要非流式响应(或者默认值取决于上游API的约定)
|
| 73 |
-
# 但为了明确支持非流式,这里我们设为 False
|
| 74 |
-
client_wants_stream = data.get('stream', False) # 客户端请求中 stream 的值
|
| 75 |
|
| 76 |
try:
|
| 77 |
-
# 使用
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
)
|
| 86 |
-
upstream_response.raise_for_status() # 检查上游 API 响应的 HTTP 状态码
|
| 87 |
-
|
| 88 |
-
# 根据客户端是否想要流式响应来处理
|
| 89 |
-
if client_wants_stream:
|
| 90 |
-
# 流式响应:使用 stream_with_context 逐块发送
|
| 91 |
-
return Response(stream_with_context(upstream_response.iter_content(chunk_size=1024)),
|
| 92 |
-
status=upstream_response.status_code,
|
| 93 |
-
content_type=upstream_response.headers.get('content-type', 'application/json'))
|
| 94 |
else:
|
| 95 |
-
#
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
except requests.exceptions.RequestException as e:
|
| 101 |
-
logging.error("
|
| 102 |
return jsonify({"success": False, "message": f"Upstream API request failed: {e}"}), 500
|
| 103 |
except Exception as e:
|
| 104 |
-
logging.error("
|
| 105 |
return jsonify({"success": False, "message": str(e)}), 500
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
if __name__ == '__main__':
|
| 109 |
print("Starting Flask app...")
|
|
|
|
| 56 |
logging.error("An unexpected error occurred in getModels: %s", e)
|
| 57 |
return jsonify({"success": False, "message": str(e)}), 500
|
| 58 |
|
| 59 |
+
@app.route("/v1/chat/completions",methods=['POST'])
|
| 60 |
def chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
headers = {
|
| 62 |
+
"Authorization":"Bearer "+getAPI_KEY(),
|
| 63 |
+
"Content-Type":"application/json"
|
| 64 |
}
|
| 65 |
data = request.get_json()
|
| 66 |
+
stream_flag = data.get('stream', True)
|
| 67 |
+
|
| 68 |
+
def generate():
|
| 69 |
+
try:
|
| 70 |
+
with requests.post(API_BASE_URL+"/chat/completions", headers=headers, json=data, stream=stream_flag) as response:
|
| 71 |
+
response.raise_for_status() # 检查上游请求是否成功
|
| 72 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 73 |
+
yield chunk
|
| 74 |
+
except requests.exceptions.RequestException as e:
|
| 75 |
+
logging.error("Request to upstream API failed: %s", e)
|
| 76 |
+
# 在这里处理上游请求失败,例如可以 yield 一个错误消息或者抛出异常
|
| 77 |
+
# 但请注意,一旦开始 yield 数据,就不能改变 HTTP 状态码和头部了
|
| 78 |
+
yield b'{"error": "Upstream API request failed"}' # 作为 JSON 错误返回
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logging.error("Unexpected error during streaming: %s", e)
|
| 81 |
+
yield b'{"error": "Internal server error during streaming"}'
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
# 如果不是流式请求,可以考虑不使用生成器,或者根据 stream_flag 来判断
|
| 86 |
+
if not stream_flag:
|
| 87 |
+
# 对于非流式请求,直接返回完整响应
|
| 88 |
+
response = requests.post(API_BASE_URL+"/chat/completions", headers=headers, json=data, stream=False)
|
| 89 |
+
response.raise_for_status()
|
| 90 |
+
return Response(response.content,
|
| 91 |
+
status=response.status_code,
|
| 92 |
+
content_type=response.headers.get('content-type'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
else:
|
| 94 |
+
# 对于流式请求,使用生成器
|
| 95 |
+
# 注意:在生成器中处理异常时,如果已经开始发送数据,状态码和头部就不能更改了。
|
| 96 |
+
# 所以最好是在生成器开始之前捕获requests.post的异常。
|
| 97 |
+
initial_response = requests.post(API_BASE_URL+"/chat/completions", headers=headers, json=data, stream=True)
|
| 98 |
+
initial_response.raise_for_status() # 检查初始请求是否成功
|
| 99 |
+
|
| 100 |
+
return Response(generate_from_response(initial_response),
|
| 101 |
+
status=initial_response.status_code,
|
| 102 |
+
content_type=initial_response.headers.get('content-type'))
|
| 103 |
|
| 104 |
except requests.exceptions.RequestException as e:
|
| 105 |
+
logging.error("Initial upstream API request failed: %s", e)
|
| 106 |
return jsonify({"success": False, "message": f"Upstream API request failed: {e}"}), 500
|
| 107 |
except Exception as e:
|
| 108 |
+
logging.error("Error setting up chat completion: %s", e)
|
| 109 |
return jsonify({"success": False, "message": str(e)}), 500
|
| 110 |
|
| 111 |
+
def generate_from_response(upstream_response):
|
| 112 |
+
# 这是一个辅助函数,用于将上游响应的迭代器包装成一个生成器
|
| 113 |
+
for chunk in upstream_response.iter_content(chunk_size=1024):
|
| 114 |
+
yield chunk
|
| 115 |
+
|
| 116 |
|
| 117 |
if __name__ == '__main__':
|
| 118 |
print("Starting Flask app...")
|