Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
-
import
|
| 4 |
-
import aiohttp
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
from fastapi import FastAPI, Request, Response
|
| 7 |
from fastapi.responses import StreamingResponse, JSONResponse
|
|
|
|
| 8 |
|
|
|
|
| 9 |
TARGET_BASE_URL = "https://api.deepinfra.com/v1/openai"
|
| 10 |
-
REAL_API_KEY = os.environ["REAL_API_KEY"]
|
| 11 |
-
FAKE_KEY = os.environ.get("FAKE_KEY", "123456")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@asynccontextmanager
|
| 14 |
async def lifespan(app: FastAPI):
|
| 15 |
app.state.session = aiohttp.ClientSession()
|
| 16 |
yield
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
app = FastAPI(lifespan=lifespan)
|
| 20 |
|
|
|
|
| 21 |
@app.api_route("/v1/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
|
| 22 |
async def proxy_endpoint(path: str = "", request: Request = None):
|
| 23 |
if request.method == "OPTIONS":
|
|
@@ -30,10 +42,14 @@ async def proxy_endpoint(path: str = "", request: Request = None):
|
|
| 30 |
# 验证本地传来的假 key
|
| 31 |
auth_header = request.headers.get("authorization", "")
|
| 32 |
if auth_header.lower() != f"bearer {FAKE_KEY}":
|
| 33 |
-
return JSONResponse(
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
target_url = f"{TARGET_BASE_URL.rstrip('/')}/{path}" if path else TARGET_BASE_URL.rstrip('/')
|
| 36 |
|
|
|
|
| 37 |
headers = {}
|
| 38 |
for key, value in request.headers.items():
|
| 39 |
if key.lower() in ["host", "content-length", "connection", "authorization", "transfer-encoding"]:
|
|
@@ -44,51 +60,70 @@ async def proxy_endpoint(path: str = "", request: Request = None):
|
|
| 44 |
body = await request.body()
|
| 45 |
query_params = dict(request.query_params)
|
| 46 |
|
|
|
|
| 47 |
is_stream = False
|
| 48 |
if body:
|
| 49 |
try:
|
| 50 |
is_stream = json.loads(body).get("stream", False)
|
| 51 |
-
except:
|
| 52 |
pass
|
| 53 |
|
| 54 |
session = request.app.state.session
|
| 55 |
timeout = aiohttp.ClientTimeout(total=None, connect=10, sock_read=60)
|
| 56 |
|
|
|
|
| 57 |
resp = await session.request(
|
| 58 |
method=request.method,
|
| 59 |
url=target_url,
|
| 60 |
headers=headers,
|
| 61 |
data=body,
|
| 62 |
params=query_params,
|
| 63 |
-
timeout=timeout
|
|
|
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
if is_stream or "text/event-stream" in resp.headers.get("content-type", ""):
|
| 67 |
async def stream_generator():
|
| 68 |
try:
|
| 69 |
async for chunk in resp.content.iter_any():
|
| 70 |
yield chunk
|
| 71 |
-
except
|
|
|
|
|
|
|
| 72 |
pass
|
| 73 |
finally:
|
| 74 |
resp.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return StreamingResponse(
|
| 76 |
stream_generator(),
|
| 77 |
-
media_type=
|
| 78 |
-
headers=
|
| 79 |
-
"cache-control": "no-cache",
|
| 80 |
-
"connection": "keep-alive",
|
| 81 |
-
"access-control-allow-origin": "*"
|
| 82 |
-
}
|
| 83 |
)
|
|
|
|
|
|
|
| 84 |
else:
|
| 85 |
content = await resp.read()
|
| 86 |
resp.close()
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
+
import logging
|
|
|
|
| 5 |
from contextlib import asynccontextmanager
|
| 6 |
from fastapi import FastAPI, Request, Response
|
| 7 |
from fastapi.responses import StreamingResponse, JSONResponse
|
| 8 |
+
import aiohttp
|
| 9 |
|
| 10 |
+
# ================= 配置区 =================
|
| 11 |
TARGET_BASE_URL = "https://api.deepinfra.com/v1/openai"
|
| 12 |
+
REAL_API_KEY = os.environ["REAL_API_KEY"] # 从 Space Secrets 读取
|
| 13 |
+
FAKE_KEY = os.environ.get("FAKE_KEY", "123456") # 你本地使用的假 key
|
| 14 |
+
|
| 15 |
+
# Space 容器可直接访问外网,无需代理
|
| 16 |
+
LOCAL_PROXY = None
|
| 17 |
+
|
| 18 |
+
# ==========================================
|
| 19 |
+
|
| 20 |
+
logging.basicConfig(level=logging.INFO)
|
| 21 |
+
logger = logging.getLogger("proxy")
|
| 22 |
|
| 23 |
@asynccontextmanager
|
| 24 |
async def lifespan(app: FastAPI):
|
| 25 |
app.state.session = aiohttp.ClientSession()
|
| 26 |
yield
|
| 27 |
+
if app.state.session:
|
| 28 |
+
await app.state.session.close()
|
| 29 |
|
| 30 |
app = FastAPI(lifespan=lifespan)
|
| 31 |
|
| 32 |
+
@app.api_route("/v1", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
|
| 33 |
@app.api_route("/v1/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
|
| 34 |
async def proxy_endpoint(path: str = "", request: Request = None):
|
| 35 |
if request.method == "OPTIONS":
|
|
|
|
| 42 |
# 验证本地传来的假 key
|
| 43 |
auth_header = request.headers.get("authorization", "")
|
| 44 |
if auth_header.lower() != f"bearer {FAKE_KEY}":
|
| 45 |
+
return JSONResponse(
|
| 46 |
+
status_code=401,
|
| 47 |
+
content={"error": {"message": "Invalid local API key", "type": "invalid_request_error"}}
|
| 48 |
+
)
|
| 49 |
|
| 50 |
target_url = f"{TARGET_BASE_URL.rstrip('/')}/{path}" if path else TARGET_BASE_URL.rstrip('/')
|
| 51 |
|
| 52 |
+
# 构建转发 headers
|
| 53 |
headers = {}
|
| 54 |
for key, value in request.headers.items():
|
| 55 |
if key.lower() in ["host", "content-length", "connection", "authorization", "transfer-encoding"]:
|
|
|
|
| 60 |
body = await request.body()
|
| 61 |
query_params = dict(request.query_params)
|
| 62 |
|
| 63 |
+
# 判断是否流式请求
|
| 64 |
is_stream = False
|
| 65 |
if body:
|
| 66 |
try:
|
| 67 |
is_stream = json.loads(body).get("stream", False)
|
| 68 |
+
except json.JSONDecodeError:
|
| 69 |
pass
|
| 70 |
|
| 71 |
session = request.app.state.session
|
| 72 |
timeout = aiohttp.ClientTimeout(total=None, connect=10, sock_read=60)
|
| 73 |
|
| 74 |
+
# 转发请求到 DeepInfra
|
| 75 |
resp = await session.request(
|
| 76 |
method=request.method,
|
| 77 |
url=target_url,
|
| 78 |
headers=headers,
|
| 79 |
data=body,
|
| 80 |
params=query_params,
|
| 81 |
+
timeout=timeout,
|
| 82 |
+
proxy=LOCAL_PROXY
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# 流式响应
|
| 86 |
if is_stream or "text/event-stream" in resp.headers.get("content-type", ""):
|
| 87 |
async def stream_generator():
|
| 88 |
try:
|
| 89 |
async for chunk in resp.content.iter_any():
|
| 90 |
yield chunk
|
| 91 |
+
except (aiohttp.ClientConnectionError, aiohttp.ServerDisconnectedError,
|
| 92 |
+
aiohttp.ClientPayloadError, TimeoutError, asyncio.TimeoutError,
|
| 93 |
+
asyncio.CancelledError):
|
| 94 |
pass
|
| 95 |
finally:
|
| 96 |
resp.close()
|
| 97 |
+
|
| 98 |
+
resp_headers = {
|
| 99 |
+
"content-type": resp.headers.get("content-type", "text/event-stream"),
|
| 100 |
+
"cache-control": "no-cache",
|
| 101 |
+
"connection": "keep-alive",
|
| 102 |
+
"access-control-allow-origin": "*"
|
| 103 |
+
}
|
| 104 |
return StreamingResponse(
|
| 105 |
stream_generator(),
|
| 106 |
+
media_type=resp_headers["content-type"],
|
| 107 |
+
headers=resp_headers
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
+
|
| 110 |
+
# 非流式响应
|
| 111 |
else:
|
| 112 |
content = await resp.read()
|
| 113 |
resp.close()
|
| 114 |
+
|
| 115 |
+
# 调试日志(可保留,帮助排查)
|
| 116 |
+
logger.warning(f"UPSTREAM STATUS: {resp.status}")
|
| 117 |
+
logger.warning(f"UPSTREAM BODY LENGTH: {len(content)}")
|
| 118 |
+
if content:
|
| 119 |
+
logger.warning(f"UPSTREAM BODY PREVIEW: {content[:200]}")
|
| 120 |
+
|
| 121 |
+
resp_headers = {
|
| 122 |
+
"content-type": resp.headers.get("content-type", "application/json"),
|
| 123 |
+
"access-control-allow-origin": "*"
|
| 124 |
+
}
|
| 125 |
+
# 避免某些编码头干���客户端解析
|
| 126 |
+
for k in ["content-encoding", "transfer-encoding"]:
|
| 127 |
+
resp_headers.pop(k, None)
|
| 128 |
+
|
| 129 |
+
return Response(content=content, status_code=resp.status, headers=resp_headers)
|