
    )#hj              #       &   d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d dlmZ d dlmZ d dl mZ! d dl"mZ# d dl$mZ% d d
l&m'Z( d dl&mZ) d dl*m+Z+ d dl,m-Z. d dl,mZ/ d dl,m0Z1 d d	l2mZ3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z:m;Z;m<Z< d dl=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZF 	 	 	 	 	 dCdeGdeGdeeG   dee?   deeG   deeeGeGf      fdZH	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dDdeIdeJdeJdeeG   deeI   deeJ   deeJ   deeJ   d eeJ   dee?   deeG   d!eeJ   d"ee<   d#e9d$eGdeeGeGf   f d%ZdEd&eKfd'ZLd(edeMfd)ZN	 dFdeeI   deeI   deeI   fd*ZO	 	 	 dGdeeI   d&ee   d+eeI   d,eeM   deeI   deeI   fd-ZP ed./      deIdeMfd0       ZQd&edee<   fd1ZRd#ee9   d&edee9   fd2ZSdddg dddddddddddddfdeeI   d3ed4eeG   d#ee9   d5eeI   d6eeI   d7eeJ   dee?   deeG   d8eeK   d,eeM   d+eeI   deGfd9ZT	 	 	 	 dHd:e	eBe@eAeDeCe5e7f   deIdeeI   d#ed;   d8eKd<eeM   d+eeI   d,eeM   d=eIdeeG   fd>ZUdeIdeeI   deeGeGf   fd?ZVdeIdeeI   d@eGdeeGeGf   fdAZW	 	 	 	 	 dIdeIdeeI   d6eeI   d7eeJ   d5eeI   d8eeK   deGfdBZXy)J    N)	lru_cache)AnyListLiteralOptionalTupleUnion)	BaseModel)verbose_logger)_generic_cost_per_character)cost_per_token)cost_per_query)cost_calculator)cost_per_second)get_model_params_and_category)cost_per_character)cost_router)HttpxBinaryResponseContent)RerankResponse)CallTypesLiteralLlmProvidersSetPassthroughCallTypesUsage)		CallTypesCostPerTokenEmbeddingResponseImageResponseModelResponseTextCompletionResponseTranscriptionResponse_cached_get_model_info_helpertoken_counter        prompt_tokenscompletion_tokensresponse_time_mscustom_cost_per_tokencustom_cost_per_secondreturnc                 V    ||y||d   | z  }|d   |z  }||fS |||z  dz  }d|fS y)zFInternal helper function for calculating cost, if custom pricing givenNinput_cost_per_tokenoutput_cost_per_token  r    )r$   r%   r&   r'   r(   
input_costoutput_costs          T/var/www/html/sandstorm/venv/lib/python3.12/site-packages/litellm/cost_calculator.py%_cost_per_token_custom_pricing_helperr2   J   sh     $)?)G(*+AB]R
+,CDGXX;&&		+,/??$F+~     modelcustom_llm_providerprompt_characterscompletion_characterscache_creation_input_tokenscache_read_input_tokensnumber_of_queriesusage_object	call_type!audio_transcription_file_durationc           
         | t        d      ||}nt        ||||z   ||	      }t        |||||
      }|
