
    h#                        d dl mZmZmZmZ d dlZd dlmZ d dlmZm	Z	 d dl
mZmZ 	 	 g dZdeded	eed
   ef   ded   fdZ	 	 ddedededee   dee   deeef   fdZdededeeef   fdZdedededeeef   fdZy)    )LiteralOptionalTupleUnionN)verbose_logger)_is_above_128kgeneric_cost_per_token)	ModelInfoUsage)zgemini-1.0-proz
gemini-progemini-2modelcustom_llm_provider	call_type)	embedding
aembeddingreturn)cost_per_charactercost_per_tokenc                 l    |dk(  rd| v sd| v sd| v sd| v sd| v ry|dk(  r|dk(  s|d	k(  ry|dk(  rd
| v ryy)z
    Route the cost calc to the right place, based on model/call_type/etc.

    Returns
        - str, the specific google cost calc function it should route to.
    	vertex_aiclaudellamamistraljamba	codestralr   r   r   r   r    )r   r   r   s      b/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/vertex_ai/cost_calculator.pycost_routerr      sf     k)Eee%		+[ I$=		+u1D    usageprompt_characterscompletion_charactersc                    t        j                  | |      }t        j                  | |      }|t        | ||      \  }}nj	 t        |dz        r2| t        vr*d|v r|d   	 J dj                  | |             ||d   z  }n(d|v r|d   J d	j                  | |             ||d   z  }|t        | ||      \  }}	||	fS |j                  }
	 t        |dz        r2| t        vr*d|v r|d   	 J dj                  | |             |
|d   z  }	n(d|v r|d   J dj                  | |             ||d   z  }	||	fS # t        $ rH}t        j                  d
j                  t        |                   t        | ||      \  }}Y d}~d}~ww xY w# t        $ rK}t        j                  d
j                  t        |                   t        | ||      \  }}	Y d}~||	fS d}~ww xY w)a#  
    Calculates the cost per character for a given VertexAI model, input messages, and response object.

    Input:
        - model: str, the model name without provider prefix
        - custom_llm_provider: str, "vertex_ai-*"
        - prompt_characters: float, the number of input characters
        - completion_characters: float, the number of output characters

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd

    Raises:
        Exception if model requires >128k pricing, but model cost not mapped
    r   r   Nr   r   r       tokens*input_cost_per_character_above_128k_tokenszzmodel info for model={} does not have 'input_cost_per_character_above_128k_tokens'-pricing for > 128k tokens
model_info={}input_cost_per_characterzVmodel info for model={} does not have 'input_cost_per_character'-pricing
model_info={}zslitellm.litellm_core_utils.llm_cost_calc.google.py::cost_per_character(): Exception occured - {}
Defaulting to None+output_cost_per_character_above_128k_tokenszimodel info for model={} does not have 'output_cost_per_character_above_128k_tokens' pricing
model_info={}output_cost_per_characterzWmodel info for model={} does not have 'output_cost_per_character'-pricing
model_info={})litellmget_model_infor   r   models_without_dynamic_pricingformat	Exceptionr   debugstrcompletion_tokens)r   r   r    r!   r"   
model_infoprompt_cost_ecompletion_costr4   s              r   r   r   :   s   , '')<J
 '')<J
  ' 3
Q#	&7!&;<!?? AJN"#OP  Q  X  X:	  & !MNO  /*<"#=>J mss:K
 0*=W2XX $+ 3
?Z ''O "33$	&;a&?@!?? BZO"#PQ  @  G  G:	  & !NOP   0:="#>?K ntt:L *J7R,SS   ''w  
	   G  N  NF
 ,$7NK
	^  
	   G  N  NF
 "0$7"A ''
	s2   A)D> A)F >	F>F

F	G&>G!!G&r5   c                    | j                  d      }| j                  d      }|j                  }|j                  }t        |      r|||z  }n|| d   z  }| j                  d      }t        |      r|	||z  }||fS || d   z  }||fS )N&input_cost_per_token_above_128k_tokens'output_cost_per_token_above_128k_tokensr'   input_cost_per_tokenoutput_cost_per_token)getprompt_tokensr4   r   )r5   r    r;   r<   r@   r4   r6   r9   s           r   _handle_128k_pricingrA      s    
 .8^^0.* /9nn1/+ ''M// 	m,2>#&LL#j1G&HH /9nn1/+ 	/03?+.UU '' ,j9P.QQ''r   c                     t        j                  | |      }|j                  d      }|j                  d      }||t        ||      S t	        | ||      S )a  
    Calculates the cost per token for a given model, prompt tokens, and completion tokens.

    Input:
        - model: str, the model name without provider prefix
        - custom_llm_provider: str, either "vertex_ai-*" or "gemini"
        - prompt_tokens: float, the number of input tokens
        - completion_tokens: float, the number of output tokens

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd

    Raises:
        Exception if model requires >128k pricing, but model cost not mapped
    r$   r;   r<   )r5   r    r%   )r-   r.   r?   rA   r	   )r   r   r    r5   r;   r<   s         r   r   r      s{    , '')<J
 .8^^0.* /9nn1/+ 	/:2>#!
 	

 "/ r   )NN)typingr   r   r   r   r-   r   .litellm.litellm_core_utils.llm_cost_calc.utilsr   r	   litellm.types.utilsr
   r   r/   r3   r   floatr   rA   r   r   r   r   <module>rG      s)   3 2  " 1 "N     W67<=  34	 @ *.-1{({({( {(  	{(
 $E?{( 5%<{(|#(#(#( 5%<#(L... . 5%<	.r   