o
    +if&                     @   s  d Z ddlZddlZ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mZmZ ddlZddlmZ eeZG dd deZG dd deZG d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZdefddZ dee!df defddZ"dej#ddfddZ$deej%e!f de!fd d!Z&ej'd"d#d$e(d%e!ddfd&d'Z)d%ee!ef de!fd(d)Z*d%ee!ef dee!ef fd*d+Z+d%ee!ef de	e!ef fd,d-Z,d.ee!ef de
e	e!ef  fd/d0Z-d1ee!ef de	e!ef fd2d3Z.d.ee!ef de!fd4d5Z/d1ee!ef de!fd6d7Z0ej'd"d#de
e! fd8d9Z1d%ej2de3fd:d;Z4d<e5defd=d>Z6dDdee! fd@dAZ7G dBdC dCej8Z9dS )EzGeneric utility functions.    N)AnyCallableDictListMappingOptionalTupleUnion)schemasc                   @      e Zd ZdZdS )LangSmithErrorz=An error occurred while communicating with the LangSmith API.N__name__
__module____qualname____doc__ r   r   H/var/www/html/corbot_env/lib/python3.10/site-packages/langsmith/utils.pyr          r   c                   @   r   )LangSmithAPIErrorz9Internal server error while communicating with LangSmith.Nr   r   r   r   r   r      r   r   c                   @   r   )LangSmithUserErrorzAUser error caused an exception when communicating with LangSmith.Nr   r   r   r   r   r      r   r   c                   @   r   )LangSmithRateLimitErrorz7You have exceeded the rate limit for the LangSmith API.Nr   r   r   r   r   r      r   r   c                   @   r   )LangSmithAuthErrorz-Couldn't authenticate with the LangSmith API.Nr   r   r   r   r   r   !   r   r   c                   @   r   )LangSmithNotFoundErrorz%Couldn't find the requested resource.Nr   r   r   r   r   r   %   r   r   c                   @   r   )LangSmithConflictErrorzThe resource already exists.Nr   r   r   r   r   r   )   r   r   c                   @   r   )LangSmithConnectionErrorz&Couldn't connect to the LangSmith API.Nr   r   r   r   r   r   -   r   r   returnc                   C   s    t jdt jdd dkS )z"Return True if tracing is enabled.LANGCHAIN_TRACING_V2LANGCHAIN_TRACING true)osenvirongetlowerr   r   r   r   tracing_is_enabled1   s   r%   
arg_groups.c                     s   dt dt f fdd}|S )z7Validate specified keyword args are mutually exclusive.funcr   c                    s*   t  dtdtdtf fdd}|S )Nargskwargsr   c                     s\    fddD }dd t |D }|r'fdd|D }tdd| | i  S )z3Validate exactly one arg in each group is not None.c                    s"   g | ]}t  fd d|D qS )c                 3   s"    | ]}  |d urdV  qd S )N   )r#   ).0argr)   r   r   	<genexpr>C   s     zJxor_args.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>.<genexpr>)sum)r+   	arg_groupr-   r   r   
<listcomp>B   s    z@xor_args.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>c                 S   s   g | ]
\}}|d kr|qS )r*   r   )r+   icountr   r   r   r1   F   s    c                    s   g | ]	}d   | qS ), )join)r+   r2   r&   r   r   r1   H   s    zFExactly one argument in each of the following groups must be defined: r4   )	enumerate
ValueErrorr5   )r(   r)   countsinvalid_groupsinvalid_group_names)r&   r'   r-   r   wrapper?   s   
z,xor_args.<locals>.decorator.<locals>.wrapper)	functoolswrapsr   )r'   r<   r6   )r'   r   	decorator>   s   zxor_args.<locals>.decorator)r   )r&   r?   r   r6   r   xor_args;   s   r@   responsec              
   C   s>   z|    W dS  tjy } z
tt|| j|d}~ww )z&Raise an error with the response text.N)raise_for_statusrequests	HTTPErrorstrtext)rA   er   r   r   raise_for_status_with_textU   s   rH   enuc                 C   s   t | tjr	| jS | S )zGet the value of a string enum.)
isinstanceenumEnumvalue)rI   r   r   r   get_enum_value]   s   rN   r*   )maxsizelevelmessagec                 C   s   t | | d S )N)_LOGGERlog)rP   rQ   r   r   r   log_onced   s   rT   c                 C   sd   | st dd| v r"d| vrt d|  d| d d dd S d	| vr.t d
|  d| d	 S )NMessage is empty.lcid*Unexpected format for serialized message: z Message does not have an id.Messager   type&Unexpected format for stored message: z Message does not have a type.)r8   replacer$   rQ   r   r   r   _get_message_typei   s   

