
    h              
          d dl Z 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
 d dlmZmZ dee   ded   d	eeeee   f   fd
Zde
ded   d	eeeee   f   fdZdee   d	ee   fdZ	 ddee   ded   d	efdZ	 dde
ded   d	ee   fdZded	ee   fdZ	 ddee   ded   d	efdZ	 ddee   ded   d	efdZded	efdZded	efdZded	efdZy)    N)AnyListLiteralTuple)verbose_logger)Batch)	CallTypesUsagefile_content_dictionarycustom_llm_provider)openaiazure	vertex_aireturnc                 ^   K   t        ||       }t        | |      }t        |       }|||fS w)z1
    Calculate the cost and usage of a batch
    r   r   r   r   )_batch_cost_calculator,_get_batch_job_total_usage_from_file_content#_get_batch_models_from_file_content)r   r   
batch_costbatch_usagebatch_modelss        W/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/batches/batch_utils.pycalculate_batch_cost_and_usager   
   sD      (/ 7J ? 7/K
 77NOL{L00s   +-batchc                    K   t        | |       d{   }t        ||      }t        ||      }t        |      }|||fS 7 .w)z?Helper function to process a completed batch and handle loggingNr   r   ),_get_batch_output_file_content_as_dictionaryr   r   r   )r   r   r   r   r   r   s         r   _handle_completed_batchr       sb      %Q"% 
 (/ 7J ? 7/K
 77NOL{L00!s   AA/Ac                     g }| D ]>  }t        |      st        |      }|j                  d      }|s.|j                  |       @ |S )z.
    Get the models from the file content
    model)_batch_response_was_successful(_get_response_from_batch_job_output_filegetappend)r   r   _item_response_body_models        r   r   r   9   sN     L()%0EeLN#''0F##F+ )     c                 l    |dk(  rt        d      t        | |      }t        j                  d|       |S )zC
    Calculate the cost of a batch based on the output file id
    r   1Vertex AI does not support file content retrievalr   total_cost=%s)
ValueError%_get_batch_job_cost_from_file_contentr   debug)r   r   
total_costs      r   r   r   I   s@     k)LMM6 7/J *5r)   c                    K   ddl m} |dk(  rt        d      | j                  t        d       || j                  |       d{   }t	        |j
                        S 7 w)zE
    Get the batch output file content as a list of dictionaries
    r   )afile_contentr   r+   Nz3Output file id is None cannot retrieve file content)file_idr   )litellm.files.mainr2   r-   output_file_id_get_file_content_as_dictionarycontent)r   r   r2   _file_contents       r   r   r   Z   sj      1k)LMM#NOO'$$/ M +=+@+@AA	s   AA#A!A#file_contentc                 @   	 | j                  d      }g }|j                         j                  d      D ])  }|s|j                  t	        j
                  |             + t        j                  dt	        j                  |d             |S # t        $ r}|d}~ww xY w)zO
    Get the file content as a list of dictionaries from JSON Lines format
    zutf-8
zjson_objects=%s   indentN)
decodestripsplitr%   jsonloadsr   r/   dumps	Exception)r9   _file_content_strjson_objectslinees        r   r6   r6   p   s    
(//8%++-33D9D##DJJt$45 : 	.

<PQ0RS s   7B AB 	BBBc                    	 d}t        j                  dt        j                  | d             | D ]b  }t	        |      st        |      }|t        j                  ||t        j                  j                        z  }t        j                  d|       d |S # t        $ r}t        j                  d|       |d}~ww xY w)	z;
    Get the cost of a batch job from the file content
    g        zfile_content_dictionary=%sr<   r=   )completion_responser   	call_typer,   z.error in _get_batch_job_cost_from_file_contentN)r   r/   rB   rD   r"   r#   litellmcompletion_costr	   aretrieve_batchvaluerE   error)r   r   r0   r&   r'   rI   s         r   r.   r.      s    
($**5LUV*W	
 -E-e4!I%!Pg55(6(;'77== 

 $$_jA -  MqQs   =B  AB 	B>!B99B>c                     d}d}d}| D ]Q  }t        |      st        |      }t        |      }||j                  z  }||j                  z  }||j
                  z  }S t        |||      S )z=
    Get the tokens of a batch job from the file content
    r   )total_tokensprompt_tokenscompletion_tokens)r"   r#   '_get_batch_job_usage_from_response_bodyrS   rT   rU   r
   )r   r   rS   rT   rU   r&   r'   usages           r   r   r      s     LM()%0EeLNB>REE...LU000M!8!88 ) !#+ r)   response_bodyc                 H    | j                  dd      xs i }t        di |}|S )z>
    Get the tokens of a batch job from the response body
    rW   N )r$   r
   )rX   _usage_dictrW   s      r   rV   rV      s-      ##GT28bK';'ELr)   batch_job_output_filec                 ^    | j                  dd      xs i }|j                  dd      xs i }|S )z9
    Get the response from the batch job output file
    responseNbodyr$   )r\   	_responser'   s      r   r#   r#      s6     ,//
DAGRI]]6406BNr)   c                 X    | j                  dd      xs i }|j                  dd      dk(  S )z7
    Check if the batch job response status == 200
    r^   Nstatus_code   r`   )r\   ra   s     r   r"   r"      s1     ,//
DAGRI==-44r)   )r   )rB   typingr   r   r   r   rM   litellm._loggingr   litellm.types.llms.openair   litellm.types.utilsr	   r
   dictfloatstrr   r   r   r   r   bytesr6   r.   r   rV   r#   boolr"   rZ   r)   r   <module>rn      s    , ,  + + 01!$Z1 !?@1 5%c"#1,11 !?@1 5%c"#12!$Z	#Y$ DL!$Z !?@ & DLBB !?@B 
$ZB,% DJ & DL!$Z !?@ < DL!$Z !?@ 04 E D S 5$ 54 5r)   