DSPy: The framework for programming—not prompting—language models
Python
Move litellm cache testing under tests/client and remove unnecessary tests (#8396)
Chen Qian7b12fd4a1594
diff --git a/tests/caching/example_cache/cache.db b/tests/caching/example_cache/cache.dbdeleted file mode 100644index 47ea29f9f..000000000Binary files a/tests/caching/example_cache/cache.db and /dev/null differdiff --git a/tests/caching/test_litellm_cache.py b/tests/clients/test_litellm_cache.pysimilarity index 59%rename from tests/caching/test_litellm_cache.pyrename to tests/clients/test_litellm_cache.pyindex 0bf881ed7..9677be40b 100644--- a/tests/caching/test_litellm_cache.py+++ b/tests/clients/test_litellm_cache.py@@ -1,5 +1,4 @@import importlib-import osimport shutilimport tempfilefrom unittest.mock import patch@@ -20,24 +19,6 @@ def temporary_blank_cache_dir(monkeypatch):dspy.configure_cache(enable_memory_cache=True, enable_disk_cache=True, enable_litellm_cache=False)-def temporary_populated_cache_dir(monkeypatch):- """- A DSPy cache directory populated with a response for the request with text "Example query"- to the model "openai/dspy-test-model".- """- module_dir = os.path.dirname(os.path.abspath(__file__))- populated_cache_path = os.path.join(module_dir, "example_cache")-- with tempfile.TemporaryDirectory() as cache_dir_path:- shutil.copytree(populated_cache_path, cache_dir_path, dirs_exist_ok=True)- monkeypatch.setenv("DSPY_CACHEDIR", cache_dir_path)- importlib.reload(dspy.clients)- dspy.configure_cache(enable_memory_cache=True, enable_disk_cache=False, enable_litellm_cache=True)- yield cache_dir_path- dspy.configure_cache(enable_memory_cache=True, enable_disk_cache=True, enable_litellm_cache=False)--def test_lm_calls_are_cached_across_lm_instances(litellm_test_server, temporary_blank_cache_dir):api_base, server_log_file_path = litellm_test_server@@ -77,25 +58,6 @@ def test_lm_calls_are_cached_across_lm_instances(litellm_test_server, temporary_assert len(request_logs) == 3-def test_lm_calls_are_cached_across_interpreter_sessions(litellm_test_server, temporary_populated_cache_dir):- """- Verifies that LM calls are cached across interpreter sessions. Pytest test cases effectively- simulate separate interpreter sessions.- """- api_base, server_log_file_path = litellm_test_server-- lm1 = dspy.LM(- model="openai/dspy-test-model",- api_base=api_base,- api_key="fakekey",- max_tokens=1000,- )- lm1("Example query")-- request_logs = read_litellm_test_server_request_logs(server_log_file_path)- assert len(request_logs) == 0--def test_lm_calls_are_cached_in_memory_when_expected(litellm_test_server, temporary_blank_cache_dir):api_base, server_log_file_path = litellm_test_server@@ -132,31 +94,6 @@ def test_lm_calls_skip_in_memory_cache_if_key_not_computable():assert mock_litellm_completion.call_count == 2-# def test_lm_calls_with_callables_are_cached_as_expected():-# with patch("litellm.completion") as mock_completion:-# lm_with_callable = dspy.LM(-# model="openai/dspy-test-model",-# api_base="fakebase",-# api_key="fakekey",-# # Define a callable kwarg for the LM to use during inference-# azure_ad_token_provider=lambda *args, **kwargs: None,-# )-# # Invoke the LM twice; the second call should be cached in memory-# lm_with_callable("Query")-# lm_with_callable("Query")--# # Define and invoke a nearly-identical LM that lacks the callable kwarg,-# # which should not hit the in-memory cache-# lm_without_callable = dspy.LM(-# model="openai/dspy-test-model",-# api_base="fakebase",-# api_key="fakekey",-# )-# lm_without_callable("Query")--# assert mock_completion.call_count == 2--def test_lms_called_expected_number_of_times_for_cache_key_generation_failures():with pytest.raises(RuntimeError), patch("litellm.completion") as mock_completion:mock_completion.side_effect = RuntimeError("Mocked exception")