
    g%                     N    d dl Z d dlZd dlmZ d dlmZ d dlmZ  G d de      Zy)    N)Optional)verbose_logger)CustomLoggerc                   p    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zdee   defdZd Zd Zy)MlflowLoggerc                 h    ddl m}  |       | _        i | _        t	        j
                         | _        y )Nr   )MlflowClient)mlflow.trackingr	   _client_stream_id_to_span	threadingLock_lock)selfr	   s     P/var/www/openai/venv/lib/python3.12/site-packages/litellm/integrations/mlflow.py__init__zMlflowLogger.__init__
   s$    0#~"$^^%
    c                 ,    | j                  ||||       y N_handle_successr   kwargsresponse_obj
start_timeend_times        r   log_success_eventzMlflowLogger.log_success_event       V\:xHr   c                 4   K   | j                  ||||       y wr   r   r   s        r   async_log_success_eventz$MlflowLogger.async_log_success_event        V\:xH   c                 n   ddl m} 	 t        j                  d       |j	                  d      r| j                  ||||       y
| j                  ||      }t        |j                         dz        }| j                  |||j                  |       y
# t        $ r t        j                  dd	       Y y
w xY w)z
        Log the success event as an MLflow span.
        Note that this method is called asynchronously in the background thread.
        r   SpanStatusCodez&MLflow logging start for success eventstream    eAspanoutputsstatusend_time_nszMLflow Logging ErrorT
stack_infoN)mlflow.entitiesr%   r   debugget_handle_stream_event_start_span_or_traceint	timestamp_end_span_or_traceOK	Exception)r   r   r   r   r   r%   r)   r,   s           r   r   zMlflowLogger._handle_success   s    
 	3	J  !IJzz(#))&,
HU00D!("4"4"6"<=''(),, +	 (   	J  !7DI	Js   :B AB  B43B4c                 ,    | j                  ||||       y r   _handle_failurer   s        r   log_failure_eventzMlflowLogger.log_failure_event0   r   r   c                 4   K   | j                  ||||       y wr   r:   r   s        r   async_log_failure_eventz$MlflowLogger.async_log_failure_event3   r!   r"   c                 v   ddl m}m} 	 | j                  ||      }t	        |j                         dz        }|j                  d      x}	r |j                  |j                  |	             | j                  |||j                  |       y	# t        $ r$}
t        j                  d|
 d       Y d	}
~
y	d	}
~
ww xY w)
z
        Log the failure event as an MLflow span.
        Note that this method is called *synchronously* unlike the success handler.
        r   )	SpanEventr%   r'   	exceptionr(   zMLflow Logging Error - Tr-   N)r/   r@   r%   r3   r4   r5   r1   	add_eventfrom_exceptionr6   ERRORr8   r   r0   )r   r   r   r   r   r@   r%   r)   r,   rA   es              r   r;   zMlflowLogger._handle_failure6   s    
 	>	Q,,VZ@Dh002S89K #JJ{33y3y77	BC##$%++'	 $   	Q  #:1#!>4PP	Qs   B B 	B8B33B8c                 F   ddl m} |j                  d      }|| j                  vrD| j                  5  || j                  vr!| j                  ||      }|| j                  |<   ddd       | j                  |   }| j                  ||       |j                  d      x}rlt        |j                         dz        }	| j                  |||j                  |	       | j                  5  | j                  j                  |       ddd       yy# 1 sw Y   xY w# 1 sw Y   yxY w)a  
        Handle the success event for a streaming response. For streaming calls,
        log_success_event handle is triggered for every chunk of the stream.
        We create a single span for the entire stream request as follows:

        1. For the first chunk, start a new span and store it in the map.
        2. For subsequent chunks, add the chunk as an event to the span.
        3. For the final chunk, end the span and remove the span from the map.
        r   r$   litellm_call_idNcomplete_streaming_responser'   r(   )r/   r%   r1   r   r   r3   _add_chunk_eventsr4   r5   r6   r7   pop)
r   r   r   r   r   r%   rG   r)   final_responser,   s
             r   r2   z!MlflowLogger._handle_stream_eventP   s    	3 **%67$"9"99"$*A*AA44VZHD?CD++O<  &&7t\2 $ZZ(EFF>Fh002S89K##&%(('	 $  ''++O<  G . s   0D%DDD c                    ddl m} 	 |j                  D ]H  }|j                   |ddt	        j
                  |j                  j                               i             J y # t        $ r t        j                  dd       Y y w xY w)	Nr   )r@   streaming_chunkdelta)name