|d   |d   fS d}d}t        j                  }| }||dz   | z   }|,| d| d|  }||v r|}nt        j
                  |       \  }}}}| }| j                  dd      }t        |      dkD  r|d   }n| }	 ||v r|} n| |v r| } n||v r|} |d	k(  s|d
k(  rZ|t        dj                  || ||            t        |||ddd      \  }}||t        dj                  ||||||            ||fS |dk(  s|dk(  rt        | |      S |dk(  s|dk(  rt        | ||      S |dk(  r9t        |||      }|dk(  rt        ||||||      S |dk(  rt        ||||      S y|dk(  rt!        | |      S |dk(  rt#        | |      S |dk(  rt%        | |      S |dk(  rt'        | |      S |dk(  rt)        | ||       S |d!k(  rt+        | |      S |d"k(  rt-        | |      S t/        | |      }|d#   dkD  r	|d#   |z  }nG|j1                  d$d      5|3t3        j4                  d%| |j1                  d$d      |       |d$   |z  d&z  }|d'   dkD  r	|d'   |z  }nG|j1                  d(d      5|3t3        j4                  d)| |j1                  d(d      |       |d(   |z  d&z  }t3        j4                  d*| ||       ||fS )+ar  
    Calculates the cost per token for a given model, prompt tokens, and completion tokens.

    Parameters:
        model (str): The name of the model to use. Default is ""
        prompt_tokens (int): The number of tokens in the prompt.
        completion_tokens (int): The number of tokens in the completion.
        response_time (float): The amount of time, in milliseconds, it took the call to complete.
        prompt_characters (float): The number of characters in the prompt. Used for vertex ai cost calculation.
        completion_characters (float): The number of characters in the completion response. Used for vertex ai cost calculation.
        custom_llm_provider (str): The llm provider to whom the call was made (see init.py for full list)
        custom_cost_per_token: Optional[CostPerToken]: the cost per input + output token for the llm api call.
        custom_cost_per_second: Optional[float]: the cost per second for the llm api call.
        call_type: Optional[str]: the call type

    Returns:
        tuple: A tuple containing the cost in USD dollars for prompt tokens and completion tokens, respectively.
    Nz"Invalid arg. Model cannot be none.)r$   r%   total_tokensr9   r:   )r$   r%   r&   r(   r'   r      /r5   speechaspeechzvprompt_characters must be provided for tts calls. prompt_characters={}, model={}, custom_llm_provider={}, call_type={})r5   r6   r7   r8   custom_prompt_costcustom_completion_costzcost for tts call is None. prompt_cost={}, completion_cost={}, model={}, custom_llm_provider={}, prompt_characters={}, completion_characters={}arerankrerankr5   r6   atranscriptiontranscriptionr5   r6   duration	vertex_ai)r5   r6   r=   r   )r5   r6   r7   r8   r$   r%   r   )r5   r6   r$   r%   	anthropic)r5   usageopenai
databricksfireworks_aiazure)r5   rQ   r&   geminideepseekr+   input_cost_per_secondz;For model=%s - input_cost_per_second: %s; response time: %sr-   r,   output_cost_per_secondz<For model=%s - output_cost_per_second: %s; response time: %szlReturned custom cost for model=%s - prompt_tokens_cost_usd_dollar: %s, completion_tokens_cost_usd_dollar: %s)	Exceptionr   r2   litellm
model_costget_llm_providersplitlen
ValueErrorformatr   rerank_costopenai_cost_per_secondgoogle_cost_routergoogle_cost_per_charactergoogle_cost_per_tokenanthropic_cost_per_tokenopenai_cost_per_tokendatabricks_cost_per_tokenfireworks_ai_cost_per_tokenazure_openai_cost_per_tokengemini_cost_per_tokendeepseek_cost_per_tokenr!   getr   debug)r5   r$   r%   r&   r6   region_namer7   r8   r9   r:   r'   r(   r;   r<   r=   r>   usage_blockresponse_costprompt_tokens_cost_usd_dollar!completion_tokens_cost_usd_dollarmodel_cost_refmodel_with_providermodel_with_provider_and_region_model_without_prefixmodel_partsprompt_costcompletion_costr   
model_infos                                 r1   r   r   a   s   T }<== "'/&)::(C$;
 :#+)53M  Qq!111 ,-!/0%''N&1C7%?"&'qQug> + /.@&D#'.'?'?e'L$1 ++c1%K
;!*1~$ 	~-#	.	 .$ H	Y 6$ I  P  P%'	  (C& 3/"###$(
$_ /"9 b  i  i#('%)	 	 O++	i	9#8 3
 	
 
&	&)*F% 36
 	

 
	+(& 3

 ..,*$7"3&;+"3  ,,(*$7+"3	  - 
	+'e;GG		($5DD		,(uKHH		.*kJJ		'*{=M
 	
 
	($5DD	
	*&U+FF2-@

 ,-1 12]B * NN2D9E ,  M6= 	 236FFM * -.2236GG . NN3T:F ,  N7> 	 347GG$N . 	z)-		
 -.OOOr3   completion_responsec                     d}|dk(  rH| j                  dt        j                               }t        | dt        j                               }||z
  }||z  dz  S )NgǺV?r#   createdendedr-   )rn   timegetattr)r~   
total_time!a100_80gb_price_per_second_public
start_timeend_times        r1    get_replicate_completion_pricingr   S  s]     	 & S(,,Y		D
.E
*
,z9D@@r3   objc                     t        | d      S )N_hidden_params)hasattr)r   s    r1   has_hidden_paramsr   a  s    3())r3   c                     ||S | y 	 t        j                  |       \  }}}}|S # t        $ r+}t        j                  dt        |              Y d }~y d }~ww xY w)NrC   zblitellm.cost_calculator.py::_get_provider_for_cost_calc() - Error inferring custom_llm_provider - )r[   r]   rZ   r   ro   str)r5   r6   rx   es       r1   _get_provider_for_cost_calcr   e  sv     &""}'.'?'?e'L$1   pqtuvqwpxy	
 	s   & 	A!AA
