Skip to content

Improve caching ttl tests #178

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions redisvl/extensions/llmcache/semantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ def delete(self) -> None:

def _refresh_ttl(self, key: str) -> None:
"""Refresh the time-to-live for the specified key."""
if self.ttl:
self._index.client.expire(key, self.ttl) # type: ignore
if self._ttl:
self._index.client.expire(key, self._ttl) # type: ignore

def _vectorize_prompt(self, prompt: Optional[str]) -> List[float]:
"""Converts a text prompt to its vector representation using the
Expand Down
41 changes: 33 additions & 8 deletions tests/integration/test_llmcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def cache(vectorizer, redis_url):
vectorizer=vectorizer, distance_threshold=0.2, redis_url=redis_url
)
yield cache_instance
cache_instance.clear() # Clear cache after each test
cache_instance._index.delete(True) # Clean up index


Expand All @@ -37,7 +36,6 @@ def cache_with_ttl(vectorizer, redis_url):
vectorizer=vectorizer, distance_threshold=0.2, ttl=2, redis_url=redis_url
)
yield cache_instance
cache_instance.clear() # Clear cache after each test
cache_instance._index.delete(True) # Clean up index


Expand All @@ -54,6 +52,24 @@ def cache_with_redis_client(vectorizer, client, redis_url):
cache_instance._index.delete(True) # Clean up index


# # Test handling invalid input for check method
def test_bad_ttl(cache):
with pytest.raises(ValueError):
cache.set_ttl(2.5)


def test_cache_ttl(cache_with_ttl):
assert cache_with_ttl.ttl == 2
cache_with_ttl.set_ttl(5)
assert cache_with_ttl.ttl == 5


def test_set_ttl(cache):
assert cache.ttl == None
cache.set_ttl(5)
assert cache.ttl == 5


# Test basic store and check functionality
def test_store_and_check(cache, vectorizer):
prompt = "This is a test prompt."
Expand Down Expand Up @@ -95,6 +111,21 @@ def test_ttl_expiration(cache_with_ttl, vectorizer):
assert len(check_result) == 0


def test_ttl_expiration_after_update(cache_with_ttl, vectorizer):
prompt = "This is a test prompt."
response = "This is a test response."
vector = vectorizer.embed(prompt)
cache_with_ttl.set_ttl(4)

assert cache_with_ttl.ttl == 4

cache_with_ttl.store(prompt, response, vector=vector)
sleep(5)

check_result = cache_with_ttl.check(vector=vector)
assert len(check_result) == 0


# Test check behavior with no match
def test_check_no_match(cache, vectorizer):
vector = vectorizer.embed("Some random sentence.")
Expand All @@ -111,12 +142,6 @@ def test_check_invalid_input(cache):
cache.check(prompt="test", return_fields="bad value")


# Test handling invalid input for check method
def test_bad_ttl(cache):
with pytest.raises(ValueError):
cache.set_ttl(2.5)


# Test storing with metadata
def test_store_with_metadata(cache, vectorizer):
prompt = "This is another test prompt."
Expand Down
Loading