@@ -53,10 +53,7 @@ async def proxy_endpoint(
5353
5454 # Verify authorization for non-public endpoints
5555 if not is_public :
56- await verify_access_key (
57- authorization = authorization ,
58- access_key = config ["server" ]["access_key" ],
59- )
56+ await verify_access_key (authorization = authorization )
6057
6158 # Log the full request URL including query parameters
6259 full_url = str (request .url ).replace (str (request .base_url ), "/" )
@@ -68,7 +65,14 @@ async def proxy_endpoint(
6865 # Parse request body (if any)
6966 request_body = None
7067 is_stream = False
71-
68+ # Get API key to use
69+ if not is_public :
70+ api_key = await key_manager .get_next_key ()
71+ if not api_key :
72+ raise HTTPException (status_code = 503 , detail = "No available API keys" )
73+ else :
74+ # For public endpoints, we don't need an API key
75+ api_key = ""
7276 try :
7377 body_bytes = await request .body ()
7478 if body_bytes :
@@ -93,21 +97,12 @@ async def proxy_endpoint(
9397 logger .debug ("Could not parse request body: %s" , str (e ))
9498 request_body = None
9599
96- # For binary, models endpoint , non-OpenAI-compatible endpoints or requests with model-specific parameters, fall back to httpx
100+ # For models, non-OpenAI-compatible endpoints or requests with model-specific parameters, fall back to httpx
97101 if is_httpx or not is_openai :
98- return await proxy_with_httpx (request , path , is_public , is_stream , is_completion )
102+ return await proxy_with_httpx (request , path , api_key , is_stream , is_completion )
99103
100104 # For OpenAI-compatible endpoints, use the OpenAI library
101105 try :
102- # Get API key to use
103- if not is_public :
104- api_key = await key_manager .get_next_key ()
105- if not api_key :
106- raise HTTPException (status_code = 503 , detail = "No available API keys" )
107- else :
108- # For public endpoints, we don't need an API key
109- api_key = ""
110-
111106 # Create an OpenAI client
112107 client = await get_openai_client (api_key )
113108
@@ -119,7 +114,7 @@ async def proxy_endpoint(
119114 else :
120115 # Fallback for other endpoints
121116 return await proxy_with_httpx (
122- request , path , is_public , is_stream , is_completion
117+ request , path , api_key , is_stream , is_completion
123118 )
124119
125120 except Exception as e :
@@ -234,10 +229,19 @@ async def stream_response() -> AsyncGenerator[bytes, None]:
234229 raise HTTPException (500 , f"Error processing chat completion: { str (e )} " ) from e
235230
236231
232+ async def _check_httpx_err (body : str or bytes , api_key : str or None ):
233+ if api_key and (isinstance (body , str ) and body .startswith ("data: " ) or (
234+ isinstance (body , bytes ) and body .startswith (b"data: " ))):
235+ body = body [6 :]
236+ has_rate_limit_error , reset_time_ms = check_rate_limit (body )
237+ if has_rate_limit_error :
238+ logger .warning ("Rate limit detected in stream. Disabling key." )
239+ await key_manager .disable_key (api_key , reset_time_ms )
240+
237241async def proxy_with_httpx (
238242 request : Request ,
239243 path : str ,
240- is_public : bool ,
244+ api_key : str ,
241245 is_stream : bool ,
242246 is_completion : bool ,
243247) -> Response :
@@ -260,20 +264,20 @@ async def proxy_with_httpx(
260264 if request .query_params :
261265 req_kwargs ["url" ] = f"{ req_kwargs ['url' ]} ?{ request .url .query } "
262266
263- if not is_public :
264- # For authenticated endpoints, use API key rotation
265- api_key = await key_manager .get_next_key ()
267+ if api_key :
266268 req_kwargs ["headers" ]["Authorization" ] = f"Bearer { api_key } "
267269
268270
269271 openrouter_resp = await client .request (** req_kwargs )
270272 if not is_stream :
273+ body = await openrouter_resp .aread ()
274+ await _check_httpx_err (body , api_key )
271275 return Response (
272- content = await openrouter_resp . aread () ,
276+ content = body ,
273277 status_code = openrouter_resp .status_code ,
274278 headers = dict (openrouter_resp .headers ),
275279 )
276- if is_public and not is_completion :
280+ if not api_key and not is_completion :
277281 return StreamingResponse (
278282 openrouter_resp .aiter_bytes (),
279283 status_code = openrouter_resp .status_code ,
@@ -296,19 +300,13 @@ async def stream_completion():
296300 yield f"{ line } \n \n " .encode ("utf-8" )
297301 except Exception as err :
298302 logger .error ("stream_completion error: %s" , err )
299- if not is_public and data .startswith ('data: ' ):
300- data = data [6 :]
301- has_rate_limit_error , reset_time_ms = check_rate_limit (data )
302- if has_rate_limit_error :
303- logger .warning ("Rate limit detected in stream. Disabling key." )
304- await key_manager .disable_key (api_key , reset_time_ms )
303+ await _check_httpx_err (data , api_key )
305304
306305 return StreamingResponse (
307306 stream_completion (),
308307 status_code = openrouter_resp .status_code ,
309308 headers = dict (openrouter_resp .headers ),
310309 )
311-
312310 except httpx .ConnectError as e :
313311 logger .error ("Connection error to OpenRouter: %s" , str (e ))
314312 raise HTTPException (503 , "Unable to connect to OpenRouter API" ) from e
0 commit comments