base_modelcustom_pricingc                    d}d}t        | |      }|du r| }||}t        |dd      }t        |dd      }|7|5|j                  dd      #t        |d         dkD  r|j                  d|       }|$|j                  dd      |j                  dd      }|||}|| | }|"| t	        |      s|| d| d| }|S | d| }|S )	z
    1. If custom pricing is true, return received model name
    2. If base_model is set (e.g. for azure models), return that
    3. If completion response has model set return that
    4. Check if model is passed in return that
    NrJ   Tr5   r   r   rp   rB   )r   r   rn   r_   "_model_contains_known_llm_provider)	r5   r~   r   r   r6   return_modelrp   completion_response_modelhidden_paramss	            r1    _select_model_name_for_cost_calcr   x  s?    #'L!%K5)< !/6Wd0 %,,?AQSW$XM (]-Fgt,8M'*+a/(,,We<L ]%6%6}d%K%W#''t< 9 E0 1 	 +2<@"12!K=,PL  22!L>BLr3      )maxsizec                 :    | j                  d      d   }|t        v S )z:
    Check if the model contains a known llm provider
    rB   r   )r^   r   )r5   _provider_prefixs     r1   r   r     s#    
 {{3'*..r3   c                 P    d }| !t        | t              r| j                  d      }|S )NrQ   )
isinstancer   rn   )r~   	usage_objs     r1   _get_usage_objectr     s3     "&I&:], (++G4	r3   c                    | | S |y t        |t              ryt        |t              ryt        |t              ryt        |t              ryt        |t
              ryt        |t              ryt        |t              ry| S )N
completion	embeddingrL   rD   rI   image_generationtext_completion)r   r   r   r    r   r   r   r   )r=   r~   s     r1   _infer_call_typer     s     "%}5	'):	;	')>	?	')C	D	'	8	'	7!	')?	@ r3   messagesr   sizequalitynoptional_paramsc                    	 t        ||       xs d}|dk(  s|dk(  r'|%t        |t              rt        |      dk(  r|dk(  rd}d}d}d}d}d}d}d}t	        | 	      }t        || |||
      }| t        | t              st        | t              rt        | t              r| j                  di       }nt        | di       }t        |t              rHt        |t        j                        s.t        | dt        j                  d7i |j                                |i }n#t        |t              r|j                         }n|}|j                  dd      }|j                  dd      }|j                  dd      }|j                  dd      }d|v r1|d   i k7  r)|d   r$|j                  di       }|j                  dd      }t        | dd      }t        j                  dt        | dd       d       t        | dd      }||j                  d|xs d      }|j                  d|      }|j                  di       j                  dd      }	|j                  di       j                  dd      }
