U
    hb                     @  s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
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 ddlmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z" ddl#m$Z% ddl&Z&ddl'Z'ddl(m)Z) ddl*m+Z+ dd	l,m-Z. e/e0Z1G d
d de2Z3G dd de3Z4G dd de3Z5G dd de3Z6G dd de3Z7G dd de3Z8G dd de3Z9G dd de3Z:G dd de3Z;G dd de<Z=G dd de=Z>dd d!d"d#d$Z?d%d&d'd(Z@d)d*d+d,d-ZAd.d/d0d1d2ZBd3d4d5d6d7ZCejDd8d9d:d4d/d;d<d=ZEd>d4d?d@dAZFd>d>d?dBdCZGd>dDd?dEdFZHd>dGdHdIdJZId>dDdKdLdMZJd>d4dHdNdOZKd>d4dKdPdQZLejDd8d9dRd&dSdTZMdUdVd?dWdXZNdYd%dZd[d\ZOejDd]d9dd^d_d4d`dad`dbdcddZPejDd8d9dd`d&dfdgZQG dhdi diejRZSG djdk dkejRZTG dldm dme+ZUeV ZWejXdndodpdqdrdsZYd`d`dtdudvZZejXddwdxdpdydzd{Z[ejXdd|dxdpdyd}d~Z\d4d&ddZ]e dZ^dddd:d:ddddZ_dddddZ`d4d4d%dddZad4ddddZbe)dZcG dd deZdd`d4dddZed`d`dddZfd4d%dddZgejDdd9d`d4dddZhdd*d:d4dddZidS )zGeneric utility functions.    )annotationsN)FutureThreadPoolExecutor)AnyCallableDict	GeneratorIterableIteratorListLiteralMappingOptionalSequenceTupleTypeVarUnioncast)parse)	ParamSpec)Retry)schemasc                   @  s   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   3/tmp/pip-unpacked-wheel-cqvhoa9t/langsmith/utils.pyr   0   s   r   c                   @  s   e Zd ZdZdS )LangSmithAPIErrorz9Internal server error while communicating with LangSmith.Nr   r   r   r   r   r    4   s   r    c                   @  s   e Zd ZdZdS )LangSmithRequestTimeoutz*Client took too long to send request body.Nr   r   r   r   r   r!   8   s   r!   c                   @  s   e Zd ZdZdS )LangSmithUserErrorzAUser error caused an exception when communicating with LangSmith.Nr   r   r   r   r   r"   <   s   r"   c                   @  s   e Zd ZdZdS )LangSmithRateLimitErrorz7You have exceeded the rate limit for the LangSmith API.Nr   r   r   r   r   r#   @   s   r#   c                   @  s   e Zd ZdZdS )LangSmithAuthErrorz-Couldn't authenticate with the LangSmith API.Nr   r   r   r   r   r$   D   s   r$   c                   @  s   e Zd ZdZdS )LangSmithNotFoundErrorz%Couldn't find the requested resource.Nr   r   r   r   r   r%   H   s   r%   c                   @  s   e Zd ZdZdS )LangSmithConflictErrorzThe resource already exists.Nr   r   r   r   r   r&   L   s   r&   c                   @  s   e Zd ZdZdS )LangSmithConnectionErrorz&Couldn't connect to the LangSmith API.Nr   r   r   r   r   r'   P   s   r'   c                   @  s   e Zd ZdZdS )LangSmithWarningzBase class for warnings.Nr   r   r   r   r   r(   W   s   r(   c                   @  s   e Zd ZdZdS )LangSmithMissingAPIKeyWarningzWarning for missing API key.Nr   r   r   r   r   r)   [   s   r)   zOptional[dict]zUnion[bool, Literal['local']])ctxreturnc                 C  sT   ddl m}m} | p| }|d dk	r.|d S | r8dS tdtddd	d	}|d
kS )z"Return True if tracing is enabled.r   )get_current_run_treeget_tracing_contextZenabledNTZ
TRACING_V2ZTRACING defaulttrue)Zlangsmith.run_helpersr,   r-   get_env_var)r*   r,   r-   ZtcZ
var_resultr   r   r   tracing_is_enabled_   s    
r3   boolr+   c                   C  s   t ddddkS )z"Return True if testing is enabled.ZTEST_TRACKINGr.   r/   falser2   r   r   r   r   test_tracking_is_disabledr   s    r8   zTuple[str, ...]r   )
arg_groupsr+   c                    s   ddd fdd}|S )z7Validate specified keyword args are mutually exclusive.r   )funcr+   c                   s&   t  dddd fdd}|S )Nr   )argskwargsr+   c                    sX    fddD }dd t |D }|rNfdd|D }tdd| |  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 k	rdV  qd S )N   )get).0argr<   r   r   	<genexpr>   s      zJxor_args.<locals>.decorator.<locals>.wrapper.<locals>.<listcomp>.<genexpr>)sum)r?   Z	arg_grouprA   r   r   
<listcomp>~   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   rD      s      c                   s   g | ]}d   | qS ), )join)r?   rF   r9   r   r   rD      s     zFExactly one argument in each of the following groups must be defined: rH   )	enumerate
ValueErrorrI   )r;   r<   countsZinvalid_groupsZinvalid_group_names)r9   r:   rA   r   wrapper{   s    
z,xor_args.<locals>.decorator.<locals>.wrapper)	functoolswraps)r:   rN   rJ   )r:   r   	decoratorz   s    zxor_args.<locals>.decoratorr   )r9   rQ   r   rJ   r   xor_argsw   s    rR   z(Union[requests.Response, httpx.Response]None)responser+   c              
   C  sJ   z|    W n8 tjk
