
    hA+                         d dl Z d dlmZ d dlmZmZmZ d dlm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  e       Z G d d      Zy)    N)datetime)AnyOptionalUnion)urlparse)Logging)&PassThroughEndpointLoggingResultValues)!PassthroughStandardLoggingPayload)!StandardPassThroughResponseObject)executor   )"AnthropicPassthroughLoggingHandler)#AssemblyAIPassthroughLoggingHandler)CoherePassthroughLoggingHandler)VertexPassthroughLoggingHandlerc                      e Zd Zd Zdedeeeef   de	de
de
defdZd	ej                  d
ee   dedede	de	de
de
defdZd	ej                  d
ee   dede	de	de
de
dededefdZde	fdZde	fdZde	fdZde	fdZde	fdZdededefdZy)PassThroughEndpointLoggingc                 d    g d| _         dg| _        dg| _        t               | _        dg| _        y )N)generateContentstreamGenerateContentpredict
rawPredictstreamRawPredictz	/messagesz/v2/chatz
/langfuse/)TRACKED_VERTEX_ROUTESTRACKED_ANTHROPIC_ROUTESTRACKED_COHERE_ROUTESr   &assemblyai_passthrough_logging_handlerTRACKED_LANGFUSE_ROUTES)selfs    p/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/pass_through_endpoints/success_handler.py__init__z#PassThroughEndpointLogging.__init__!   s>    &
" *5% '1\"/1 	3
 )5~$    logging_obj standard_logging_response_objectresult
start_timeend_time	cache_hitc                    K   t        j                  |j                  ||||fi |  |j                  dt	        |t
              rt        j                  |      n|||dd| d{    y7 w)z@Helper function to handle both sync and async logging operationsF)r%   r&   r'   r(   N )thread_pool_executorsubmitsuccess_handlerasync_success_handler
isinstancedictjsondumps)r   r#   r$   r%   r&   r'   r(   kwargss           r    _handle_loggingz*PassThroughEndpointLogging._handle_logging6   s       	##'',	
 	
 0k// 

 fd+ 

6"5!

 

 
	
 
	
s   A$A.&A,'A.httpx_responseresponse_bodyrequest_body	url_routec
                    d |
d}d }| j                  |      r(t        j                  d|||||||	d|
}|d   }|d   }
n|| j                  |      r-t	        j
                  d||xs i ||||||	d|
}|d   }|d   }
n>| j                  |      r-t        j                  d||xs i ||||||	|d	|
}|d   }|d   }
||d<   |
|d<   |S )	N)r$   r3   )r5   r#   r8   r%   r&   r'   r(   r%   r3   r5   r6   r#   r8   r%   r&   r'   r(   )	r5   r6   r#   r8   r%   r&   r'   r(   r7   r$   r*   )	is_vertex_router   vertex_passthrough_handleris_anthropic_router   anthropic_passthrough_handleris_cohere_route"cohere_passthrough_logging_handlerpassthrough_chat_handler)r   r5   r6   r7   r#   r8   r%   r&   r'   r(   r3   return_dictr$   )vertex_passthrough_logging_handler_result,anthropic_passthrough_logging_handler_result)cohere_passthrough_logging_handler_results                   r    )normalize_llm_passthrough_logging_payloadzDPassThroughEndpointLogging.normalize_llm_passthrough_logging_payload\   sm    15
 ;?(	*/JJ 	#1 +'!)%'	 	 6 :(C - ?xHF$$Y/2PP 
#1"/"52 +'!)%'
 
 9 =XF - B(KF!!),2KK #1"/"52 +'!)%'!-  6 :(C - ?xHF, 	67 !'Hr"   passthrough_logging_payloadc                   K   d }|
|j                   d<   | j                  |      rVt        j                  |j                  j
                        dury  | j                  j                  d
||xs i ||||||d| y | j                  |      ry  | j                  d
|||	||||||d	|}|d   }|d   }|t        |j                        }| j                  ||
|      } | j                  d
|||||||
d	| d {    y 7 w)NrG   Tr:   )	r5   r6   r7   r#   r8   r%   r&   r'   r(   r$   r3   )response)r#   rG   r3   )r#   r$   r%   r&   r'   r(   %standard_pass_through_logging_payloadr*   )model_call_detailsis_assemblyai_router   _should_log_requestrequestmethodr   is_langfuse_routerF   r   text_set_cost_per_requestr4   )r   r5   r6   r#   r8   r%   r&   r'   r(   r7   rG   r3   r$   *normalized_llm_passthrough_logging_payloads                 r    "pass_through_async_success_handlerz=PassThroughEndpointLogging.pass_through_async_success_handler   s       	) ( 	&&'DE ##I.3GG"**11 
 ^D77^^ 
