-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
91 lines (69 loc) · 2.28 KB
/
proxy.py
File metadata and controls
91 lines (69 loc) · 2.28 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import json
import os
from typing import List
import httpx
from fastapi import FastAPI, Request, Response
DEFAULT_MODELS = ["gpt-5", "gpt-5.3-codex"]
def get_env_bool(name: str, default: bool) -> bool:
value = os.getenv(name)
if value is None:
return default
return value.strip().lower() in {"1", "true", "yes", "on"}
def get_models() -> List[str]:
raw = os.getenv("PROXY_MODELS", ",".join(DEFAULT_MODELS)).strip()
if not raw:
return DEFAULT_MODELS
return [item.strip() for item in raw.split(",") if item.strip()]
REAL_BASE = os.getenv("REAL_BASE", "https://ai.love-gwen.top/openai/v1").rstrip("/")
PROXY_TIMEOUT = float(os.getenv("PROXY_TIMEOUT", "120"))
FORCE_CHAT_STREAM = get_env_bool("FORCE_CHAT_STREAM", True)
PROXY_TITLE = os.getenv("PROXY_TITLE", "OpenAI-Compatible API Proxy")
app = FastAPI(title=PROXY_TITLE)
@app.get("/")
async def index():
return {
"name": PROXY_TITLE,
"upstream": REAL_BASE,
"models_endpoint": "/v1/models",
"health_endpoint": "/healthz",
}
@app.get("/healthz")
async def healthz():
return {"ok": True, "upstream": REAL_BASE}
@app.get("/v1/models")
async def models():
return {
"object": "list",
"data": [
{"id": model_id, "object": "model", "owned_by": "proxy"}
for model_id in get_models()
],
}
@app.api_route("/v1/{path:path}", methods=["GET", "POST"])
async def proxy(request: Request, path: str):
url = f"{REAL_BASE}/{path}"
body = await request.body()
headers = dict(request.headers)
headers.pop("host", None)
headers.pop("content-length", None)
if FORCE_CHAT_STREAM and path == "chat/completions":
try:
data = json.loads(body)
data["stream"] = True
body = json.dumps(data).encode()
except Exception:
pass
async with httpx.AsyncClient(timeout=PROXY_TIMEOUT) as client:
resp = await client.request(
request.method,
url,
content=body,
headers=headers,
params=request.query_params,
)
return Response(
content=resp.content,
status_code=resp.status_code,
headers=dict(resp.headers),
media_type=resp.headers.get("content-type"),
)