r_   c                 C   sT   | st dd| v rd| vrt d|  d| d S d| vr&t d|  d| d S )	NrU   rV   r)   rX   z Message does not have kwargs.datar\   z Message does not have data.)r8   r^   r   r   r   _get_message_fields|   s   

ra   c                 C   s   t | }t| }||dS )z&Extract message from a message object.r[   r`   )r_   ra   )rQ   message_typemessage_datar   r   r   _convert_message   s   
re   inputsc                 C   s@   d| v rdd | d D S d| v rt | d gS td|  d)Nmessagesc                 S   s   g | ]}t |qS r   )re   )r+   rQ   r   r   r   r1      s    z,get_messages_from_inputs.<locals>.<listcomp>rQ   z-Could not find message(s) in run with inputs .)re   r8   )rf   r   r   r   get_messages_from_inputs   s
   ri   outputsc                 C   sr   d| vrt d|  d| d }t|dkr#t dt| d| d|d }d|vr3t d	| d
t|d S )Ngenerations,No generations found in in run with output: rh   r*   z3Chat examples expect exactly one generation. Found z generations: r   rQ   z"Unexpected format for generation: z%. Generation does not have a message.)r8   lenre   rj   rk   first_generationr   r   r   #get_message_generation_from_outputs   s"   
rp   c                 C   sT   d| v r| d S d| v r"| d }t |dkr|d S td|  dtd|  d)	Npromptpromptsr*   r   z$Multiple prompts in run with inputs z!. Please create example manually.z)Could not find prompt in run with inputs rh   )rm   r8   )rf   rr   r   r   r   get_prompt_from_inputs   s   
rs   c                 C   s`   d| vrt d|  d| d }t|dkrt d| |d }d|vr,t d| |d S )	Nrk   rl   rh   r*   zMultiple generations in run: r   rF   zNo text in generation: )r8   rm   rn   r   r   r   get_llm_generation_from_outputs   s   rt   c                   C   sz   zt jg dt jt jd ddgW S  t jtfy<   zt jddgt jt jd dgW  Y S  t jtfy;   tdw w )z7Get the correct docker compose command for this system.)dockercompose	--version)stdoutstderrru   rv   zdocker-composerw   zNeither 'docker compose' nor 'docker-compose' commands are available. Please install the Docker server following the instructions for your operating system at https://docs.docker.com/engine/install/)
subprocess
check_callDEVNULLCalledProcessErrorFileNotFoundErrorr8   r   r   r   r   get_docker_compose_command   s*   
r   c                 C   s<   | j d| jid}| jrt| jdkri | j|d d< |S )z*Convert a LangChain message to an example.contentrb   r   r`   additional_kwargs)r[   r   r   rm   )rQ   	convertedr   r   r   convert_langchain_message   s   r   objc                 C   s@   t tt| ddttt| ddtt| dott| dtgS )z
    Check if the given object is similar to BaseMessage.

    Args:
        obj (object): The object to check.

    Returns:
        bool: True if the object is similar to BaseMessage, False otherwise.
    r   Nr   r[   )allrJ   getattrrE   dicthasattr)r   r   r   r   is_base_message_like   s   
r   Tc              
   C   s0   t jdt jdt jd| rdS dS )z,Get the project name for a LangSmith tracer.HOSTED_LANGSERVE_PROJECT_NAMELANGCHAIN_PROJECTLANGCHAIN_SESSIONdefaultN)r!   r"   r#   )return_default_valuer   r   r   get_tracer_project  s   r   c                   @   s   e Zd ZdZdefddZdS )FilterPoolFullWarningzFFilter urrllib3 warnings logged when the connection pool isn't reused.r   c                 C   s   d|  vS )zJurllib3.connectionpool:Connection pool is full, discarding connection: ...z.Connection pool is full, discarding connection)
getMessage)selfrecordr   r   r   filter  s   
zFilterPoolFullWarning.filterN)r   r   r   r   boolr   r   r   r   r   r     s    r   )T):r   rK   r=   loggingr!   rz   typingr   r   r   r   r   r   r   r	   rC   	langsmithr
   
ls_schemas	getLoggerr   rR   	Exceptionr   r   r   r   r   r   r   r   r   r%   rE   r@   ResponserH   rL   rN   	lru_cacheintrT   r_   ra   re   ri   rp   rs   rt   r   BaseMessageLiker   r   objectr   r   Filterr   r   r   r   r   <module>   sJ    (


""&"
