66
77import httpx
88from httpx_retries import Retry , RetryTransport
9- from PIL import Image
109from loguru import logger
10+ from PIL import Image
1111
1212from .base_client import (
1313 DEFAULT_SYSTEM_PROMPT ,
@@ -48,6 +48,10 @@ def __init__(
4848 allow_truncated_content : bool = False ,
4949 max_concurrency : int = 100 ,
5050 http_timeout : int = 600 ,
51+ connect_timeout : int = 10 ,
52+ max_connections : int | None = None ,
53+ max_keepalive_connections : int | None = 20 ,
54+ keepalive_expiry : float | None = 5 ,
5155 debug : bool = False ,
5256 max_retries : int = 3 ,
5357 retry_backoff_factor : float = 0.5 ,
@@ -81,6 +85,10 @@ def __init__(
8185 self .server_headers = server_headers
8286
8387 self .http_timeout = http_timeout
88+ self .connect_timeout = connect_timeout
89+ self .max_connections = max_connections
90+ self .max_keepalive_connections = max_keepalive_connections
91+ self .keepalive_expiry = keepalive_expiry
8492 self .max_retries = max_retries
8593 self .retry_backoff_factor = retry_backoff_factor
8694
@@ -102,23 +110,47 @@ def chat_url(self) -> str:
102110 def _new_client (self ) -> httpx .Client :
103111 return httpx .Client (
104112 headers = self .server_headers ,
105- timeout = httpx .Timeout (connect = 10.0 , read = self .http_timeout , write = self .http_timeout , pool = None ),
113+ timeout = httpx .Timeout (
114+ connect = self .connect_timeout ,
115+ read = self .http_timeout ,
116+ write = self .http_timeout ,
117+ pool = None ,
118+ ),
106119 transport = RetryTransport (
107- retry = Retry (total = self .max_retries , backoff_factor = self .retry_backoff_factor ),
120+ retry = Retry (
121+ total = self .max_retries ,
122+ backoff_factor = self .retry_backoff_factor ,
123+ ),
108124 transport = httpx .HTTPTransport (
109- limits = httpx .Limits (max_connections = None , max_keepalive_connections = 20 ),
125+ limits = httpx .Limits (
126+ max_connections = self .max_connections ,
127+ max_keepalive_connections = self .max_keepalive_connections ,
128+ keepalive_expiry = self .keepalive_expiry ,
129+ ),
110130 ),
111131 ),
112132 )
113133
114134 async def _new_aio_client (self ) -> httpx .AsyncClient :
115135 return httpx .AsyncClient (
116136 headers = self .server_headers ,
117- timeout = httpx .Timeout (connect = 10.0 , read = self .http_timeout , write = self .http_timeout , pool = None ),
137+ timeout = httpx .Timeout (
138+ connect = self .connect_timeout ,
139+ read = self .http_timeout ,
140+ write = self .http_timeout ,
141+ pool = None ,
142+ ),
118143 transport = RetryTransport (
119- retry = Retry (total = self .max_retries , backoff_factor = self .retry_backoff_factor ),
144+ retry = Retry (
145+ total = self .max_retries ,
146+ backoff_factor = self .retry_backoff_factor ,
147+ ),
120148 transport = httpx .AsyncHTTPTransport (
121- limits = httpx .Limits (max_connections = None , max_keepalive_connections = 20 ),
149+ limits = httpx .Limits (
150+ max_connections = self .max_connections ,
151+ max_keepalive_connections = self .max_keepalive_connections ,
152+ keepalive_expiry = self .keepalive_expiry ,
153+ ),
122154 ),
123155 ),
124156 )
@@ -290,7 +322,7 @@ def get_response_content(self, response_data: dict) -> str:
290322 # Set MINERU_VLM_END_TOKEN to override or disable stripping (e.g., set to an empty string).
291323 end_token = os .getenv ("MINERU_VLM_END_TOKEN" , "<|im_end|>" )
292324 if end_token and isinstance (content , str ) and content .endswith (end_token ):
293- content = content [:- len (end_token )]
325+ content = content [: - len (end_token )]
294326 return content or ""
295327
296328 def predict (
0 commit comments