forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway_serving.py
More file actions
50 lines (40 loc) · 1.63 KB
/
Copy pathgateway_serving.py
File metadata and controls
50 lines (40 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Gateway-only serving transport-failure classification."""
from __future__ import annotations
from typing import Any
try:
import httpx
except ImportError: # pragma: no cover - stubbed test environments
httpx = None # type: ignore[assignment]
try:
from langchain_core.language_models import BaseChatModel # noqa: F401
except ImportError:
BaseChatModel = Any # type: ignore[misc,assignment]
from utils.llm.gateway_client import GATEWAY_TRANSPORT_STATUS_CODES
def is_gateway_transport_failure(exc: BaseException) -> bool:
"""Return True for gateway unreachable / hard HTTP failures."""
if httpx is not None:
if isinstance(exc, (httpx.TimeoutException, httpx.NetworkError, httpx.RemoteProtocolError)):
return True
if isinstance(exc, httpx.HTTPStatusError):
return exc.response is not None and exc.response.status_code in GATEWAY_TRANSPORT_STATUS_CODES
status_code = getattr(exc, 'status_code', None)
if isinstance(status_code, int) and status_code in GATEWAY_TRANSPORT_STATUS_CODES:
return True
response = getattr(exc, 'response', None)
response_status = getattr(response, 'status_code', None)
if isinstance(response_status, int) and response_status in GATEWAY_TRANSPORT_STATUS_CODES:
return True
message = str(exc).casefold()
transport_markers = (
'timeout',
'timed out',
'connection refused',
'connection reset',
'connecterror',
'network error',
'bad gateway',
'gateway timeout',
'502',
'504',
)
return any(marker in message for marker in transport_markers)