Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,11 @@ def forward_to_target(subpath):
|
|
| 13 |
try:
|
| 14 |
# 构建目标 URL
|
| 15 |
target_url = f'https://{subpath}'
|
|
|
|
| 16 |
|
| 17 |
# 获取请求数据
|
| 18 |
data = request.json
|
|
|
|
| 19 |
|
| 20 |
# 检查是否是特定路径需要特殊处理
|
| 21 |
if '/v1/chat/completions' in subpath:
|
|
@@ -25,6 +27,7 @@ def forward_to_target(subpath):
|
|
| 25 |
|
| 26 |
api_key = auth_header.split(" ")[1]
|
| 27 |
target_url = f"https://{subpath.split('/')[1]}"
|
|
|
|
| 28 |
|
| 29 |
model = data['model']
|
| 30 |
messages = data['messages']
|
|
@@ -50,6 +53,7 @@ def forward_to_target(subpath):
|
|
| 50 |
'functions': functions,
|
| 51 |
'function_call': function_call
|
| 52 |
}
|
|
|
|
| 53 |
|
| 54 |
if stream:
|
| 55 |
def generate():
|
|
@@ -60,20 +64,26 @@ def forward_to_target(subpath):
|
|
| 60 |
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
| 61 |
else:
|
| 62 |
response = requests.post(target_url, headers=headers, json=payload)
|
|
|
|
| 63 |
return jsonify(response.json())
|
| 64 |
|
| 65 |
else:
|
| 66 |
# 获取请求头
|
| 67 |
headers = {key: value for key, value in request.headers if key != 'Host'}
|
|
|
|
| 68 |
|
| 69 |
# 转发请求到目标 URL
|
| 70 |
response = requests.post(target_url, headers=headers, json=data)
|
|
|
|
| 71 |
|
| 72 |
# 返回目标 URL 的响应
|
| 73 |
return Response(response.content, status=response.status_code, content_type=response.headers['Content-Type'])
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
-
print("Exception:
|
| 77 |
return jsonify({"error": str(e)}), 500
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 13 |
try:
|
| 14 |
# 构建目标 URL
|
| 15 |
target_url = f'https://{subpath}'
|
| 16 |
+
print(f"Target URL: {target_url}") # 调试信息
|
| 17 |
|
| 18 |
# 获取请求数据
|
| 19 |
data = request.json
|
| 20 |
+
print(f"Request data: {data}") # 调试信息
|
| 21 |
|
| 22 |
# 检查是否是特定路径需要特殊处理
|
| 23 |
if '/v1/chat/completions' in subpath:
|
|
|
|
| 27 |
|
| 28 |
api_key = auth_header.split(" ")[1]
|
| 29 |
target_url = f"https://{subpath.split('/')[1]}"
|
| 30 |
+
print(f"Adjusted target URL for special handling: {target_url}") # 调试信息
|
| 31 |
|
| 32 |
model = data['model']
|
| 33 |
messages = data['messages']
|
|
|
|
| 53 |
'functions': functions,
|
| 54 |
'function_call': function_call
|
| 55 |
}
|
| 56 |
+
print(f"Payload: {payload}") # 调试信息
|
| 57 |
|
| 58 |
if stream:
|
| 59 |
def generate():
|
|
|
|
| 64 |
return Response(stream_with_context(generate()), content_type='text/event-stream')
|
| 65 |
else:
|
| 66 |
response = requests.post(target_url, headers=headers, json=payload)
|
| 67 |
+
response.raise_for_status() # 确保抛出请求错误
|
| 68 |
return jsonify(response.json())
|
| 69 |
|
| 70 |
else:
|
| 71 |
# 获取请求头
|
| 72 |
headers = {key: value for key, value in request.headers if key != 'Host'}
|
| 73 |
+
print(f"Headers: {headers}") # 调试信息
|
| 74 |
|
| 75 |
# 转发请求到目标 URL
|
| 76 |
response = requests.post(target_url, headers=headers, json=data)
|
| 77 |
+
response.raise_for_status() # 确保抛出请求错误
|
| 78 |
|
| 79 |
# 返回目标 URL 的响应
|
| 80 |
return Response(response.content, status=response.status_code, content_type=response.headers['Content-Type'])
|
| 81 |
|
| 82 |
+
except requests.exceptions.RequestException as e:
|
| 83 |
+
print(f"RequestException: {e}") # 调试信息
|
| 84 |
+
return jsonify({"error": str(e)}), 500
|
| 85 |
except Exception as e:
|
| 86 |
+
print(f"Exception: {e}") # 调试信息
|
| 87 |
return jsonify({"error": str(e)}), 500
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|