Skip to content

Commit 99bc5a1

Browse files
Fix semantic cache with empty metadata (#201)
Fix a bug where `metadata={}` was not getting handled correctly.
1 parent c852e3d commit 99bc5a1

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

redisvl/extensions/llmcache/schema.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def non_empty_metadata(cls, v):
4444
def to_dict(self) -> Dict:
4545
data = self.dict(exclude_none=True)
4646
data["prompt_vector"] = array_to_buffer(self.prompt_vector)
47-
if self.metadata:
47+
if self.metadata is not None:
4848
data["metadata"] = serialize(self.metadata)
49-
if self.filters:
49+
if self.filters is not None:
5050
data.update(self.filters)
5151
del data["filters"]
5252
return data

redisvl/extensions/llmcache/semantic.py

-2
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,6 @@ def check(
300300
key = cache_search_result["id"]
301301
self._refresh_ttl(key)
302302

303-
print(cache_search_result, flush=True)
304-
305303
# Create cache hit
306304
cache_hit = CacheHit(**cache_search_result)
307305
cache_hit_dict = {

tests/integration/test_llmcache.py

+16
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,22 @@ def test_store_with_metadata(cache, vectorizer):
295295
assert check_result[0]["prompt"] == prompt
296296

297297

298+
def test_store_with_empty_metadata(cache, vectorizer):
299+
prompt = "This is another test prompt."
300+
response = "This is another test response."
301+
metadata = {}
302+
vector = vectorizer.embed(prompt)
303+
304+
cache.store(prompt, response, vector=vector, metadata=metadata)
305+
check_result = cache.check(vector=vector, num_results=1)
306+
307+
assert len(check_result) == 1
308+
print(check_result, flush=True)
309+
assert check_result[0]["response"] == response
310+
assert check_result[0]["metadata"] == metadata
311+
assert check_result[0]["prompt"] == prompt
312+
313+
298314
def test_store_with_invalid_metadata(cache, vectorizer):
299315
prompt = "This is another test prompt."
300316
response = "This is another test response."

0 commit comments

Comments
 (0)