From c6799e8aad394739737fdb591de9f2e54cd9107c Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Thu, 8 Aug 2024 08:03:08 -0700 Subject: [PATCH] fix use get_file_check_sum --- litellm/litellm_core_utils/core_helpers.py | 22 +++++++++++++++++++++- litellm/utils.py | 13 ++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 8148147ef9..a9e5353167 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -1,6 +1,9 @@ # What is this? ## Helper utilities -from typing import List, Literal, Optional, Tuple +import os +from typing import BinaryIO, List, Literal, Optional, Tuple + +from litellm._logging import verbose_logger def map_finish_reason( @@ -83,3 +86,20 @@ def _get_parent_otel_span_from_kwargs(kwargs: Optional[dict] = None): return kwargs["litellm_parent_otel_span"] except: return None + + +def get_file_check_sum(_file: BinaryIO): + """ + Helper to safely get file checksum - used as a cache key + """ + try: + file_descriptor = _file.fileno() + file_stat = os.fstat(file_descriptor) + file_size = str(file_stat.st_size) + file_checksum = _file.name + file_size + return file_checksum + except Exception as e: + verbose_logger.error(f"Error getting file_checksum: {(str(e))}") + file_checksum = _file.name + return file_checksum + return file_checksum diff --git a/litellm/utils.py b/litellm/utils.py index a20e961727..35596c784c 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -55,7 +55,10 @@ import litellm._service_logger # for storing API inputs, outputs, and metadata import litellm.litellm_core_utils import litellm.litellm_core_utils.json_validation_rule from litellm.caching import DualCache -from litellm.litellm_core_utils.core_helpers import map_finish_reason +from litellm.litellm_core_utils.core_helpers import ( + get_file_check_sum, + map_finish_reason, +) from litellm.litellm_core_utils.exception_mapping_utils import get_error_message from litellm.litellm_core_utils.llm_request_utils import _ensure_extra_body_is_safe from litellm.litellm_core_utils.redact_messages import ( @@ -557,12 +560,8 @@ def function_setup( or call_type == CallTypes.transcription.value ): _file_name: BinaryIO = args[1] if len(args) > 1 else kwargs["file"] - file_name = getattr(_file_name, "name", "audio_file") - file_descriptor = _file_name.fileno() - file_stat = os.fstat(file_descriptor) - file_size = str(file_stat.st_size) - - file_checksum = _file_name.name + file_size + file_checksum = get_file_check_sum(_file=_file_name) + file_name = _file_name.name if "metadata" in kwargs: kwargs["metadata"]["file_checksum"] = file_checksum else: