@@ -64,12 +64,16 @@ class MockRedisVLSemanticCache:
64
64
def __init__ (self ) -> None :
65
65
self .data : Dict [tuple , List [Dict [str , Any ]]] = {}
66
66
self .distance_threshold : float = 0.2 # Default value
67
+ self .index = Mock ()
68
+ self .index .name = "test_index"
67
69
68
70
def check (self , vector : List [float ]) -> List [Dict [str , Any ]]:
69
71
for stored_vector , stored_data in self .data .items ():
70
72
distance = np .linalg .norm (np .array (vector ) - np .array (stored_vector ))
73
+ # Important: The distance check is now more explicit about threshold
71
74
if distance <= self .distance_threshold :
72
75
return stored_data
76
+ # If threshold is not met, return empty list
73
77
return []
74
78
75
79
def store (
@@ -86,7 +90,39 @@ def clear(self) -> None:
86
90
87
91
def _vectorize_prompt (self , prompt : str ) -> List [float ]:
88
92
# 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 )
90
126
91
127
92
128
class TestRedisCache :
@@ -196,9 +232,10 @@ def redis_semantic_cache(self, mock_embeddings: Mock) -> RedisSemanticCache:
196
232
"langchain_redis.cache.RedisVLSemanticCache" ,
197
233
return_value = MockRedisVLSemanticCache (),
198
234
):
199
- return RedisSemanticCache (
235
+ cache = RedisSemanticCache (
200
236
embeddings = mock_embeddings , redis_url = "redis://localhost:6379"
201
237
)
238
+ return cache
202
239
203
240
def test_update (self , redis_semantic_cache : RedisSemanticCache ) -> None :
204
241
prompt = "test prompt"
0 commit comments