From 0c26404cb3ce8915d1baae9812ed993c2462c10f Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 10 Apr 2024 16:44:40 -0700 Subject: [PATCH 1/2] fix - allow base64 cache hits embedding responses --- litellm/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index eee7f8202e9..7ce98174cf9 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -236,7 +236,7 @@ class HiddenParams(OpenAIObject): class Config: extra = "allow" - protected_namespaces = () + protected_namespaces = () def get(self, key, default=None): # Custom .get() method to access attributes with a default value if the attribute doesn't exist @@ -606,7 +606,7 @@ class ModelResponse(OpenAIObject): class Embedding(OpenAIObject): - embedding: list = [] + embedding: Union[list, str] = [] index: int object: str From 8bc02b34c2cea40c5e3418fb94f380489744a09d Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 10 Apr 2024 16:46:56 -0700 Subject: [PATCH 2/2] test -base64 cache hits --- .../tests/test_openai_simple_embedding.py | 10 ++++++ litellm/tests/test_caching.py | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 litellm/proxy/tests/test_openai_simple_embedding.py diff --git a/litellm/proxy/tests/test_openai_simple_embedding.py b/litellm/proxy/tests/test_openai_simple_embedding.py new file mode 100644 index 00000000000..7dd38c0b396 --- /dev/null +++ b/litellm/proxy/tests/test_openai_simple_embedding.py @@ -0,0 +1,10 @@ +import openai + +client = openai.OpenAI(api_key="sk-1234", base_url="http://0.0.0.0:4000") + +# # request sent to model set on litellm proxy, `litellm --model` +response = client.embeddings.create( + model="text-embedding-ada-002", input=["test"], encoding_format="base64" +) + +print(response) diff --git a/litellm/tests/test_caching.py b/litellm/tests/test_caching.py index 835d3611b83..2c3c863de5f 100644 --- a/litellm/tests/test_caching.py +++ b/litellm/tests/test_caching.py @@ -387,6 +387,41 @@ async def test_embedding_caching_azure_individual_items_reordered(): assert embedding_val_1.data[1]["index"] == 1 +@pytest.mark.asyncio +async def test_embedding_caching_base_64(): + """ """ + litellm.cache = Cache( + type="redis", + host=os.environ["REDIS_HOST"], + port=os.environ["REDIS_PORT"], + ) + import uuid + + inputs = [ + f"{uuid.uuid4()} hello this is ishaan", + f"{uuid.uuid4()} hello this is ishaan again", + ] + + embedding_val_1 = await aembedding( + model="azure/azure-embedding-model", + input=inputs, + caching=True, + encoding_format="base64", + ) + embedding_val_2 = await aembedding( + model="azure/azure-embedding-model", + input=inputs, + caching=True, + encoding_format="base64", + ) + + assert embedding_val_2._hidden_params["cache_hit"] == True + print(embedding_val_2) + print(embedding_val_1) + assert embedding_val_2.data[0]["embedding"] == embedding_val_1.data[0]["embedding"] + assert embedding_val_2.data[1]["embedding"] == embedding_val_1.data[1]["embedding"] + + @pytest.mark.asyncio async def test_redis_cache_basic(): """