|j                  di       j                  dd      }nW|t!        d|  d |       t        |      dkD  rt#        ||!      }nt        |      dkD  rt#        ||"      }t#        ||"      }|t!        d|  d |       |	 t        j$                  |#      \  }}}}|t*        j,                  j.                  k(  s:|t*        j0                  j.                  k(  s|t2        j4                  j.                  k(  rc|d%k(  rt        | t6              rzt9        || &      S |d'k(  r*t        | t6              rt;        ||	| |(      S t=        d)      t?        ||
|||	|*      S |t*        j@                  j.                  k(  s|t*        jB                  j.                  k(  r!t        jD                  jG                  |+      }n|t*        jH                  j.                  k(  s|t*        jJ                  j.                  k(  rt        | d,d      }n|t*        jL                  j.                  k(  s|t*        jN                  j.                  k(  rP| Nt        | tP              r>| jR                  }||j                  d-i       xs i } ni } | j                  d.      xs d}!|!}d/|v s	d0|v s|d0k(  rtU        |t+        |      1      }n4|t        jV                  v sd2|v r|t        jX                  vrt[        | |      S |t!        d|  d |       ||d%k(  rt        |      dkD  rCt        jD                  j]                  d3|id4      }"t        jD                  jG                  |"+      }| Pt        | t^              r@t        jD                  ja                  | 5      }#t        jD                  jG                  |#+      }tc        |||||||||||||||6      \  }$}%|$|%z   }&|&S # t&        $ r8}t        j                  d$j)                  t        |                   Y d}~\d}~ww xY w# t&        $ r}|d}~ww xY w)8a  
    Calculate the cost of a given completion call fot GPT-3.5-turbo, llama2, any litellm supported llm.

    Parameters:
        completion_response (litellm.ModelResponses): [Required] The response received from a LiteLLM completion request.

        [OPTIONAL PARAMS]
        model (str): Optional. The name of the language model used in the completion calls
        prompt (str): Optional. The input prompt passed to the llm
        completion (str): Optional. The output completion text from the llm
        total_time (float, int): Optional. (Only used for Replicate LLMs) The total time used for the request in seconds
        custom_cost_per_token: Optional[CostPerToken]: the cost per input + output token for the llm api call.
        custom_cost_per_second: Optional[float]: the cost per second for the llm api call.

    Returns:
        float: The cost in USD dollars for the completion based on the provided parameters.

    Exceptions:
        Raises exception if model not in the litellm model cost map. Register model, via custom pricing or PR - https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json


    Note:
        - If completion_response is provided, the function extracts token information and the model name from it.
        - If completion_response is not provided, the function calculates token counts based on the model and input text.
        - The cost is calculated based on the model, prompt tokens, and completion tokens.
        - For certain models containing "togethercomputer" in the name, prices are based on the model size.
        - For un-mapped Replicate models, the cost is calculated based on the total time used for the request.
    r   aimage_generationr   Nr   rU   zdall-e-2r#   )r~   )r5   r~   r6   r   r   rQ   r$   r%   r9   r:   prompt_tokens_detailscached_tokens_response_msz!completion_response response ms:  r   r6   rp   r   r   1024-x-1024r   standardr   rA   z[Model is None and does not exist in passed completion_response. Passed completion_response=z, model=)r5   r   )r5   textrC   zXlitellm.cost_calculator.py::completion_cost() - Error inferring custom_llm_provider - {}rO   )r5   image_responsebedrock)r5   r   r   r   zTcompletion_response must be of type ImageResponse for bedrock image cost calculation)r5   r   r6   r   r   r   )r   rN   billed_unitssearch_unitstogethercomputertogether_ai)r=   	replicater   )datar=   )response_obj)r5   r$   r%   r6   r&   rp   r(   r'   r7   r8   r9   r:   r<   r=   r>   r.   )2r   r   r   r_   r   r   r
   dictrn   r   r[   r   setattr
model_dumpr   ro   r`   r"   r]   rZ   ra   r   r   valuer   r   passthrough_image_generationr   vertex_ai_image_cost_calculatorbedrock_image_cost_calculator	TypeErrordefault_image_cost_calculatorrD   rE   utils_count_charactersrK   rL   rI   rH   r   metar   replicate_modelsr\   r   get_formatted_promptr   get_response_stringr   )'r~   r5   promptr   r   r   r=   r6   rp   r   r   r   r'   r(   r   r   r   r$   r7   r%   r8   r9   r:   r>   cost_per_token_usage_objectr   _usager   r   rx   r   meta_objr   r   prompt_stringcompletion_stringrs   rt   _final_costs'                                          r1   r|   r|     s   dm$Y0CDT	 -->P1P!5#&E
a#w.E+//359#1536)7H 38
# 1 3 3)!
 **I6-t4-t4:M:Q:QR;	 $$7"E	)Y/
7==9 'MM;I$8$8$:;
  Iy1"--/""JJ:M &

+> B*0**5RTU*V'&,jj1JA&N#'6123r923(.

3JB(O%*?*C*COUV*W' !4naHJ  3G<OQ_ae4f3gghi $$79I4PM(&3&7&7)+>+F$'# ,//{K$(():B?CCM (++,=rBFFz "%%&7<@@ } q  sF  rG  GO  PU  OV  W  8}q  -EH MVq -E G -E
 K=m  oB  nC  CK  LQ  KR  S  &	3:3K3K40*Aq 33999I77===0MMSSS #k11=A:#':  %	11=A8#!':(7	   j  5#(;$3  ))///9	@Q@Q@W@W3W ' ? ?V ? L11777I3399907#Z1- ))///9	@Q@Q@W@W3W".:#^4 /33'#+<<#C#IrL#%L !$$^49  %1! %'%"m3
 2%9YCWXE
 W---1E7---34GTT=m  oB  nC  CK  LQ  KR  S  */Bk/Q8}q  ' B B$h/< !C ! %,MM$C$C$C$W!".:#]4 %,MM$E$E!4 %F %! )0(G(G* )H )% '/ 3'##9"7/"7(C$;4.O
	
)-$ 46WWC  $$nuuA D  s]   K7Y9 :X5 A9Y9 #Y9 3Y9 FY9 C Y9 5	Y6>-Y1+Y9 1Y66Y9 9	Z	ZZ	response_object)r   
aembeddingr   acompletionatext_completionr   r   r   
moderationamoderationrK   rL   rE   rD   rI   rH   	cache_hitr   c	           
          	 d}	||du rd}	|	S t        | t              r|| j                  d<   t        | |||||||      }	|	S # t        $ r}
|
d}
~
ww xY w)z7
    Returns
    - float or None: cost of response
    r#   NTr   )r~   r5   r=   r6   r   r   r   r   )r   r
   r   r|   rZ   )r   r5   r6   r=   r   r   r   r   r   rr   r   s              r1   response_cost_calculatorr     s    P" Y$%6M  /95DS../@A+$3#$7 /-%	M  s   A 3A 	AAAc                     d}t        j                  | |      \  }}}}	 |dk(  rt        | |      S |dk(  rt        | |      S t	        d|  d|       # t
        $ r}|d}~ww xY w)	zI
    Returns
    - float or None: cost of response OR none if error.
    rA   rJ   cohere)r5   num_queriesazure_aiz.invalid custom_llm_provider for rerank model: z, custom_llm_provider: N)r[   r]   cohere_rerank_cost_per_queryazure_ai_rerank_cost_per_queryr`   rZ   )r5   r6   default_num_queriesrx   r   s        r1   rb   rb   A  s     #*#;#;)<$ AAq(*/)<  !J.1)<  <UGCZ[nZop
 	
  s"   A A A 	A%A  A%rN   c                     t        | ||      S )NrM   )rc   rM   s      r1   transcription_costr   ^  s     ")<x r3   c           
          |xs d}d|v rd|vr|j                  dd      n|}t        t        |j                  d            \  }}| d|  }	|r+| j	                  |      r| d| d| j                  |d       }	|r| d|	 n|	}
