diff --git a/litellm/litellm_core_utils/llm_request_utils.py b/litellm/litellm_core_utils/llm_request_utils.py index 8cfa48e937d..50dbdc5536e 100644 --- a/litellm/litellm_core_utils/llm_request_utils.py +++ b/litellm/litellm_core_utils/llm_request_utils.py @@ -3,89 +3,66 @@ from typing import Dict, Optional import litellm -class LitellmCoreRequestUtils: +def _ensure_extra_body_is_safe(extra_body: Optional[Dict]) -> Optional[Dict]: + """ + Ensure that the extra_body sent in the request is safe, otherwise users will see this error - @staticmethod - def _ensure_extra_body_is_safe(extra_body: Optional[Dict]) -> Optional[Dict]: - """ - Ensure that the extra_body sent in the request is safe, otherwise users will see this error - - "Object of type TextPromptClient is not JSON serializable + "Object of type TextPromptClient is not JSON serializable - Relevant Issue: https://github.com/BerriAI/litellm/issues/4140 - """ - if extra_body is None: - return None - - if not isinstance(extra_body, dict): - return extra_body - - if "metadata" in extra_body and isinstance(extra_body["metadata"], dict): - if "prompt" in extra_body["metadata"]: - _prompt = extra_body["metadata"].get("prompt") - - # users can send Langfuse TextPromptClient objects, so we need to convert them to dicts - # Langfuse TextPromptClients have .__dict__ attribute - if _prompt is not None and hasattr(_prompt, "__dict__"): - extra_body["metadata"]["prompt"] = _prompt.__dict__ + Relevant Issue: https://github.com/BerriAI/litellm/issues/4140 + """ + if extra_body is None: + return None + if not isinstance(extra_body, dict): return extra_body - @staticmethod - def pick_cheapest_chat_models_from_llm_provider(custom_llm_provider: str, n=1): - """ - Pick the n cheapest chat models from the LLM provider. + if "metadata" in extra_body and isinstance(extra_body["metadata"], dict): + if "prompt" in extra_body["metadata"]: + _prompt = extra_body["metadata"].get("prompt") - Args: - custom_llm_provider (str): The name of the LLM provider. - n (int): The number of cheapest models to return. + # users can send Langfuse TextPromptClient objects, so we need to convert them to dicts + # Langfuse TextPromptClients have .__dict__ attribute + if _prompt is not None and hasattr(_prompt, "__dict__"): + extra_body["metadata"]["prompt"] = _prompt.__dict__ - Returns: - list[str]: A list of the n cheapest chat models. - """ - if custom_llm_provider not in litellm.models_by_provider: - return [] + return extra_body - known_models = litellm.models_by_provider.get(custom_llm_provider, []) - model_costs = [] - for model in known_models: - try: - model_info = litellm.get_model_info( - model=model, custom_llm_provider=custom_llm_provider - ) - except Exception: - continue - if model_info.get("mode") != "chat": - continue - _cost = model_info.get("input_cost_per_token", 0) + model_info.get( - "output_cost_per_token", 0 +def pick_cheapest_chat_models_from_llm_provider(custom_llm_provider: str, n=1): + """ + Pick the n cheapest chat models from the LLM provider. + + Args: + custom_llm_provider (str): The name of the LLM provider. + n (int): The number of cheapest models to return. + + Returns: + list[str]: A list of the n cheapest chat models. + """ + if custom_llm_provider not in litellm.models_by_provider: + return [] + + known_models = litellm.models_by_provider.get(custom_llm_provider, []) + model_costs = [] + + for model in known_models: + try: + model_info = litellm.get_model_info( + model=model, custom_llm_provider=custom_llm_provider ) - model_costs.append((model, _cost)) + except Exception: + continue + if model_info.get("mode") != "chat": + continue + _cost = model_info.get("input_cost_per_token", 0) + model_info.get( + "output_cost_per_token", 0 + ) + model_costs.append((model, _cost)) - # Sort by cost (ascending) - model_costs.sort(key=lambda x: x[1]) + # Sort by cost (ascending) + model_costs.sort(key=lambda x: x[1]) - # Return the top n cheapest models - return [model for model, _ in model_costs[:n]] - - @staticmethod - def select_model_for_request_transformation( - model: str, - base_model: Optional[str] = None, - litellm_params: Optional[Dict] = None, - ) -> str: - """ - If `base_model` is passed in by user, use it for the request transformation - - Else, use the model passed in the request - """ - if base_model is not None: - return base_model - elif ( - litellm_params is not None and litellm_params.get("base_model") is not None - ): - return litellm_params["base_model"] - else: - return model + # Return the top n cheapest models + return [model for model, _ in model_costs[:n]]