-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.py
More file actions
45 lines (35 loc) · 1.56 KB
/
Copy pathopenai.py
File metadata and controls
45 lines (35 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
from flask import request
from adapters.base import BaseAdapter
from adapters.common import clean_body, is_model_not_supported, try_fallback
from auth import get_copilot_token
from config import COPILOT_API_BASE
from proxy import build_headers, build_response, forward_request
class OpenAIAdapter(BaseAdapter):
def handle_request(self, path):
if get_copilot_token() is None:
return {"error": "Copilot token 未就绪,请检查授权状态"}, 503
normalized_path = path[3:] if path.startswith('v1/') else path
url = f"{COPILOT_API_BASE}/{normalized_path}"
headers = build_headers(request.headers.get('Content-Type', 'application/json'))
body = clean_body(request.get_data())
try:
resp = forward_request(
method=request.method,
url=url,
headers=headers,
data=body,
)
if resp.status_code != 200:
error_text = resp.text[:500]
print(f"[!] API 返回 {resp.status_code}: {error_text}")
if resp.status_code == 400 and is_model_not_supported(resp):
fallback_resp = try_fallback(request.method, url, headers, body)
if fallback_resp is not None:
resp = fallback_resp
return build_response(resp)
except requests.exceptions.Timeout:
return {"error": "请求超时"}, 504
except Exception as e:
print(f"[✗] 代理错误: {e}")
return {"error": str(e)}, 502