o
    ưik                     @   s   d dl Z d dlmZmZmZmZ d dl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 G dd deZG d	d
 d
eZdS )    N)AnyDictListOptional)	BaseModelField)verbose_logger)CustomLogger)get_async_httpx_clienthttpxSpecialProviderc                   @   s   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eed< ed	d
dZee	ee
f  ed< edddZeed< d	Zeee  ed< d	Zee	ee
f  ed< d	S )LLMResponse
latency_msstatus_code
input_textoutput_text	node_typemodelnum_input_tokensnum_output_tokensNzCOptional. When available, logprobs are used to compute Uncertainty.)defaultdescriptionoutput_logprobs.z3timestamp constructed in "%Y-%m-%dT%H:%M:%S" format)r   
created_attagsuser_metadata)__name__
__module____qualname__int__annotations__strr   r   r   r   r   r   r   r   r    r!   r!   S/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/galileo.pyr      s$   
 r   c                   @   sP   e Zd ZdddZdd Zdd Zd	ed
ededefddZdd Zdd Z	dS )GalileoObservereturnNc                 C   s@   g | _ d| _tdd | _tdd | _d | _ttj	d| _
d S )N   ZGALILEO_BASE_URLZGALILEO_PROJECT_ID)Zllm_provider)in_memory_records
batch_sizeosgetenvbase_url
project_idheadersr
   r   ZLoggingCallbackasync_httpx_handler)selfr!   r!   r"   __init__%   s   zGalileoObserve.__init__c                 C   sZ   ddd}t jj| j d|tdtddd}| d	 }ddd
| d| _d S )Nzapplication/jsonz!application/x-www-form-urlencoded)acceptContent-Typez/loginZGALILEO_USERNAMEZGALILEO_PASSWORD)usernamepassword)urlr,   dataaccess_tokenzBearer )r0   r1   Authorization)litellmZmodule_level_clientpostr*   r(   r)   jsonr,   )r.   r,   Zgalileo_login_responser6   r!   r!   r"   set_galileo_headers0   s   
	z"GalileoObserve.set_galileo_headersc                 C   s   d }|d ur| dd dkst|tjrd }|S |d ur.t|tjr.|d d d  }|S |d ur@t|tjr@|jd j}|S |d urNt|tj	rN|d }|S )N	call_typeZ	embeddingchoicesr   messager5   )
get
isinstancer8   ZEmbeddingResponseZModelResponser:   ZTextCompletionResponser=   textZImageResponse)r.   response_objkwargsoutputr!   r!   r"   get_output_str_from_responseH   s,   




z+GalileoObserve.get_output_str_from_responserC   rB   
start_timeend_timec                    s   t d t||  d }|dd}tjj||d}|di p%i }|dd}	|d	d}
| j||d
}|d urlt	|d||||dd|	|
|
dd	}| }| j| t| j| jkrn|  I d H  d S d S d S )NzOn Async Successi  r<   r8   )r5   r<   usageZprompt_tokensr   Zcompletion_tokens)rB   rC      r   -z%Y-%m-%dT%H:%M:%S)	r   r   r   r   r   r   r   r   r   )r   debugr   total_secondsr?   r8   utilsZget_formatted_promptrE   r   strftimeZ
model_dumpr&   appendlenr'   flush_in_memory_records)r.   rC   rB   rF   rG   Z_latency_msZ
_call_typer   Z_usager   r   r   Zrequest_recordZrequest_dictr!   r!   r"   async_log_success_event^   sB   

z&GalileoObserve.async_log_success_eventc                    sz   t d | jj| j d| j d| jd| jidI d H }|jdkr-t d g | _d S t d t d	|j	|j d S )
Nzflushing in memory recordsz
/projects/z/observe/ingestrecords)r4   r,   r:   rI   z5Galileo Logger:successfully flushed in memory recordsz1Galileo Logger: failed to flush in memory recordsz'Galileo Logger error=%s, status code=%s)
r   rK   r-   r9   r*   r+   r,   r&   r   rA   )r.   responser!   r!   r"   rQ      s$   



z&GalileoObserve.flush_in_memory_recordsc                    s   t d d S )NzOn Async Failure)r   rK   )r.   rC   rB   rF   rG   r!   r!   r"   async_log_failure_event   s   z&GalileoObserve.async_log_failure_event)r$   N)
r   r   r   r/   r;   rE   r   rR   rQ   rU   r!   r!   r!   r"   r#   $   s    

)r#   )r(   typingr   r   r   r   Zpydanticr   r   r8   Zlitellm._loggingr   Z"litellm.integrations.custom_loggerr	   Z&litellm.llms.custom_httpx.http_handlerr
   r   r   r#   r!   r!   r!   r"   <module>   s    