💻 Code: Increase the default timeout to 100 seconds to improve the success rate of o1-preview responses.
Browse files
main.py
CHANGED
@@ -20,11 +20,13 @@ from collections import defaultdict
|
|
20 |
from typing import List, Dict, Union
|
21 |
from urllib.parse import urlparse
|
22 |
|
|
|
|
|
|
|
23 |
@asynccontextmanager
|
24 |
async def lifespan(app: FastAPI):
|
25 |
# 启动时的代码
|
26 |
-
|
27 |
-
TIMEOUT = float(os.getenv("TIMEOUT", 40))
|
28 |
timeout = httpx.Timeout(connect=15.0, read=TIMEOUT, write=30.0, pool=30.0)
|
29 |
default_headers = {
|
30 |
"User-Agent": "curl/7.68.0", # 模拟 curl 的 User-Agent
|
@@ -215,9 +217,9 @@ async def process_request(request: Union[RequestModel, ImageGenerationRequest],
|
|
215 |
logger.info(f"provider: {provider['provider']:<10} model: {request.model:<10} engine: {engine}")
|
216 |
|
217 |
url, headers, payload = await get_payload(request, engine, provider)
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
try:
|
222 |
if request.stream:
|
223 |
model = provider['model'][request.model]
|
@@ -323,10 +325,10 @@ class ModelRequestHandler:
|
|
323 |
# else:
|
324 |
# if model_name in provider['model'].keys():
|
325 |
# provider_list.append(provider)
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
return provider_list
|
331 |
|
332 |
async def request_model(self, request: Union[RequestModel, ImageGenerationRequest], token: str, endpoint=None):
|
@@ -530,14 +532,14 @@ async def get_stats(request: Request, token: str = Depends(verify_admin_api_key)
|
|
530 |
if isinstance(middleware, StatsMiddleware):
|
531 |
async with middleware.lock:
|
532 |
stats = {
|
|
|
|
|
533 |
"request_counts": dict(middleware.request_counts),
|
534 |
"request_times": dict(middleware.request_times),
|
535 |
"ip_counts": {k: dict(v) for k, v in middleware.ip_counts.items()},
|
536 |
"request_arrivals": {k: [t.isoformat() for t in v] for k, v in middleware.request_arrivals.items()},
|
537 |
"channel_success_counts": dict(middleware.channel_success_counts),
|
538 |
"channel_failure_counts": dict(middleware.channel_failure_counts),
|
539 |
-
"channel_success_percentages": middleware.calculate_success_percentages(),
|
540 |
-
"channel_failure_percentages": middleware.calculate_failure_percentages()
|
541 |
}
|
542 |
return JSONResponse(content=stats)
|
543 |
return {"error": "StatsMiddleware not found"}
|
|
|
20 |
from typing import List, Dict, Union
|
21 |
from urllib.parse import urlparse
|
22 |
|
23 |
+
import os
|
24 |
+
is_debug = os.getenv("DEBUG", False)
|
25 |
+
|
26 |
@asynccontextmanager
|
27 |
async def lifespan(app: FastAPI):
|
28 |
# 启动时的代码
|
29 |
+
TIMEOUT = float(os.getenv("TIMEOUT", 100))
|
|
|
30 |
timeout = httpx.Timeout(connect=15.0, read=TIMEOUT, write=30.0, pool=30.0)
|
31 |
default_headers = {
|
32 |
"User-Agent": "curl/7.68.0", # 模拟 curl 的 User-Agent
|
|
|
217 |
logger.info(f"provider: {provider['provider']:<10} model: {request.model:<10} engine: {engine}")
|
218 |
|
219 |
url, headers, payload = await get_payload(request, engine, provider)
|
220 |
+
if is_debug:
|
221 |
+
logger.info(json.dumps(headers, indent=4, ensure_ascii=False))
|
222 |
+
logger.info(json.dumps(payload, indent=4, ensure_ascii=False))
|
223 |
try:
|
224 |
if request.stream:
|
225 |
model = provider['model'][request.model]
|
|
|
325 |
# else:
|
326 |
# if model_name in provider['model'].keys():
|
327 |
# provider_list.append(provider)
|
328 |
+
if is_debug:
|
329 |
+
import json
|
330 |
+
for provider in provider_list:
|
331 |
+
print(json.dumps(provider, indent=4, ensure_ascii=False, default=circular_list_encoder))
|
332 |
return provider_list
|
333 |
|
334 |
async def request_model(self, request: Union[RequestModel, ImageGenerationRequest], token: str, endpoint=None):
|
|
|
532 |
if isinstance(middleware, StatsMiddleware):
|
533 |
async with middleware.lock:
|
534 |
stats = {
|
535 |
+
"channel_success_percentages": middleware.calculate_success_percentages(),
|
536 |
+
"channel_failure_percentages": middleware.calculate_failure_percentages(),
|
537 |
"request_counts": dict(middleware.request_counts),
|
538 |
"request_times": dict(middleware.request_times),
|
539 |
"ip_counts": {k: dict(v) for k, v in middleware.ip_counts.items()},
|
540 |
"request_arrivals": {k: [t.isoformat() for t in v] for k, v in middleware.request_arrivals.items()},
|
541 |
"channel_success_counts": dict(middleware.channel_success_counts),
|
542 |
"channel_failure_counts": dict(middleware.channel_failure_counts),
|
|
|
|
|
543 |
}
|
544 |
return JSONResponse(content=stats)
|
545 |
return {"error": "StatsMiddleware not found"}
|