t        j                  d|
 d|	        |
t        j                  v rt        j                  |
   }n|	t        j                  v rt        j                  |	   }n| d| j                  d      d    }|r| d| n|}|t        j                  v rt        j                  |   }n=|t        j                  v rt        j                  |   }nt        d	|
 d|	 d| d
|       |d   |z  |z  |z  S )a  
    Default image cost calculator for image generation

    Args:
        model (str): Model name
        image_response (ImageResponse): Response from image generation
        quality (Optional[str]): Image quality setting
        n (Optional[int]): Number of images generated
        size (Optional[str]): Image size (e.g. "1024x1024" or "1024-x-1024")

    Returns:
        float: Cost in USD for the image generation

    Raises:
        Exception: If model pricing not found in cost map
    r   xz-x-rB   r4   zLooking up cost for models: z, z#Model not found in cost map. Tried z, and input_cost_per_pixel)
replacemapintr^   
startswithr   ro   r[   r\   rZ   )r5   r6   r   r   r   r   size_strheightwidthbase_model_namemodel_name_with_quality	cost_infomodel_without_provider#model_with_quality_without_providers                 r1   r   r   f  s   2 )MH (?uH4 	e$  X^^E23MFE "
!E7+Ou//0CD"#1XJa>QSU0V/WX 	 +27)1_%&  
&'>&?r/ARS
 '"4"44&&'>?		G..	.&&7	 %-:Qu{{3/?/C.D!E5<wiq/01BX 	, /'2D2DD**+NOI#w'9'99**+ABI56M5NbQ`Paac  eH  dI  IO  Pf  Og  h  +,v5=AAr3   )r   r   r#   NN)r4   r   r   r#   NNNNr   r   NNNNr   r#   )r#   )N)NNN)NNNr4   )NNrA   r   N)Yr   	functoolsr   typingr   r   r   r   r   r	   pydanticr
   r[   litellm._loggingr   .litellm.litellm_core_utils.llm_cost_calc.utilsr   'litellm.llms.anthropic.cost_calculationr   rg   #litellm.llms.azure.cost_calculationrk   %litellm.llms.azure_ai.cost_calculatorr   r   *litellm.llms.bedrock.image.cost_calculatorr   r   #litellm.llms.cohere.cost_calculatorr   'litellm.llms.databricks.cost_calculatorri   %litellm.llms.deepseek.cost_calculatorrm   )litellm.llms.fireworks_ai.cost_calculatorrj   #litellm.llms.gemini.cost_calculatorrl   $litellm.llms.openai.cost_calculationr   rc   rh   (litellm.llms.together_ai.cost_calculatorr   &litellm.llms.vertex_ai.cost_calculatorr   re   rf   r   rd   7litellm.llms.vertex_ai.image_generation.cost_calculatorr   litellm.types.llms.openair   litellm.types.rerankr   litellm.types.utilsr   r   r   r   litellm.utilsr   r   r   r   r   r   r    r!   r"   floatr2   r   r   r   r   boolr   r   r   r   r   r   r|   r   rb   r   r   r.   r3   r1   <module>r     s     = =    " V X Y R U A / 
 
 
  (+48.2 uo
 $L1 %UO eE5L!"0 (+)-'++/12-.48.2'+$("./2-oPoPoP oP uo	oP
 "#oP  }oP $C=oP "*#oP &c]oP $L1oP %UOoP"  }#oP& 5/'oP*  +oP, (--oP. 5%</oPdA$ A*3 *4 * *.C=!# c], !%%))-6C=6!#6 6 TN	6
 "#6 c]6r 2/c /d / /		e_	()@C8 "%,0!48.2&*%) $)_C=_ 	_ _ ()_ 3-_ c]_ }_  $L1!_" %UO#_$ d^%_& TN'_( )_* +_B
 !% $%)E;"	; ; "#; 	;< =;> ~?;@ A;B TNC;D E;F e_G;|!# 5%<:%-c]>C
5%< *.!'&*FBFB!#FB c]FB }	FB
 3-FB d^FB FBr3   