o
    ưi                     @   sN   d Z ddlmZmZ ddlmZ ddlmZ dededee	e	f fdd	Z
d
S )zg
Helper util for handling perplexity-specific cost calculation
- e.g.: citation tokens, search queries
    )TupleUnion)Usage)get_model_infomodelusagereturnc                 C   s  t |dd}|durt|tr|d}|durdt|fS t| dd}ddttttdt	f dtd	tfd
d}||d}|j
pBd| }t |ddpLd}|d}	|dkrd|	durd||	}
|||
 7 }||d}|jpod| }t |ddpyd}|dkrt|dr|jrt |jddpd}|d}|dkr|dur||}||| 7 }d}t|dr|jrt |jddpd}|dp|d}|dkr|durt|tr||ddd }n||}|| }||7 }||fS )aW  
    Calculates the cost per token for a given model, prompt tokens, and completion tokens.

    Input:
        - model: str, the model name without provider prefix
        - usage: LiteLLM Usage block, containing perplexity-specific usage information

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
    ZcostN
total_cost        Z
perplexity)r   Zcustom_llm_providervaluedefaultr   c              	   S   s2   | du r|S zt | W S  ttfy   | Y S w )z@Safely cast a value to float with proper type handling for mypy.N)float
ValueError	TypeError)r   r    r   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/perplexity/cost_calculator.py_safe_float_cast%   s   
z(cost_per_token.<locals>._safe_float_castinput_cost_per_tokenr   citation_tokenscitation_cost_per_tokenoutput_cost_per_tokenreasoning_tokenscompletion_tokens_detailsZoutput_cost_per_reasoning_tokenprompt_tokens_detailsZweb_search_requestsZsearch_queries_cost_per_queryZsearch_context_cost_per_queryZsearch_context_size_lowi  )r
   )getattr
isinstancedictgetr   r   r   strintobjectZprompt_tokensZcompletion_tokenshasattrr   r   )r   r   Z	cost_infor	   Z
model_infor   r   Zprompt_costr   Zcitation_cost_valuer   r   Zcompletion_costr   Zreasoning_cost_valueZreasoning_cost_per_tokenZnum_search_queriesZsearch_cost_valueZsearch_cost_per_queryZsearch_costr   r   r   cost_per_token   sD   
&



r"   N)__doc__typingr   r   Zlitellm.types.utilsr   Zlitellm.utilsr   r   r   r"   r   r   r   r   <module>   s
    "