diff --git a/litellm/__init__.py b/litellm/__init__.py index d47c8e7b873..f81a9093a6e 100644 --- a/litellm/__init__.py +++ b/litellm/__init__.py @@ -15,7 +15,7 @@ openrouter_key = None huggingface_key = None vertex_project = None vertex_location = None - +caching = False hugging_api_token = None model_cost = { "gpt-3.5-turbo": {"max_tokens": 4000, "input_cost_per_token": 0.0000015, "output_cost_per_token": 0.000002}, diff --git a/litellm/__pycache__/__init__.cpython-311.pyc b/litellm/__pycache__/__init__.cpython-311.pyc index 07167d14986..f76206ecdc8 100644 Binary files a/litellm/__pycache__/__init__.cpython-311.pyc and b/litellm/__pycache__/__init__.cpython-311.pyc differ diff --git a/litellm/__pycache__/main.cpython-311.pyc b/litellm/__pycache__/main.cpython-311.pyc index d2da312cb36..f0af40629ad 100644 Binary files a/litellm/__pycache__/main.cpython-311.pyc and b/litellm/__pycache__/main.cpython-311.pyc differ diff --git a/litellm/__pycache__/utils.cpython-311.pyc b/litellm/__pycache__/utils.cpython-311.pyc index c8658d0785a..fe9983c5b45 100644 Binary files a/litellm/__pycache__/utils.cpython-311.pyc and b/litellm/__pycache__/utils.cpython-311.pyc differ diff --git a/litellm/tests/test_caching.py b/litellm/tests/test_caching.py new file mode 100644 index 00000000000..895e9e0fac4 --- /dev/null +++ b/litellm/tests/test_caching.py @@ -0,0 +1,27 @@ +import sys, os +import traceback +from dotenv import load_dotenv +load_dotenv() +import os +sys.path.insert(0, os.path.abspath('../..')) # Adds the parent directory to the system path +import pytest +import litellm +from litellm import embedding, completion + +litellm.caching = True +messages = [{"role": "user", "content": "Hey, how's it going?"}] + + + +# test if response cached +try: + response1 = completion(model="gpt-3.5-turbo", messages=messages) + response2 = completion(model="gpt-3.5-turbo", messages=messages) + if response2 != response1: + print(f"response1: {response1}") + print(f"response2: {response2}") + raise Exception +except Exception as e: + print(f"error occurred: {traceback.format_exc()}") + pytest.fail(f"Error occurred: {e}") + diff --git a/litellm/tests/test_completion.py b/litellm/tests/test_completion.py index 5bd78b33f70..8c094613140 100644 --- a/litellm/tests/test_completion.py +++ b/litellm/tests/test_completion.py @@ -213,7 +213,7 @@ def test_completion_together_ai_stream(): except Exception as e: pytest.fail(f"Error occurred: {e}") - +test_completion_together_ai_stream() def test_petals(): model_name = "stabilityai/StableBeluga2" try: diff --git a/litellm/utils.py b/litellm/utils.py index 545138313b1..6d828b26a9a 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -28,6 +28,7 @@ supabaseClient = None callback_list = [] user_logger_fn = None additional_details = {} +local_cache = {} def print_verbose(print_statement): if litellm.set_verbose: @@ -138,12 +139,22 @@ def client(original_function): def wrapper(*args, **kwargs): start_time = None + result = None try: function_setup(*args, **kwargs) ## MODEL CALL start_time = datetime.datetime.now() - result = original_function(*args, **kwargs) + ## CHECK CACHE RESPONSES + messages = args[1] if len(args) > 1 else kwargs["messages"] + prompt = " ".join(message["content"] for message in messages) + if litellm.caching and prompt in local_cache: + result = local_cache[prompt] + else: + result = original_function(*args, **kwargs) end_time = datetime.datetime.now() + ## CACHE RESPONSES + if litellm.caching: + local_cache[prompt] = result ## LOG SUCCESS crash_reporting(*args, **kwargs) my_thread = threading.Thread(target=handle_success, args=(args, kwargs, result, start_time, end_time)) # don't interrupt execution of main thread