rD } ztt|| j|W 5 d}~X Y nX dS )z&Raise an error with the response text.N)raise_for_statusrequests	HTTPErrorstrtext)rT   er   r   r   raise_for_status_with_text   s    r[   zUnion[enum.Enum, str]rX   )enur+   c                 C  s   t | tjr| jS | S )zGet the value of a string enum.)
isinstanceenumEnumvalue)r\   r   r   r   get_enum_value   s    ra   r=   )maxsizeint)levelmessager+   c                 C  s   t | | dS )z4Log a message at the specified level, but only once.N)_LOGGERlog)rd   re   r   r   r   log_once   s    rh   zMapping[str, Any])re   r+   c                 C  sh   | st dd| krDd| kr,t d|  d| d d dd S d	| kr\t d
|  d| d	 S 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.)rL   replacelowerre   r   r   r   _get_message_type   s    

rt   c                 C  sX   | st dd| kr4d| kr,t d|  d| d S d| krLt d|  d| d S d S )	Nri   rj   r<   rl   z Message does not have kwargs.datarp   z Message does not have data.)rL   rs   r   r   r   _get_message_fields   s    

rv   zDict[str, Any]c                 C  s   t | }t| }||dS )z&Extract message from a message object.ro   ru   )rt   rv   )re   Zmessage_typeZmessage_datar   r   r   _convert_message   s    rx   zList[Dict[str, Any]])inputsr+   c                 C  sD   d| krdd | d D S d| kr0t | d gS td|  ddS )aG  Extract messages from the given inputs dictionary.

    Args:
        inputs (Mapping[str, Any]): The inputs dictionary.

    Returns:
        List[Dict[str, Any]]: A list of dictionaries representing
            the extracted messages.

    Raises:
        ValueError: If no message(s) are found in the inputs dictionary.
    messagesc                 S  s   g | ]}t |qS r   )rx   )r?   re   r   r   r   rD      s     z,get_messages_from_inputs.<locals>.<listcomp>re   z-Could not find message(s) in run with inputs .N)rx   rL   )ry   r   r   r   get_messages_from_inputs   s
    r|   )outputsr+   c                 C  sr   d| krt d|  d| d }t|dkrFt dt| d| d|d }d|krft d	| d
