Skip to content

Commit c3a947b

Browse files
committed
fix: update RedisVL semantic cache import to fix deprecation warning
Updated import from 'redisvl.extensions.llmcache' to 'redisvl.extensions.cache.llm' to address deprecation warning. Adjusted mock implementation in unit tests to ensure compatibility with new module.
1 parent 57095c9 commit c3a947b

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

libs/redis/langchain_redis/cache.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from redis import Redis
1717
from redis.commands.json.path import Path
1818
from redis.exceptions import ResponseError
19-
from redisvl.extensions.llmcache import ( # type: ignore[import]
19+
from redisvl.extensions.cache.llm import ( # type: ignore[import]
2020
SemanticCache as RedisVLSemanticCache,
2121
)
2222
from redisvl.schema.fields import VectorDataType # type: ignore[import]

libs/redis/tests/unit_tests/test_cache.py

+39-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ class MockRedisVLSemanticCache:
6464
def __init__(self) -> None:
6565
self.data: Dict[tuple, List[Dict[str, Any]]] = {}
6666
self.distance_threshold: float = 0.2 # Default value
67+
self.index = Mock()
68+
self.index.name = "test_index"
6769

6870
def check(self, vector: List[float]) -> List[Dict[str, Any]]:
6971
for stored_vector, stored_data in self.data.items():
7072
distance = np.linalg.norm(np.array(vector) - np.array(stored_vector))
73+
# Important: The distance check is now more explicit about threshold
7174
if distance <= self.distance_threshold:
7275
return stored_data
76+
# If threshold is not met, return empty list
7377
return []
7478

7579
def store(
@@ -86,7 +90,39 @@ def clear(self) -> None:
8690

8791
def _vectorize_prompt(self, prompt: str) -> List[float]:
8892
# Simple mock implementation, returns different vectors for different prompts
89-
return [hash(prompt) % 10 * 0.1, hash(prompt) % 7 * 0.1, hash(prompt) % 5 * 0.1]
93+
# Make sure vectors are different enough to test distance threshold
94+
if prompt == "test prompt 1":
95+
return [0.1, 0.2, 0.3]
96+
elif prompt == "test prompt 2":
97+
return [0.5, 0.6, 0.7] # More than 0.1 distance from first vector
98+
else:
99+
return [
100+
hash(prompt) % 10 * 0.1,
101+
hash(prompt) % 7 * 0.1,
102+
hash(prompt) % 5 * 0.1,
103+
]
104+
105+
def acheck(self, vector: List[float]) -> List[Dict[str, Any]]:
106+
# Async version with same behavior
107+
return self.check(vector)
108+
109+
async def astore(
110+
self,
111+
prompt: str,
112+
response: str,
113+
vector: List[float],
114+
metadata: Optional[Dict[str, Any]] = None,
115+
) -> None:
116+
# Async version with same behavior
117+
self.store(prompt, response, vector, metadata)
118+
119+
async def aclear(self) -> None:
120+
# Async version with same behavior
121+
self.clear()
122+
123+
async def _avectorize_prompt(self, prompt: str) -> List[float]:
124+
# Async version with same behavior
125+
return self._vectorize_prompt(prompt)
90126

91127

92128
class TestRedisCache:
@@ -196,9 +232,10 @@ def redis_semantic_cache(self, mock_embeddings: Mock) -> RedisSemanticCache:
196232
"langchain_redis.cache.RedisVLSemanticCache",
197233
return_value=MockRedisVLSemanticCache(),
198234
):
199-
return RedisSemanticCache(
235+
cache = RedisSemanticCache(
200236
embeddings=mock_embeddings, redis_url="redis://localhost:6379"
201237
)
238+
return cache
202239

203240
def test_update(self, redis_semantic_cache: RedisSemanticCache) -> None:
204241
prompt = "test prompt"

0 commit comments

Comments
 (0)