From 47dd52c5666dc6fcbbbd5bd47993507e24acc9f2 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 1 Jun 2024 09:24:16 -0700 Subject: [PATCH] fix used hashed api key --- litellm/llms/openai.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/litellm/llms/openai.py b/litellm/llms/openai.py index c560cd3b9d2..c72b404c379 100644 --- a/litellm/llms/openai.py +++ b/litellm/llms/openai.py @@ -6,6 +6,7 @@ from typing import ( Literal, Iterable, ) +import hashlib from typing_extensions import override, overload from pydantic import BaseModel import types, time, json, traceback @@ -27,7 +28,6 @@ from .prompt_templates.factory import prompt_factory, custom_prompt from openai import OpenAI, AsyncOpenAI from ..types.llms.openai import * import openai -from functools import lru_cache class OpenAIError(Exception): @@ -524,8 +524,15 @@ class OpenAIChatCompletion(BaseLLM): ), ) # Creating a new OpenAI Client - # check in memory cache before doing so - _cache_key = f"api_key={api_key},api_base={api_base},timeout={timeout},max_retries={max_retries},organization={organization}" + # check in memory cache before creating a new one + # Convert the API key to bytes + hashed_api_key = None + if api_key is not None: + hash_object = hashlib.sha256(api_key.encode()) + # Hexadecimal representation of the hash + hashed_api_key = hash_object.hexdigest() + + _cache_key = f"hashed_api_key={hashed_api_key},api_base={api_base},timeout={timeout},max_retries={max_retries},organization={organization}" if _cache_key in litellm.in_memory_llm_clients_cache: return litellm.in_memory_llm_clients_cache[_cache_key]