t|d S )a'  Retrieve the message generation from the given outputs.

    Args:
        outputs (Mapping[str, Any]): The outputs dictionary.

    Returns:
        Dict[str, Any]: The message generation.

    Raises:
        ValueError: If no generations are found or if multiple generations are present.
    generations,No generations found in in run with output: r{   r=   z3Chat examples expect exactly one generation. Found z generations: r   re   z"Unexpected format for generation: z%. Generation does not have a message.)rL   lenrx   r}   r~   Zfirst_generationr   r   r   #get_message_generation_from_outputs   s    
r   c                 C  sX   d| kr| d S d| krD| d }t |dkr4|d S td|  dtd|  dd	S )
zRetrieve the prompt from the given inputs.

    Args:
        inputs (Mapping[str, Any]): The inputs dictionary.

    Returns:
        str: The prompt.

    Raises:
        ValueError: If the prompt is not found or if multiple prompts are present.
    promptpromptsr=   r   z$Multiple prompts in run with inputs z!. Please create example manually.z)Could not find prompt in run with inputs r{   N)r   rL   )ry   r   r   r   r   get_prompt_from_inputs  s    
r   c                 C  s`   d| krt d|  d| d }t|dkr:t d| |d }d|krXt d| |d S )	z(Get the LLM generation from the outputs.r~   r   r{   r=   zMultiple generations in run: r   rY   zNo text in generation: )rL   r   r   r   r   r   get_llm_generation_from_outputs  s    r   z	List[str]c                   C  s   z$t jdddgt jt jd ddgW S  t jtfk
r   z$t jddgt jt jd dgW  Y S  t jtfk
r   tdY nX Y nX dS )z7Get the correct docker compose command for this system.ZdockerZcomposez	--version)stdoutstderrzdocker-composezNeither '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/N)
subprocess
check_callDEVNULLCalledProcessErrorFileNotFoundErrorrL   r   r   r   r   get_docker_compose_command,  s&    
r   zls_schemas.BaseMessageLikedictc                 C  s:   | j d| jid}| jr6t| jdkr6| j|d d< |S )z*Convert a LangChain message to an example.contentrw   r   ru   additional_kwargs)ro   r   r   r   )re   Z	convertedr   r   r   convert_langchain_messageG  s    r   object)objr+   c                 C  s@   t tt| ddttt| ddtt| do:tt| dtgS )zCheck 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   ro   )allr]   getattrrX   r   hasattr)r   r   r   r   is_base_message_likeS  s    	r   d   )Z	LANGSMITHZ	LANGCHAIN)
namespaceszOptional[str]r   )namer0   r   r+   c                  s<    fdd|D }|D ]  t j }|dk	r|  S q|S )a+  Retrieve an environment variable from a list of namespaces.

    Args:
        name (str): The name of the environment variable.
        default (Optional[str], optional): The default value to return if the
            environment variable is not found. Defaults to None.
        namespaces (Tuple, optional): A tuple of namespaces to search for the
            environment variable. Defaults to ("LANGSMITH", "LANGCHAINs").

    Returns:
        Optional[str]: The value of the environment variable if found,
            otherwise the default value.
    c                   s   g | ]}| d   qS )_r   )r?   	namespacer   r   r   rD   y  s     zget_env_var.<locals>.<listcomp>N)osenvironr>   )r   r0   r   namesr`   r   r   r   r2   e  s    
r2   Tc              	   C  s&   t jdtdtd| rdndddS )z,Get the project name for a LangSmith tracer.ZHOSTED_LANGSERVE_PROJECT_NAMEZPROJECTZSESSIONr0   Nr/   )r   r   r>   r2   )Zreturn_default_valuer   r   r   get_tracer_project  s     
r   c                      s:   e Zd ZdZddddd fddZdd	d
dZ  ZS )FilterPoolFullWarningzFFilter urrllib3 warnings logged when the connection pool isn't reused.r.   rX   rS   )r   hostr+   c                   s   t  | || _dS )zInitialize the FilterPoolFullWarning filter.

        Args:
            name (str, optional): The name of the filter. Defaults to "".
            host (str, optional): The host to filter. Defaults to "".
        N)super__init___host)selfr   r   	__class__r   r   r     s    zFilterPoolFullWarning.__init__r4   r5   c                 C  s   |  }d|krdS | j|kS )zJurllib3.connectionpool:Connection pool is full, discarding connection: ...z.Connection pool is full, discarding connectionT)
