diff --git a/litellm/litellm_core_utils/get_httpx_clients.py b/litellm/litellm_core_utils/get_httpx_clients.py deleted file mode 100644 index afb48af57ad..00000000000 --- a/litellm/litellm_core_utils/get_httpx_clients.py +++ /dev/null @@ -1,62 +0,0 @@ -from typing import Optional -import litellm -from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler -from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler -import httpx - - -def _get_async_httpx_client(params: Optional[dict] = None) -> AsyncHTTPHandler: - """ - Retrieves the async HTTP client from the cache - If not present, creates a new client - - Caches the new client and returns it. - """ - _params_key_name = "" - if params is not None: - for key, value in params.items(): - try: - _params_key_name += f"{key}_{value}" - except Exception: - pass - - _cache_key_name = "async_httpx_client" + _params_key_name - if _cache_key_name in litellm.in_memory_llm_clients_cache: - return litellm.in_memory_llm_clients_cache[_cache_key_name] - - if params is not None: - _new_client = AsyncHTTPHandler(**params) - else: - _new_client = AsyncHTTPHandler( - timeout=httpx.Timeout(timeout=600.0, connect=5.0) - ) - litellm.in_memory_llm_clients_cache[_cache_key_name] = _new_client - return _new_client - - -def _get_httpx_client(params: Optional[dict] = None) -> HTTPHandler: - """ - Retrieves the HTTP client from the cache - If not present, creates a new client - - Caches the new client and returns it. - """ - _params_key_name = "" - if params is not None: - for key, value in params.items(): - try: - _params_key_name += f"{key}_{value}" - except Exception: - pass - - _cache_key_name = "httpx_client" + _params_key_name - if _cache_key_name in litellm.in_memory_llm_clients_cache: - return litellm.in_memory_llm_clients_cache[_cache_key_name] - - if params is not None: - _new_client = HTTPHandler(**params) - else: - _new_client = HTTPHandler(timeout=httpx.Timeout(timeout=600.0, connect=5.0)) - - litellm.in_memory_llm_clients_cache[_cache_key_name] = _new_client - return _new_client diff --git a/litellm/llms/anthropic.py b/litellm/llms/anthropic.py index c01a704600d..236f7cd4f8b 100644 --- a/litellm/llms/anthropic.py +++ b/litellm/llms/anthropic.py @@ -8,8 +8,11 @@ from typing import Callable, Optional, List, Union from litellm.utils import ModelResponse, Usage, map_finish_reason, CustomStreamWrapper import litellm from .prompt_templates.factory import prompt_factory, custom_prompt -from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler -from litellm.litellm_core_utils.get_httpx_clients import _get_async_httpx_client +from litellm.llms.custom_httpx.http_handler import ( + AsyncHTTPHandler, + _get_async_httpx_client, + _get_httpx_client, +) from .base import BaseLLM import httpx # type: ignore from litellm.types.llms.anthropic import AnthropicMessagesToolChoice diff --git a/litellm/llms/bedrock_httpx.py b/litellm/llms/bedrock_httpx.py index 6752204088c..7c7210f84cf 100644 --- a/litellm/llms/bedrock_httpx.py +++ b/litellm/llms/bedrock_httpx.py @@ -41,8 +41,9 @@ from .prompt_templates.factory import ( _bedrock_converse_messages_pt, _bedrock_tools_pt, ) -from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler -from litellm.litellm_core_utils.get_httpx_clients import ( +from litellm.llms.custom_httpx.http_handler import ( + AsyncHTTPHandler, + HTTPHandler, _get_async_httpx_client, _get_httpx_client, ) diff --git a/litellm/llms/custom_httpx/http_handler.py b/litellm/llms/custom_httpx/http_handler.py index f0a5163f39d..a3c5865fa37 100644 --- a/litellm/llms/custom_httpx/http_handler.py +++ b/litellm/llms/custom_httpx/http_handler.py @@ -219,3 +219,60 @@ class HTTPHandler: self.close() except Exception: pass + + +def _get_async_httpx_client(params: Optional[dict] = None) -> AsyncHTTPHandler: + """ + Retrieves the async HTTP client from the cache + If not present, creates a new client + + Caches the new client and returns it. + """ + _params_key_name = "" + if params is not None: + for key, value in params.items(): + try: + _params_key_name += f"{key}_{value}" + except Exception: + pass + + _cache_key_name = "async_httpx_client" + _params_key_name + if _cache_key_name in litellm.in_memory_llm_clients_cache: + return litellm.in_memory_llm_clients_cache[_cache_key_name] + + if params is not None: + _new_client = AsyncHTTPHandler(**params) + else: + _new_client = AsyncHTTPHandler( + timeout=httpx.Timeout(timeout=600.0, connect=5.0) + ) + litellm.in_memory_llm_clients_cache[_cache_key_name] = _new_client + return _new_client + + +def _get_httpx_client(params: Optional[dict] = None) -> HTTPHandler: + """ + Retrieves the HTTP client from the cache + If not present, creates a new client + + Caches the new client and returns it. + """ + _params_key_name = "" + if params is not None: + for key, value in params.items(): + try: + _params_key_name += f"{key}_{value}" + except Exception: + pass + + _cache_key_name = "httpx_client" + _params_key_name + if _cache_key_name in litellm.in_memory_llm_clients_cache: + return litellm.in_memory_llm_clients_cache[_cache_key_name] + + if params is not None: + _new_client = HTTPHandler(**params) + else: + _new_client = HTTPHandler(timeout=httpx.Timeout(timeout=600.0, connect=5.0)) + + litellm.in_memory_llm_clients_cache[_cache_key_name] = _new_client + return _new_client