Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -131,6 +131,7 @@ async def proxy_openai_api(request: Request):
|
|
131 |
streaming = client.stream(request.method, COMPLETIONS_URL, headers=headers, params=request.query_params, json=request_body)
|
132 |
async with streaming as stream_response:
|
133 |
if stream_response.status_code in {401, 402, 429}:
|
|
|
134 |
return
|
135 |
if stream_response.status_code == 403:
|
136 |
raise CensoredError('отклонено по цензуре') # это специфичная ошибка именно для опенроутера!
|
@@ -145,15 +146,22 @@ async def proxy_openai_api(request: Request):
|
|
145 |
except RequestError as exc:
|
146 |
raise HTTPException(status_code=500, detail=f'произошла ошибка при запросе: {exc}')
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
|
159 |
@cache_results
|
|
|
131 |
streaming = client.stream(request.method, COMPLETIONS_URL, headers=headers, params=request.query_params, json=request_body)
|
132 |
async with streaming as stream_response:
|
133 |
if stream_response.status_code in {401, 402, 429}:
|
134 |
+
yield 'auth_error'
|
135 |
return
|
136 |
if stream_response.status_code == 403:
|
137 |
raise CensoredError('отклонено по цензуре') # это специфичная ошибка именно для опенроутера!
|
|
|
146 |
except RequestError as exc:
|
147 |
raise HTTPException(status_code=500, detail=f'произошла ошибка при запросе: {exc}')
|
148 |
|
149 |
+
async def get_response():
|
150 |
+
for api_key in API_KEYS:
|
151 |
+
response_generator = stream_api_response(api_key)
|
152 |
+
try:
|
153 |
+
first_chunk = await response_generator.__anext__()
|
154 |
+
if first_chunk == 'auth_error':
|
155 |
+
print(f'ключ API {api_key} недействителен или превышен лимит отправки запросов')
|
156 |
+
continue
|
157 |
+
else:
|
158 |
+
return OverrideStreamResponse(itertools.chain([first_chunk], response_generator))
|
159 |
+
except StopAsyncIteration:
|
160 |
+
continue
|
161 |
+
raise HTTPException(status_code=401, detail='все ключи API использованы, доступ запрещен.')
|
162 |
+
|
163 |
+
response = await get_response()
|
164 |
+
return response
|
165 |
|
166 |
|
167 |
@cache_results
|