From 01adc45f0fe6b4ee3fc5d2e5c0304a89d1afc838 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 24 Aug 2024 09:06:59 -0700 Subject: [PATCH] test caching default on /off --- litellm/tests/test_caching.py | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/litellm/tests/test_caching.py b/litellm/tests/test_caching.py index e474dff2e4..402f3d51f1 100644 --- a/litellm/tests/test_caching.py +++ b/litellm/tests/test_caching.py @@ -1875,3 +1875,83 @@ async def test_qdrant_semantic_cache_acompletion_stream(): except Exception as e: print(f"{str(e)}\n\n{traceback.format_exc()}") raise e + + +@pytest.mark.asyncio() +async def test_cache_default_off_acompletion(): + litellm.set_verbose = True + import logging + + from litellm._logging import verbose_logger + + verbose_logger.setLevel(logging.DEBUG) + + from litellm.caching import CacheMode + + random_number = random.randint( + 1, 100000 + ) # add a random number to ensure it's always adding /reading from cache + litellm.cache = Cache( + type="local", + mode=CacheMode.default_off, + ) + + ### No Cache hits when it's default off + + response1 = await litellm.acompletion( + model="gpt-3.5-turbo", + messages=[ + { + "role": "user", + "content": f"write a one sentence poem about: {random_number}", + } + ], + mock_response="hello", + max_tokens=20, + ) + print(f"Response1: {response1}") + + response2 = await litellm.acompletion( + model="gpt-3.5-turbo", + messages=[ + { + "role": "user", + "content": f"write a one sentence poem about: {random_number}", + } + ], + max_tokens=20, + ) + print(f"Response2: {response2}") + assert response1.id != response2.id + + ## Cache hits when it's default off and then opt in + + response3 = await litellm.acompletion( + model="gpt-3.5-turbo", + messages=[ + { + "role": "user", + "content": f"write a one sentence poem about: {random_number}", + } + ], + mock_response="hello", + cache={"use-cache": True}, + metadata={"key": "value"}, + max_tokens=20, + ) + print(f"Response3: {response3}") + + response4 = await litellm.acompletion( + model="gpt-3.5-turbo", + messages=[ + { + "role": "user", + "content": f"write a one sentence poem about: {random_number}", + } + ], + cache={"use-cache": True}, + metadata={"key": "value"}, + max_tokens=20, + ) + print(f"Response4: {response4}") + assert response3.id == response4.id