o
    ưi                  
   @   s  U d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	 dZ
ee ed< dd Zdeeef fdd	Zd
edee fddZ	dd
edee dee dee fddZdd Zdeeef deeef fddZdeeeef  de	eeeef  eeeef  f fddZdS )    N)AnyDictFinalListOptionalTuplez~/.opik.configCONFIG_FILE_PATH_DEFAULTc               
   C   s   t  } g d}d}t| |\}}t|d> |\}}t|d> |\}}|dO }||d krH||d krH||d krH|d	 d
k rG|d	  d7  < n
|||df|d d < d|d	 B }	td}
|dd|dd|dd|	dd|
  	S )N)r   r   r   r   l     Ys       i p  r            i?  i      z>08x-z>04x)timetime_nsdivmodosurandomhex)nslastZsixteen_secst1Zrest1t2Zrest2t3_Zt4Zrand r   V/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/opik/utils.pycreate_uuid7	   s   $
,r   returnc                     sH   t jt} t   |   fdd  D }d|v r"|d S i S )Nc                    s   i | ]
}|t  |qS r   )dictitems).0sectionconfigr   r   
<dictcomp>.   s    z*_read_opik_config_file.<locals>.<dictcomp>Zopik)r   path
expanduserr   configparserConfigParserreadsections)Zconfig_pathconfig_valuesr   r$   r   _read_opik_config_file(   s   

r.   keyc                 C   s   d}t ||   d S )NZopik_)r   getenvupper)r/   Z
env_prefixr   r   r   _get_env_variable8   s   r2   
user_valuedefault_valuec                 C   s:   |dur|S t | }|dur|S t }| |v r||  S |S )z
    Get the configuration value of a variable, order priority is:
    1. user provided value
    2. environment variable
    3. Opik configuration file
    4. default value
    N)r2   r.   )r/   r3   r4   Z	env_valuer-   r   r   r   get_opik_config_variable=   s   r5   c                 C   sD   i }| j d ur| j |d< | jd ur| j|d< | jd ur | j|d< |S )Ncompletion_tokensprompt_tokenstotal_tokens)r6   r7   r8   )usageZ
usage_dictr   r   r   create_usage_objectZ   s   





r:   xc                 C   s   dd |   D S )zRemove None values from dict.c                 S   s   i | ]\}}|d ur||qS )Nr   )r"   kvr   r   r   r&   h   s    z!_remove_nulls.<locals>.<dictcomp>)r!   )r;   r   r   r   _remove_nullsf   s   r>   payloadc                 C   s$   dd | D }dd | D }||fS )aL  
    Separate traces and spans from payload.

    Traces are identified by not having a "type" field.
    Spans are identified by having a "type" field.

    Args:
        payload: List of dicts containing trace and span data

    Returns:
        Tuple of (traces, spans) where both are lists of dicts with null values removed
    c                 S   s   g | ]
}d |vrt |qS typer>   r"   r;   r   r   r   
<listcomp>z       z5get_traces_and_spans_from_payload.<locals>.<listcomp>c                 S   s   g | ]
}d |v rt |qS r@   rB   rC   r   r   r   rD   {   rE   r   )r?   Ztracesspansr   r   r   !get_traces_and_spans_from_payloadk   s   rG   )NN)r)   r   r   typingr   r   r   r   r   r   r   str__annotations__r   r.   r2   r5   r:   r>   rG   r   r   r   r   <module>   s2   
  
""