-
Notifications
You must be signed in to change notification settings - Fork 93
Add SemanticCache TTL refresh controls #618
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
Open
omribz156
wants to merge
3
commits into
redis:main
Choose a base branch
from
omribz156:codex/semantic-cache-ttl-refresh-control
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import warnings | ||
| from collections import namedtuple | ||
| from time import sleep, time | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
@@ -393,6 +394,63 @@ def test_ttl_refresh(cache_with_ttl, vectorizer): | |
| assert len(check_result) == 1 | ||
|
|
||
|
|
||
| def test_check_can_skip_ttl_refresh(cache_with_ttl, vectorizer): | ||
| prompt = "This is a test prompt." | ||
| response = "This is a test response." | ||
| vector = vectorizer.embed(prompt) | ||
|
|
||
| cache_with_ttl.store(prompt, response, vector=vector) | ||
|
|
||
| with patch.object(cache_with_ttl, "expire") as expire: | ||
| check_result = cache_with_ttl.check(vector=vector, refresh_ttl=False) | ||
|
|
||
| assert len(check_result) == 1 | ||
| expire.assert_not_called() | ||
|
|
||
|
|
||
| def test_check_respects_cache_ttl_refresh_setting( | ||
| client, vectorizer, redis_url, worker_id | ||
| ): | ||
| skip_if_no_redis_search(client) | ||
| cache_instance = SemanticCache( | ||
| name=f"llmcache_no_refresh_{worker_id}", | ||
| vectorizer=vectorizer, | ||
| distance_threshold=0.2, | ||
| ttl=2, | ||
| redis_url=redis_url, | ||
| refresh_ttl_on_hit=False, | ||
| ) | ||
| prompt = "This is a test prompt." | ||
| response = "This is a test response." | ||
| vector = vectorizer.embed(prompt) | ||
|
|
||
| try: | ||
| cache_instance.store(prompt, response, vector=vector) | ||
|
|
||
| with patch.object(cache_instance, "expire") as expire: | ||
| check_result = cache_instance.check(vector=vector) | ||
| finally: | ||
| cache_instance._index.delete(True) | ||
|
|
||
| assert len(check_result) == 1 | ||
| expire.assert_not_called() | ||
|
|
||
|
|
||
| def test_check_refresh_ttl_override(cache_with_ttl, vectorizer): | ||
| prompt = "This is a test prompt." | ||
| response = "This is a test response." | ||
| vector = vectorizer.embed(prompt) | ||
| cache_with_ttl.refresh_ttl_on_hit = False | ||
|
|
||
| cache_with_ttl.store(prompt, response, vector=vector) | ||
|
|
||
| with patch.object(cache_with_ttl, "expire") as expire: | ||
| check_result = cache_with_ttl.check(vector=vector, refresh_ttl=True) | ||
|
|
||
| assert len(check_result) == 1 | ||
| expire.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_ttl_refresh(cache_with_ttl, vectorizer): | ||
| prompt = "This is a test prompt." | ||
|
|
@@ -409,6 +467,39 @@ async def test_async_ttl_refresh(cache_with_ttl, vectorizer): | |
| assert len(check_result) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_check_can_skip_ttl_refresh(cache_with_ttl, vectorizer): | ||
| prompt = "This is a test prompt." | ||
| response = "This is a test response." | ||
| vector = vectorizer.embed(prompt) | ||
|
|
||
| async with cache_with_ttl: | ||
| await cache_with_ttl.astore(prompt, response, vector=vector) | ||
|
|
||
| with patch.object(cache_with_ttl, "aexpire", new_callable=AsyncMock) as aexpire: | ||
| check_result = await cache_with_ttl.acheck(vector=vector, refresh_ttl=False) | ||
|
|
||
| assert len(check_result) == 1 | ||
| aexpire.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_check_refresh_ttl_override(cache_with_ttl, vectorizer): | ||
| prompt = "This is a test prompt." | ||
| response = "This is a test response." | ||
| vector = vectorizer.embed(prompt) | ||
| cache_with_ttl.refresh_ttl_on_hit = False | ||
|
|
||
| async with cache_with_ttl: | ||
| await cache_with_ttl.astore(prompt, response, vector=vector) | ||
|
|
||
| with patch.object(cache_with_ttl, "aexpire", new_callable=AsyncMock) as aexpire: | ||
| check_result = await cache_with_ttl.acheck(vector=vector, refresh_ttl=True) | ||
|
|
||
| assert len(check_result) == 1 | ||
| aexpire.assert_called_once() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, fixed in 3dfafaa. The async TTL-refresh assertions now use assert_not_awaited() and assert_awaited_once(). Verified with black --check, compileall, and git diff --check. |
||
|
|
||
|
|
||
| # Test manual expiration of single document | ||
| def test_drop_document(cache, vectorizer): | ||
| prompt = "This is a test prompt." | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this class inherits from
BaseLLMCache, you'll want to make the corresponding update on there as well. Otherwise type-checkers will complainThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed in 362ca90. I added the matching refresh_ttl parameter to BaseLLMCache.check() and BaseLLMCache.acheck(), plus the sync docstring note, so SemanticCache stays compatible with the base interface. Verified with python -m compileall on base.py/semantic.py and git diff --check.