client_cache
langroid/language_models/client_cache.py
Client caching/singleton pattern for LLM clients to prevent connection pool exhaustion.
get_openai_client(api_key, base_url=None, organization=None, timeout=120.0, default_headers=None, http_client=None, http_client_config=None)
¶
Get or create a singleton OpenAI client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
OpenAI API key |
required |
base_url
|
Optional[str]
|
Optional base URL for API |
None
|
organization
|
Optional[str]
|
Optional organization ID |
None
|
timeout
|
Union[float, Timeout]
|
Request timeout |
120.0
|
default_headers
|
Optional[Dict[str, str]]
|
Optional default headers |
None
|
http_client
|
Optional[Any]
|
Optional httpx.Client instance |
None
|
http_client_config
|
Optional[Dict[str, Any]]
|
Optional config dict for creating httpx.Client |
None
|
Returns:
| Type | Description |
|---|---|
OpenAI
|
OpenAI client instance |
Source code in langroid/language_models/client_cache.py
get_async_openai_client(api_key, base_url=None, organization=None, timeout=120.0, default_headers=None, http_client=None, http_client_config=None)
¶
Get or create a singleton AsyncOpenAI client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
OpenAI API key |
required |
base_url
|
Optional[str]
|
Optional base URL for API |
None
|
organization
|
Optional[str]
|
Optional organization ID |
None
|
timeout
|
Union[float, Timeout]
|
Request timeout |
120.0
|
default_headers
|
Optional[Dict[str, str]]
|
Optional default headers |
None
|
http_client
|
Optional[Any]
|
Optional httpx.AsyncClient instance |
None
|
http_client_config
|
Optional[Dict[str, Any]]
|
Optional config dict for creating httpx.AsyncClient |
None
|
Returns:
| Type | Description |
|---|---|
AsyncOpenAI
|
AsyncOpenAI client instance |
Source code in langroid/language_models/client_cache.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
get_groq_client(api_key)
¶
Get or create a singleton Groq client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Groq API key |
required |
Returns:
| Type | Description |
|---|---|
Groq
|
Groq client instance |
Source code in langroid/language_models/client_cache.py
get_async_groq_client(api_key)
¶
Get or create a singleton AsyncGroq client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Groq API key |
required |
Returns:
| Type | Description |
|---|---|
AsyncGroq
|
AsyncGroq client instance |
Source code in langroid/language_models/client_cache.py
get_cerebras_client(api_key)
¶
Get or create a singleton Cerebras client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Cerebras API key |
required |
Returns:
| Type | Description |
|---|---|
Cerebras
|
Cerebras client instance |
Source code in langroid/language_models/client_cache.py
get_async_cerebras_client(api_key)
¶
Get or create a singleton AsyncCerebras client with the given configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Cerebras API key |
required |
Returns:
| Type | Description |
|---|---|
AsyncCerebras
|
AsyncCerebras client instance |
Source code in langroid/language_models/client_cache.py
prune_cache(max_age_seconds)
¶
Remove cache entries whose last-used time exceeds max_age_seconds.
Evicted clients are not closed here because they may still be serving
in-flight requests. Cleanup is handled by the atexit handler and the
garbage collector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age_seconds
|
float
|
Maximum age (in seconds) for cache entries to keep. Entries older than this value are removed. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of cache entries removed. |