o
    Zh"                  	   @   s  d dl Z d dlZd dlmZmZ d dlmZmZmZ d dlm	Z	 z
d dl
mZ dZW n ey5   dZY nw eeZed Zerd*d
ededefddZdejdefddZdejdefddZdejdeeef fddZdejdeeef fddZdejdeeef fddZdejdeeef fddZdej deeef fddZ!dej"deeef fdd Z#dej$deeef fd!d"Z%dejdeeef fd#d$Z&	d+d%ee d&ee d'ee defd(d)Z'dS dS ),    N)datetimetimezone)AnyLiteralOptional)uuid4)tracingTF)toolchainllmZ	retrieverZ	embeddingpromptparseroutputdatadefault_keyreturnc              
   C   s   t | tr	| }|S t | tr5zt| }t |tr|}n|| i}W |S W |S  tjy4   || i}Y |S w | durwt| drwt| jrwt | t	swz
| jddd}W |S  t
yv } ztdt	|  d|  || i}W Y d}~|S d}~ww || i}|S )a8  Parse inputs or outputs into a dictionary format.

        Args:
            data: The data to parse (can be inputs or outputs)
            default_key: The default key to use if data is not a dict
                ("input" or "output")

        Returns:
            Dict: The parsed data as a dictionary
        N
model_dumpTjsonZexclude_nonemodez&Failed to use model_dump to serialize z
 to JSON: )
isinstancedictstrr   loadsJSONDecodeErrorhasattrcallabler   type	Exceptionloggerdebug)r   r   Zdata_Zparsed_jsone r"   V/var/www/html/lang_env/lib/python3.10/site-packages/langsmith/wrappers/_agent_utils.pyparse_io   sH   





	r$   spanc                 C   s6   t | jdd }|dv rdS |dv rdS |dv rdS dS )Nr   )ZagenthandoffZcustomr
   )functionZ	guardrailr	   )
generationresponser   )getattr	span_datar%   Z	span_typer"   r"   r#   get_run_type<   s   r-   c                 C   sR   t | jdr| jjr| jjS t| jdd }|dkrdS |dkr!dS |dkr'dS d	S )
Nnamer   r(   Z
Generationr)   Responser&   ZHandoffSpan)r   r+   r.   r*   r,   r"   r"   r#   get_run_nameG   s   r1   r+   c                 C   s   t | jdt | jddS )Ninputr   )inputsoutputs)r$   r2   r   r+   r"   r"   r#   _extract_function_span_dataT   s   

r6   c                 C   s\   t | jdt | jd| j| jdd}| jr,| jd| jd| jdd|d	 d
< |S )Nr2   r   )modelmodel_config)r3   r4   invocation_paramstotal_tokensZprompt_tokensZcompletion_tokens)r:   Zinput_tokensZoutput_tokensr4   usage_metadata)r$   r2   r   r7   r8   usageget)r+   r   r"   r"   r#   _extract_generation_span_data\   s   




r>   c                    s4  i  | j d ur| j | jjd d< | jd ur| jjddd}d|dg i d< |dd  }rdd	|v rG|d	|d
< |d
 dd|d
 d< d|v r^|d|d< |d dd|d d< | d d< dd | D  d<  fdd| D }| d d d d d dddd | d<  S )N)r2   instructionsr3   Tr   r   r   r4   r<   Zoutput_tokens_detailsZoutput_token_detailsZreasoning_tokensr   	reasoningZinput_tokens_detailsZinput_token_detailsZcached_tokensZ
cache_readr;   c                 S   s   i | ]\}}|d v r||qS ))
max_output_tokensr7   Zparallel_tool_callsr@   temperaturetextZtool_choicetoolsZtop_pZ
truncationr"   .0kvr"   r"   r#   
<dictcomp>   s    z/_extract_response_span_data.<locals>.<dictcomp>r9   c                    s,   i | ]\}}|h d   d vr||qS )>   r   r<   r?   r9   )unionrE   r   r"   r#   rI      s    r7   rA   rB   ZchatZopenai)Zls_model_nameZls_max_tokensZls_temperatureZls_model_typeZls_providermetadata)r2   r)   r?   r   popitemsupdater=   )r+   r)   r<   rL   r"   rK   r#   _extract_response_span_datao   sP   






	rP   c                 C   s   | j | jdd| jidS )N)rD   handoffsoutput_type)r9   rL   )rD   rQ   rR   r5   r"   r"   r#   _extract_agent_span_data   s   rS   c                 C   s   d| j | jdiS )Nr3   Z
from_agentZto_agentrT   r5   r"   r"   r#   _extract_handoff_span_data   s
   rU   c                 C   s   dd| j iiS )NrL   	triggered)rV   r5   r"   r"   r#   _extract_guardrail_span_data   s   rW   c                 C   s
   d| j iS )NrL   rK   r5   r"   r"   r#   _extract_custom_span_data   s   
rX   c                 C   s   i }t | jtjr|t| j |S t | jtjr$|t| j |S t | jtjr5|t	| j |S t | jtj
rF|t| j |S t | jtjrW|t| j |S t | jtjrh|t| j |S t | jtjry|t| j |S i S N)r   r+   r   FunctionSpanDatarO   r6   GenerationSpanDatar>   ResponseSpanDatarP   AgentSpanDatarS   HandoffSpanDatarU   GuardrailSpanDatarW   CustomSpanDatarX   )r%   r   r"   r"   r#   extract_span_data   s.   
ra   
start_timerun_idparent_dotted_orderc                 C   sD   | pt tj}|ptt }|d| }|dur |d | S |S )z3Create a dotted order from a start time and run id.z%Y%m%dT%H%M%S%fZN.)r   nowr   utcr   r   strftime)rb   rc   rd   stZid_Zcurrent_dotted_orderr"   r"   r#   ensure_dotted_order   s   rj   )r   rY   )(r   loggingr   r   typingr   r   r   uuidr   Zagentsr   ZHAVE_AGENTSImportError	getLogger__name__r   ZRunTypeTr   r   r$   r0   r-   r1   rZ   r6   r[   r>   r\   rP   r]   rS   r^   rU   r_   rW   r`   rX   ra   rj   r"   r"   r"   r#   <module>   sr    
(





@




 