11import json
2- from typing import Any
2+ from typing import Any , Tuple
33
44import httpx
55import werkzeug
66
77
88def proxy_response (
9+ request : werkzeug .Request ,
910 method : str ,
1011 url : str ,
1112 headers : httpx ._types .HeaderTypes | None ,
@@ -14,7 +15,54 @@ def proxy_response(
1415 data : httpx ._types .RequestData | None ,
1516 files : httpx ._types .RequestFiles | None ,
1617) -> werkzeug .Response :
17- with httpx .Client () as client :
18+ is_llm , is_stream = check_llm_streaming_request (request )
19+
20+ if is_llm and is_stream :
21+ return proxy_stream_response (
22+ method = method ,
23+ url = url ,
24+ headers = headers ,
25+ params = params ,
26+ json = json ,
27+ data = data ,
28+ files = files ,
29+ timeout = httpx .Timeout (None , read = 300 , write = 10 ),
30+ )
31+ elif is_llm :
32+ return proxy_blocking_response (
33+ method = method ,
34+ url = url ,
35+ headers = headers ,
36+ params = params ,
37+ json = json ,
38+ data = data ,
39+ files = files ,
40+ timeout = httpx .Timeout (None , read = 300 , write = 10 ),
41+ )
42+ else :
43+ return proxy_blocking_response (
44+ method = method ,
45+ url = url ,
46+ headers = headers ,
47+ params = params ,
48+ json = json ,
49+ data = data ,
50+ files = files ,
51+ timeout = httpx .Timeout (None , read = 10 , write = 10 ),
52+ )
53+
54+
55+ def proxy_blocking_response (
56+ method : str ,
57+ url : str ,
58+ headers : httpx ._types .HeaderTypes | None ,
59+ params : httpx ._types .QueryParamTypes | None ,
60+ json : Any | None ,
61+ data : httpx ._types .RequestData | None ,
62+ files : httpx ._types .RequestFiles | None ,
63+ timeout : httpx ._types .TimeoutTypes | None ,
64+ ) -> werkzeug .Response :
65+ with httpx .Client (timeout = timeout ) as client :
1866 response = client .request (
1967 method = method , url = url , headers = headers , params = params , json = json , data = data , files = files
2068 )
@@ -34,8 +82,11 @@ def proxy_stream_response(
3482 json : Any | None ,
3583 data : httpx ._types .RequestData | None ,
3684 files : httpx ._types .RequestFiles | None ,
85+ timeout : httpx ._types .TimeoutTypes | None ,
3786) -> werkzeug .Response :
38- stream_context = httpx .stream (method = method , url = url , headers = headers , json = json , data = data , files = files )
87+ stream_context = httpx .stream (
88+ timeout = timeout , method = method , url = url , headers = headers , json = json , data = data , files = files
89+ )
3990
4091 # Manually enter the context manager to get the response
4192 stream_response = stream_context .__enter__ ()
@@ -62,3 +113,18 @@ def OidcApiProxyErrorResponse(message: str, error_code: int = 500) -> werkzeug.R
62113 status = error_code ,
63114 content_type = "application/json" ,
64115 )
116+
117+
118+ def check_llm_streaming_request (request : werkzeug .Request ) -> Tuple [bool , bool ]:
119+ is_llm = False
120+ is_stream = False
121+
122+ if request .method .lower () in ["post" ]:
123+ if request .path in ["/chat-messages" , "/workflows/run" ]:
124+ is_llm = True
125+ if request .is_json :
126+ json = request .get_json ()
127+ if str (json .get ("response_mode" , "" )).lower () == "streaming" :
128+ is_stream = True
129+
130+ return is_llm , is_stream
0 commit comments