-+1r'#%!#
 
 ##I. ?>> #1"/!- +'!)%'  7 ;6 -
 @IF+3/P',,0, ++#(C , 
 #d"" 	
#-M!2M	
 	
 		
 		
s   C:D<D=Dc                 2    | j                   D ]  }||v s y yNTF)r   r   r8   routes      r    r;   z*PassThroughEndpointLogging.is_vertex_route  s"    //E	! 0 r"   c                 2    | j                   D ]  }||v s y yrV   )r   rW   s      r    r=   z-PassThroughEndpointLogging.is_anthropic_route  s"    22E	! 3 r"   c                 2    | j                   D ]  }||v s y y )NT)r   rW   s      r    r?   z*PassThroughEndpointLogging.is_cohere_route  s    //E	! 0r"   c                 X    t        |      }|j                  dk(  ryd|j                  v ryy)Nzapi.assemblyai.comTz/transcriptF)r   hostnamepath)r   r8   
parsed_urls      r    rL   z.PassThroughEndpointLogging.is_assemblyai_route  s0    i(
"66joo-r"   c                 \    t        |      }| j                  D ]  }||j                  v s y yrV   )r   r   r]   )r   r8   r^   rX   s       r    rP   z,PassThroughEndpointLogging.is_langfuse_route  s0    i(
11E
' 2 r"   r3   c                     |j                  d      2|j                  d      |d<   |j                  d      |j                  d<   |S )z
        Helper function to set the cost per request in the logging object

        Only set the cost per request if it's set in the passthrough logging payload.
        If it's not set, don't set it in the logging object.
        cost_per_requestresponse_cost)getrK   )r   r#   rG   r3   s       r    rR   z0PassThroughEndpointLogging._set_cost_per_request!  sV     '**+=>J&A&E&E"'F?# ,//0BC **?; r"   N)__name__
__module____qualname__r!   LiteLLMLoggingObjr   r   r	   r0   strr   boolr4   httpxResponser   rF   r
   rT   r;   r=   r?   rL   rP   rR   r*   r"   r    r   r       s   6*$
&$
 +0-2+
$
 $
 $
 $
 $
LMM  ~M 	M
 'M M M M M M^T
T
  ~T
 '	T

 T
 T
 T
 T
 T
 T
 &GT
l C  
S 3 & &G 	r"   r   )r1   r   typingr   r   r   urllib.parser   rj   *litellm.litellm_core_utils.litellm_loggingr   rg   litellm.proxy._typesr	   :litellm.types.passthrough_endpoints.pass_through_endpointsr
   litellm.types.utilsr   litellm.utilsr   r+   ;llm_provider_handlers.anthropic_passthrough_logging_handlerr   :llm_provider_handlers.assembly_passthrough_logging_handlerr   8llm_provider_handlers.cohere_passthrough_logging_handlerr   8llm_provider_handlers.vertex_passthrough_logging_handlerr   r@   r   r*   r"   r    <module>rw      sW      ' ' !  S G B : &E%F "X Xr"   