Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Litellm use lru cache #7698

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions litellm/caching/_internal_lru_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from functools import lru_cache, wraps
from typing import Callable, TypeVar, cast

RT = TypeVar("RT") # Return type


def typed_lru_cache(maxsize: int = 128) -> Callable:
def decorator(func: Callable[..., RT]) -> Callable[..., RT]:
wrapped = lru_cache(maxsize=maxsize)(func)
return cast(Callable[..., RT], wraps(func)(wrapped))

return decorator
27 changes: 26 additions & 1 deletion litellm/litellm_core_utils/get_llm_provider_logic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Optional, Tuple
from functools import lru_cache
from typing import Optional, Tuple, overload

import httpx

import litellm
from litellm.caching._internal_lru_cache import typed_lru_cache
from litellm.secret_managers.main import get_secret, get_secret_str

from ..types.router import LiteLLM_Params
Expand Down Expand Up @@ -84,6 +86,29 @@ def handle_anthropic_text_model_custom_llm_provider(
return model, custom_llm_provider


@overload
def get_llm_provider(
model: str,
custom_llm_provider: str,
api_base: str,
api_key: str,
litellm_params: LiteLLM_Params,
) -> Tuple[str, str, str, str]:
pass


@overload
def get_llm_provider(
model: str,
custom_llm_provider: Optional[str] = None,
api_base: Optional[str] = None,
api_key: Optional[str] = None,
litellm_params: Optional[LiteLLM_Params] = None,
) -> Tuple[str, str, Optional[str], Optional[str]]:
pass


@lru_cache(maxsize=16)
def get_llm_provider( # noqa: PLR0915
model: str,
custom_llm_provider: Optional[str] = None,
Expand Down
1 change: 1 addition & 0 deletions litellm/litellm_core_utils/get_supported_openai_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import litellm
from litellm import LlmProviders
from litellm.caching._internal_lru_cache import typed_lru_cache
from litellm.exceptions import BadRequestError


Expand Down
4 changes: 2 additions & 2 deletions litellm/types/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class GenericLiteLLMParams(BaseModel):
max_budget: Optional[float] = None
budget_duration: Optional[str] = None

model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True)
model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True, frozen=True)

def __init__(
self,
Expand Down Expand Up @@ -249,7 +249,7 @@ class LiteLLM_Params(GenericLiteLLMParams):
"""

model: str
model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True)
model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True, frozen=True)

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion tests/llm_translation/test_xai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system-path
) # Adds the parent directory to the system path


import httpx
Expand Down
Loading