getMessager   r   recordmsgr   r   r   filter  s    zFilterPoolFullWarning.filter)r.   r.   )r   r   r   r   r   r   __classcell__r   r   r   r   r     s   
r   c                   @  s   e Zd ZdZddddZdS )FilterLangSmithRetryz!Filter for retries from this lib.r4   r5   c                 C  s   |  }d|kS )z!Filter retries from this library.LangSmithRetry)r   r   r   r   r   r     s    zFilterLangSmithRetry.filterN)r   r   r   r   r   r   r   r   r   r     s   r   c                   @  s   e Zd ZdZdS )r   z&Wrapper to filter logs with this name.Nr   r   r   r   r   r     s   r   zlogging.LoggerzSequence[logging.Filter]zGenerator[(None, None, None)])loggerfiltersr+   c                 c  s~   t  |D ]}| | q
W 5 Q R X z
dV  W 5 t > |D ]2}z| | W q: tk
rj   td Y q:X q:W 5 Q R X X dS )zTemporarily adds specified filters to a logger.

    Parameters:
    - logger: The logger to which the filters will be added.
    - filters: A sequence of logging.Filter objects to be temporarily added
        to the logger.
    zFailed to remove filterN)_FILTER_LOCK	addFilterremoveFilterBaseExceptionrf   warning)r   r   r   r   r   r   filter_logs  s    
r   )cacher+   c                 C  s   | dk	r| S t dddS )zGet the testing cache directory.

    Args:
        cache (Optional[str]): The cache path.

    Returns:
        Optional[str]: The cache path if provided, otherwise the value
        from the LANGSMITH_TEST_CACHE environment variable.
    NZ
TEST_CACHEr/   r7   )r   r   r   r   get_cache_dir  s    
r   zUnion[str, pathlib.Path]zOptional[Sequence[str]])pathignore_hostsr+   c              	   #  s   zddl }W n tk
r(   tdY nX ddlm} |  ddd fdd}tj| \}}|j|	d	sz|	d
r~dnd|dddddgddg|d}|
| dV  W 5 Q R X dS )Use a cache for requests.r   NzNvcrpy is required to use caching. Install with:pip install -U "langsmith[vcr]")_patchr   )requestr+   c                   s(   rt  fddD rd S i  _ S )Nc                 3  s   | ]} j |V  qd S N)url
startswith)r?   r   r   r   r   rB     s     z>with_cache.<locals>._filter_request_headers.<locals>.<genexpr>)anyheadersr   r   r   r   _filter_request_headers  s    z+with_cache.<locals>._filter_request_headersz.yamlz.ymlZyamljsonZnew_episodesurimethodr   bodyauthorizationz
Set-Cookie)
serializerZcassette_library_dirZrecord_modeZmatch_onZfilter_headersZbefore_record_request)vcrImportErrorZlangsmith._internalr   patch_urllib3r   r   splitZVCRendswithZuse_cassette)r   r   r   r   r   	cache_dirZ
cache_fileZls_vcrr   r   r   
with_cache  s0    

