Commit
·
dbad759
1
Parent(s):
9124b31
fix length
Browse files
main.py
CHANGED
|
@@ -6,6 +6,7 @@ from fastapi.responses import StreamingResponse
|
|
| 6 |
from fastapi.security import APIKeyHeader, APIKeyQuery
|
| 7 |
from itertools import cycle
|
| 8 |
import asyncio
|
|
|
|
| 9 |
|
| 10 |
# --- Configuration ---
|
| 11 |
PROXY_API_KEY = os.environ.get("PROXY_API_KEY")
|
|
@@ -71,13 +72,19 @@ async def proxy(request: Request, model_path: str, api_key: str = Security(get_a
|
|
| 71 |
express_key = next(key_rotator)
|
| 72 |
|
| 73 |
project_id = await get_project_id(express_key)
|
| 74 |
-
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
# Model-specific request body modification
|
| 78 |
if "gemini-2.5-flash-image-preview" in model_path:
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
target_url = f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{model_path}?key={express_key}"
|
| 83 |
|
|
@@ -86,19 +93,27 @@ async def proxy(request: Request, model_path: str, api_key: str = Security(get_a
|
|
| 86 |
yield chunk
|
| 87 |
|
| 88 |
async with httpx.AsyncClient(timeout=None) as client:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
req = client.build_request(
|
| 90 |
method=request.method,
|
| 91 |
url=target_url,
|
| 92 |
-
headers=
|
| 93 |
-
|
| 94 |
)
|
| 95 |
response = await client.send(req, stream=True)
|
| 96 |
|
| 97 |
if "streamGenerateContent" in model_path:
|
| 98 |
return StreamingResponse(stream_response(response), media_type=response.headers.get("content-type"))
|
| 99 |
else:
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
import uvicorn
|
|
|
|
| 6 |
from fastapi.security import APIKeyHeader, APIKeyQuery
|
| 7 |
from itertools import cycle
|
| 8 |
import asyncio
|
| 9 |
+
import json
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
PROXY_API_KEY = os.environ.get("PROXY_API_KEY")
|
|
|
|
| 72 |
express_key = next(key_rotator)
|
| 73 |
|
| 74 |
project_id = await get_project_id(express_key)
|
| 75 |
+
|
| 76 |
+
raw_request_body = await request.body()
|
| 77 |
+
request_body_to_send = raw_request_body
|
| 78 |
|
| 79 |
# Model-specific request body modification
|
| 80 |
if "gemini-2.5-flash-image-preview" in model_path:
|
| 81 |
+
try:
|
| 82 |
+
request_json = json.loads(raw_request_body)
|
| 83 |
+
if "generationConfig" in request_json and "thinkingConfig" in request_json.get("generationConfig", {}):
|
| 84 |
+
del request_json["generationConfig"]["thinkingConfig"]
|
| 85 |
+
request_body_to_send = json.dumps(request_json).encode('utf-8')
|
| 86 |
+
except json.JSONDecodeError:
|
| 87 |
+
pass # Not a json body, proxy as is
|
| 88 |
|
| 89 |
target_url = f"https://aiplatform.googleapis.com/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{model_path}?key={express_key}"
|
| 90 |
|
|
|
|
| 93 |
yield chunk
|
| 94 |
|
| 95 |
async with httpx.AsyncClient(timeout=None) as client:
|
| 96 |
+
# Exclude host, auth, and content-length headers from the original request
|
| 97 |
+
headers_to_proxy = {
|
| 98 |
+
k: v for k, v in request.headers.items()
|
| 99 |
+
if k.lower() not in ['host', 'authorization', 'x-goog-api-key', 'content-length']
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
req = client.build_request(
|
| 103 |
method=request.method,
|
| 104 |
url=target_url,
|
| 105 |
+
headers=headers_to_proxy,
|
| 106 |
+
content=request_body_to_send,
|
| 107 |
)
|
| 108 |
response = await client.send(req, stream=True)
|
| 109 |
|
| 110 |
if "streamGenerateContent" in model_path:
|
| 111 |
return StreamingResponse(stream_response(response), media_type=response.headers.get("content-type"))
|
| 112 |
else:
|
| 113 |
+
# For non-streaming, read the response and return it.
|
| 114 |
+
# httpx.Response can be returned directly in FastAPI
|
| 115 |
+
await response.aread()
|
| 116 |
+
return response
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
import uvicorn
|