Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions basedpyright-code-budget.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"reportAny": {
"limit": 29204
"limit": 28842
},
"reportArgumentType": {
"limit": 2635
Expand All @@ -24,7 +24,7 @@
"limit": 19
},
"reportExplicitAny": {
"limit": 9227
"limit": 9105
},
"reportFunctionMemberAccess": {
"limit": 7
Expand Down Expand Up @@ -54,10 +54,10 @@
"limit": 0
},
"reportMissingParameterType": {
"limit": 5850
"limit": 5843
},
"reportMissingTypeArgument": {
"limit": 15833
"limit": 15816
},
"reportMissingTypeStubs": {
"limit": 40
Expand Down Expand Up @@ -99,19 +99,19 @@
"limit": 0
},
"reportUnknownArgumentType": {
"limit": 45242
"limit": 45207
},
"reportUnknownLambdaType": {
"limit": 113
},
"reportUnknownMemberType": {
"limit": 40340
"limit": 40297
},
"reportUnknownParameterType": {
"limit": 20293
"limit": 20272
},
"reportUnknownVariableType": {
"limit": 31796
"limit": 31750
},
"reportUnnecessaryCast": {
"limit": 122
Expand Down
33 changes: 17 additions & 16 deletions litellm/caching/redis_semantic_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import asyncio
import json
import os
from collections.abc import Callable, Mapping
from typing import Any, Final, cast

import litellm
Expand Down Expand Up @@ -47,7 +48,7 @@ def __init__(
similarity_threshold: float | None = None,
embedding_model: str = "text-embedding-ada-002",
index_name: str | None = None,
**kwargs,
**kwargs: object,
):
"""
Initialize the Redis Semantic Cache.
Expand Down Expand Up @@ -150,11 +151,11 @@ def _cache_key_filterable_field(cls) -> dict[str, str]:

def _init_semantic_cache(
self,
semantic_cache_cls: Any,
semantic_cache_cls: Callable[..., object],
index_name: str,
redis_url: str,
cache_vectorizer: Any,
) -> Any:
cache_vectorizer: object,
) -> object:
def _is_schema_mismatch(exc: ValueError) -> bool:
error_message: Final = str(exc).lower()
return any(phrase in error_message for phrase in ("schema does not match", "index schema"))
Expand Down Expand Up @@ -206,12 +207,12 @@ def _is_schema_mismatch(exc: ValueError) -> bool:
def _get_cache_filters(self, key: str) -> dict[str, str]:
return {self.CACHE_KEY_FIELD_NAME: str(key)}

def _get_cache_key_filter_expression(self, key: str) -> Any:
def _get_cache_key_filter_expression(self, key: str) -> object:
from redisvl.query.filter import Tag

return Tag(self.CACHE_KEY_FIELD_NAME) == str(key)

def _cache_hit_matches_key(self, cache_hit: dict[str, Any], key: str) -> bool:
def _cache_hit_matches_key(self, cache_hit: Mapping[str, object], key: str) -> bool:
# Pre-isolation entries with no ``litellm_cache_key`` field cannot be
# safely reassigned to a caller's scope and are treated as misses.
cached_key = cache_hit.get(self.CACHE_KEY_FIELD_NAME)
Expand Down Expand Up @@ -297,7 +298,7 @@ def _collect_responses_input_text(cls, value: Any, prompt_parts: list[str]) -> N
return

@staticmethod
def _coerce_response_input_value(value: Any) -> Any:
def _coerce_response_input_value(value: object) -> object:
model_dump: Final = getattr(value, "model_dump", None)
if callable(model_dump):
return model_dump()
Expand Down Expand Up @@ -340,7 +341,7 @@ def _get_embedding(self, prompt: str, metadata: dict[str, Any] | None = None) ->
)
return embedding_response["data"][0]["embedding"]

def _get_cache_logic(self, cached_response: Any) -> Any:
def _get_cache_logic(self, cached_response: Any) -> object:
"""
Process the cached response to prepare it for use.

Expand Down Expand Up @@ -369,7 +370,7 @@ def _get_cache_logic(self, cached_response: Any) -> Any:

return cached_response

def set_cache(self, key: str, value: Any, **kwargs) -> None:
def set_cache(self, key: str, value: object, **kwargs) -> None:
"""
Store a value in the semantic cache.

Expand Down Expand Up @@ -405,7 +406,7 @@ def set_cache(self, key: str, value: Any, **kwargs) -> None:
except Exception as e:
print_verbose(f"Error setting {value_str or value} in the Redis semantic cache: {e}")

def get_cache(self, key: str, **kwargs) -> Any:
def get_cache(self, key: str, **kwargs) -> object:
"""
Retrieve a semantically similar cached response.

Expand All @@ -428,7 +429,7 @@ def get_cache(self, key: str, **kwargs) -> Any:
# Check the cache for semantically similar prompts in this exact
# LiteLLM cache-key scope.
prompt_embedding: Final = self._get_embedding(prompt, metadata=kwargs.get("metadata"))
check_kwargs: Final[dict[str, Any]] = {
check_kwargs: Final[Mapping[str, object]] = {
"prompt": prompt,
"vector": prompt_embedding,
"filter_expression": self._get_cache_key_filter_expression(key),
Expand Down Expand Up @@ -508,7 +509,7 @@ async def _get_async_embedding(self, prompt: str, metadata: dict[str, Any] | Non
print_verbose(f"Error generating async embedding: {e}")
raise ValueError(f"Failed to generate embedding: {e}") from e

async def async_set_cache(self, key: str, value: Any, **kwargs) -> None:
async def async_set_cache(self, key: str, value: object, **kwargs) -> None:
"""
Asynchronously store a value in the semantic cache.

Expand Down Expand Up @@ -548,7 +549,7 @@ async def async_set_cache(self, key: str, value: Any, **kwargs) -> None:
except Exception as e:
print_verbose(f"Error in async_set_cache: {e}")

async def async_get_cache(self, key: str, **kwargs) -> Any:
async def async_get_cache(self, key: str, **kwargs) -> object:
"""
Asynchronously retrieve a semantically similar cached response.

Expand All @@ -573,7 +574,7 @@ async def async_get_cache(self, key: str, **kwargs) -> Any:

# Check the cache for semantically similar prompts in this exact
# LiteLLM cache-key scope.
check_kwargs: Final[dict[str, Any]] = {
check_kwargs: Final[Mapping[str, object]] = {
"prompt": prompt,
"vector": prompt_embedding,
"filter_expression": self._get_cache_key_filter_expression(key),
Expand Down Expand Up @@ -615,7 +616,7 @@ async def async_get_cache(self, key: str, **kwargs) -> Any:
print_verbose(f"Error in async_get_cache: {e}")
kwargs.setdefault("metadata", {})["semantic-similarity"] = 0.0

async def _index_info(self) -> dict[str, Any]:
async def _index_info(self) -> Mapping[str, object]:
"""
Get information about the Redis index.

Expand All @@ -625,7 +626,7 @@ async def _index_info(self) -> dict[str, Any]:
aindex: Final = await self.llmcache._get_async_index()
return await aindex.info()

async def async_set_cache_pipeline(self, cache_list: list[tuple[str, Any]], **kwargs) -> None:
async def async_set_cache_pipeline(self, cache_list: list[tuple[str, Any]], **kwargs: object) -> None:
"""
Asynchronously store multiple values in the semantic cache.

Expand Down
Loading
Loading