attributesz!Error adding chunk events to spanTr-   )r/   r@   choicesrB   jsondumpsrN   
model_dumpr8   r   r0   )r   r)   r   r@   choices        r   rI   zMlflowLogger._add_chunk_eventsy   su    -		W&...$+TZZ8O8O8Q-R#S /  	W  !DQUV	Ws   AA    BBc                     d|j                  d      i}dD ],  }|j                  di       j                  |d      x}s(|||<   . |S )z.Construct span inputs with optional parametersmessages)	functionstoolsr&   tool_choiceuseroptional_paramsN)r1   rJ   )r   r   inputskeyvalues        r   _construct_inputzMlflowLogger._construct_input   sT    fjj45JC

#4b9==c4HHuH#s K r   c           
         |j                  d      |j                  d      |j                  d      d}|j                  d      }|r|j                  |j                  d      |j                  d      |j                  d      |j                  d	      |j                  d
      d|j                  d      |j                  d      |j                  d      d       |S |j                  di       }|j                  |j                  d      |j                  d      |j                  d      |j                  d      |j                  d      d       |S )a  
        Extract span attributes from kwargs.

        With the latest version of litellm, the standard_logging_object contains
        canonical information for logging. If it is not present, we extract
        subset of attributes from other kwargs.
        rG   	call_typemodel)rG   rb   rc   standard_logging_objectapi_base	cache_hitcompletion_tokensprompt_tokenstotal_tokens)rg   rh   ri   responseresponse_costsaved_cache_cost)re   rf   usageraw_llm_responserk   rl   litellm_paramscustom_llm_provider)rc   rf   rp   re   rk   )r1   update)r   r   rP   standard_objro   s        r   _extract_attributesz MlflowLogger._extract_attributes   sE     &zz*;<K0ZZ(


 zz";< , 0 0 <!-!1!1+!>-9-=-=>Q-R)5)9)9/)J(4(8(8(H
 )5(8(8(D%1%5%5o%F(4(8(89K(L2  $ZZ(8"=N#ZZ0!'K!8+1::6K+L . 2 2: >%+ZZ%@ r   rb   returnc                 h    ddl m} |dv r|j                  S |dk(  r|j                  S |j                  S )Nr   )SpanType)
completionacompletion
embeddings)r/   rv   LLM	EMBEDDING)r   rb   rv   s      r   _get_span_typezMlflowLogger._get_span_type   s6    ,55<<,&%%%<<r   c           	         ddl }|j                  dd      }d| }| j                  |      }t        |j	                         dz        }| j                  |      }| j                  |      }	|j                         x}
r6| j                  j                  ||
j                  |
j                  |||	|      S | j                  j                  ||||	|      S )	z
        Start an MLflow span or a trace.

        If there is an active span, we start a new span as a child of
        that span. Otherwise, we start a new trace.
        r   Nrb   rw   zlitellm-r'   )rO   
request_id	parent_id	span_typer]   rP   start_time_ns)rO   r   r]   rP   r   )mlflowr1   r|   r4   r5   r`   rs   get_current_active_spanr   
start_spanr~   span_idstart_trace)r   r   r   r   rb   	span_namer   r   r]   rP   active_spans              r   r3   z!MlflowLogger._start_span_or_trace   s     	JJ{L9	yk*	''	2	J002S89&&v.--f5
 88::;:<<**&11%--#%+ +   <<++#%+ ,  r   c                     |j                   *| j                  j                  |j                  |||       y| j                  j	                  |j                  |j
                  |||       y)zEnd an MLflow span or a trace.N)r~   r*   r+   r,   )r~   r   r*   r+   r,   )r   r   	end_tracer~   end_spanr   )r   r)   r*   r,   r+   s        r   r6   zMlflowLogger._end_span_or_trace   sc    >>!LL""??'	 #  LL!!??' " r   N)__name__
__module____qualname__r   r   r    r   r<   r>   r;   r2   rI   r`   rs   r   strr|   r3   r6    r   r   r   r   	   sd    &IIJ0IIQ4'=RW(T   #  "Hr   r   )	rR   r   typingr   litellm._loggingr   "litellm.integrations.custom_loggerr   r   r   r   r   <module>r      s"       + ;n< nr   