From b6f8dbbc0e9559d5095d1400674ed24385b5c3e5 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Thu, 23 Nov 2023 18:17:11 -0800 Subject: [PATCH] (test) caching --- litellm/tests/test_caching.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/litellm/tests/test_caching.py b/litellm/tests/test_caching.py index 432ed89154..c86454a53b 100644 --- a/litellm/tests/test_caching.py +++ b/litellm/tests/test_caching.py @@ -155,28 +155,42 @@ def test_redis_cache_completion(): litellm.set_verbose = False random_number = random.randint(1, 100000) # add a random number to ensure it's always adding / reading from cache - messages = [{"role": "user", "content": f"who is ishaan CTO of litellm, call all llm apis{random_number}"}] + messages = [{"role": "user", "content": f"write a one sentence {random_number}"}] litellm.cache = Cache(type="redis", host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'], password=os.environ['REDIS_PASSWORD']) print("test2 for caching") - response1 = completion(model="gpt-3.5-turbo", messages=messages, caching=True) - response2 = completion(model="gpt-3.5-turbo", messages=messages, caching=True) - response3 = completion(model="command-nightly", messages=messages, caching=True) + response1 = completion(model="gpt-3.5-turbo", messages=messages, caching=True, max_tokens=10, seed=1222) + response2 = completion(model="gpt-3.5-turbo", messages=messages, caching=True, max_tokens=10, seed=1222) + response3 = completion(model="gpt-3.5-turbo", messages=messages, caching=True, temperature=0.1) + response4 = completion(model="command-nightly", messages=messages, caching=True) print("\nresponse 1", response1) print("\nresponse 2", response2) print("\nresponse 3", response3) + print("\nresponse 4", response4) litellm.cache = None - if response3['choices'][0]['message']['content'] == response2['choices'][0]['message']['content']: - # if models are different, it should not return cached response - print(f"response2: {response2}") - print(f"response3: {response3}") - pytest.fail(f"Error occurred:") + + """ + 1 & 2 should be exactly the same + 1 & 3 should be different, since input params are diff + 1 & 4 should be diff, since models are diff + """ if response1['choices'][0]['message']['content'] != response2['choices'][0]['message']['content']: # 1 and 2 should be the same + # 1&2 have the exact same input params. This MUST Be a CACHE HIT print(f"response1: {response1}") print(f"response2: {response2}") pytest.fail(f"Error occurred:") + if response1['choices'][0]['message']['content'] == response3['choices'][0]['message']['content']: + # if input params like seed, max_tokens are diff it should NOT be a cache hit + print(f"response1: {response1}") + print(f"response3: {response3}") + pytest.fail(f"Response 1 == response 3. Same model, diff params shoudl not cache Error occurred:") + if response1['choices'][0]['message']['content'] == response4['choices'][0]['message']['content']: + # if models are different, it should not return cached response + print(f"response1: {response1}") + print(f"response4: {response4}") + pytest.fail(f"Error occurred:") -# test_redis_cache_completion() +test_redis_cache_completion() # redis cache with custom keys def custom_get_cache_key(*args, **kwargs):