r   z"Optional[Union[str, pathlib.Path]]c              	   c  s0   | dk	r&t | | dV  W 5 Q R X ndV  dS )r   N)r   )r   r   r   r   r   with_optional_cache  s    r   c                  C  s&   t jt  } dd | D }d|S )Nc                 S  s   g | ]}d |kr|qS )z
langsmith/r   )r?   liner   r   r   rD     s      z_format_exc.<locals>.<listcomp>r.   )	tracebackformat_exceptionsysexc_inforI   )Ztb_linesZfiltered_linesr   r   r   _format_exc  s    r   T   zDict[int, Any])valmemo	max_depth_depthr+   c                   s   t | }t|dd }|d k	r<z
|W S  tk
r:   Y nX  krH| S t| trl fdd|  D S t| tr fdd| D S t| trt fdd| D S t| trЇ fdd	| D S | S )
N__deepcopy__c              	     s2   i | ]*\}}t | d  t | d  qS rE   _middle_copy)r?   kvr   r   r   r   r   
<dictcomp>4  s       z _middle_copy.<locals>.<dictcomp>c                   s   g | ]}t | d  qS rE   r   r?   itemr   r   r   rD   ;  s     z _middle_copy.<locals>.<listcomp>c                 3  s    | ]}t | d  V  qdS )r=   Nr   r   r   r   r   rB   =  s     z_middle_copy.<locals>.<genexpr>c                   s   h | ]}t | d  qS rE   r   r   r   r   r   	<setcomp>?  s     z_middle_copy.<locals>.<setcomp>)	ro   r   r   r]   r   itemslisttupleset)r   r   r   r   clscopierr   r   r   r   &  s(    




r   )r   r+   c              
   C  sX   i }zt | |W S  tk
rR } z"tdt| t| | W Y S d}~X Y nX dS )zDeep copy a value with a compromise for uncopyable objects.

    Args:
        val: The value to be deep copied.

    Returns:
        The deep copied value.
    zFailed to deepcopy input: %sN)copydeepcopyr   rf   debugreprr   )r   r   rZ   r   r   r   deepish_copyD  s    	r   )current_versiontarget_versionr+   c                 C  s(   ddl m} || }||}||kS )zGCheck if the current version is greater or equal to the target version.r   )version)	packagingr   r   )r   r   r   currenttargetr   r   r   is_version_greater_or_equalY  s    

r   zTuple[str, str, str])
identifierr+   c                 C  s   | r&|  ddks&| ds&| dr4td|  | dd}|d }t|dkr\|d nd}d|kr|dd\}}|r|std|  |||fS |std|  d||fS dS )	aE  Parse a string in the format of owner/name:hash, name:hash, owner/name, or name.

    Args:
        identifier (str): The prompt identifier to parse.

    Returns:
        Tuple[str, str, str]: A tuple containing (owner, name, hash).

    Raises:
        ValueError: If the identifier doesn't match the expected formats.
    /r=   zInvalid identifier format: :r   Zlatest-N)rG   r   r   rL   r   r   )r   partsZ
owner_namecommitownerr   r   r   r   parse_prompt_identifierb  s(    
r  Pc                      sN   e Zd ZdZddddd fddZd	d
ddddddd fddZ  ZS )ContextThreadPoolExecutorz?ThreadPoolExecutor that copies the context to the child thread.zCallable[P, T]zP.argszP.kwargsz	Future[T])r:   r;   r<   r+   c                   s0   t  ttdtf tjt j	|f||S )aC  Submit a function to the executor.

        Args:
            func (Callable[..., T]): The function to submit.
            *args (Any): The positional arguments to the function.
            **kwargs (Any): The keyword arguments to the function.

        Returns:
            Future[T]: The future for the function.
        .)
r   submitr   r   r   rO   partialcontextvarscopy_contextrun)r   r:   r;   r<   r   r   r   r
    s    
 z ContextThreadPoolExecutor.submitNr=   timeout	chunksizezCallable[..., T]zIterable[Any]zOptional[float]rc   zIterator[T])fn	iterablesr  r  r+   c                  sJ   dd t t|d D  ddd fdd}t j|f|||d	S )
a  Return an iterator equivalent to stdlib map.

        Each function will receive its own copy of the context from the parent thread.

        Args:
            fn: A callable that will take as many arguments as there are
                passed iterables.
            timeout: The maximum number of seconds to wait. If None, then there
                is no limit on the wait time.
            chunksize: The size of the chunks the iterable will be broken into
                before being passed to a child process. This argument is only
                used by ProcessPoolExecutor; it is ignored by
                ThreadPoolExecutor.

        Returns:
            An iterator equivalent to: map(func, *iterables) but the calls may
            be evaluated out-of-order.

        Raises:
            TimeoutError: If the entire result iterator could not be generated
                before the given timeout.
            Exception: If fn(*args) raises for any values.
        c                 S  s   g | ]}t  qS r   )r  r  )r?   r   r   r   r   rD     s     z1ContextThreadPoolExecutor.map.<locals>.<listcomp>r   r   r   )r;   r+   c                    s      jf|  S r   )popr  )r;   Zcontextsr  r   r   _wrapped_fn  s    z2ContextThreadPoolExecutor.map.<locals>._wrapped_fnr  )ranger   r   map)r   r  r  r  r  r  r   r  r   r    s    zContextThreadPoolExecutor.map)r   r   r   r   r
  r  r   r   r   r   r   r	    s
   r	  )api_urlr+   c                 C  s@   | pt ttddd}| s&td| dddS )zBGet the LangSmith API URL from the environment or the given value.ZENDPOINTzhttps://api.smith.langchain.comr/   z!LangSmith API URL cannot be empty"'r  )r   rX   r2   stripr"   rstrip)r  Z_api_urlr   r   r   get_api_url  s    r  )api_keyr+   c                 C  s@   | dk	r| n
t ddd}|dks(| s,dS | ddS )z8Get the API key from the environment or the given value.NZAPI_KEYr/   r  r  )r2   r  )r  Zapi_key_r   r   r   get_api_key  s    r   )r   r+   c                 C  s\   z>t | jdd }t|}|dkp<|dp<|dW S  tjk
rV   Y dS X dS )zCheck if the URL is localhost.

    Parameters
    ----------
    url : str
        The URL to check.

    Returns:
    -------
    bool
        True if the URL is localhost, False otherwise.
    r  r   z	127.0.0.1z0.0.0.0z::FN)urllib_parseurlsplitnetlocr   socketgethostbynamer   gaierror)r   r#  ipr   r   r   _is_localhost  s    
r(     )web_urlr  c                 C  s   | r| S t |}t|r d}njt|jdrZt|jddd }t |j|d}n0t|j	
drpd}nt|j	
drd	}nd
}|S )z1Get the host URL based on the web URL or API URL.zhttp://localhostz/apir=   r   )r   zeu.zhttps://eu.smith.langchain.comzdev.zhttps://dev.smith.langchain.comzhttps://smith.langchain.com)r!  urlparser(  rX   r   r   rsplit
urlunparse_replacer#  r   )r*  r  
parsed_urllinknew_pathr   r   r   get_host_url  s    
r2  )r  depthr+   c                 C  s   |dkst | st| S t| dr(| jS t| tjrDt| j|d S t| dr|t| drlt| j	drl| j	jS t| j
|d S t| S )Nr)  r   r=   __call__r   )callablerX   r   r   r]   rO   r  _get_function_namer:   r   r4  )r  r3  r   r   r   r6    s    

r6  )N)N)T)N)N)r   r   )r   )jr   
__future__r   
contextlibr  r   r^   rO   loggingr   pathlibr$  r   r   	threadingr   concurrent.futuresr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   urllibr   r!  ZhttpxrV   Ztyping_extensionsr   Zurllib3.utilr   Z	langsmithr   Z
ls_schemas	getLoggerr   rf   	Exceptionr   r    r!   r"   r#   r$   r%   r&   r'   UserWarningr(   r)   r3   r8   rR   r[   ra   	lru_cacherh   rt   rv   rx   r|   r   r   r   r   r   r   r2   r   Filterr   r   r   RLockr   contextmanagerr   r   r   r   r   r   r   r   r   r  r  r	  r  r   r(  r2  r6  r   r   r   r   <module>   s   D




 

 *    	#G
