o
    Zh{:                     @  s   d Z ddlm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 ddlmZ ddlmZ ddlmZ ddlmZ erHdd	lmZ dd
lmZ G dd dZe ZG dd dZG dd dZe ZdgZdS )a  Make approximate assertions as "expectations" on test results.

This module is designed to be used within test cases decorated with the
`@pytest.mark.decorator` decorator
It allows you to log scores about a test case and optionally make assertions that log as
"expectation" feedback to LangSmith.

Example usage:
    .. code-block:: python

        import pytest
        from langsmith import expect


        @pytest.mark.langsmith
        def test_output_semantically_close():
            response = oai_client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Say hello!"},
                ],
            )
            response_txt = response.choices[0].message.content
            # Intended usage
            expect.embedding_distance(
                prediction=response_txt,
                reference="Hello!",
            ).to_be_less_than(0.9)

            # Score the test case
            matcher = expect.edit_distance(
                prediction=response_txt,
                reference="Hello!",
            )
            # Apply an assertion and log 'expectation' feedback to LangSmith
            matcher.to_be_less_than(1)

            # You can also directly make assertions on values directly
            expect.value(response_txt).to_contain("Hello!")
            # Or using a custom check
            expect.value(response_txt).against(lambda x: "Hello" in x)

            # You can even use this for basic metric logging within tests

            expect.score(0.8)
            expect.score(0.7, key="similarity").to_be_greater_than(0.7)
    )annotationsN)TYPE_CHECKINGAnyCallableLiteralOptionalUnionoverloadclient)run_helpers)	run_trees)utils)EditDistanceConfig)EmbeddingConfigc                   @  s$   e Zd ZdZd
ddZdddZd	S )_NULL_SENTRYzA sentinel singleton class used to distinguish omitted keyword arguments
    from those passed in with the value None (which may have different behavior).
    returnLiteral[False]c                 C     dS )NF selfr   r   H/var/www/html/lang_env/lib/python3.10/site-packages/langsmith/_expect.py__bool__P      z_NULL_SENTRY.__bool__strc                 C  r   )N	NOT_GIVENr   r   r   r   r   __repr__S   r   z_NULL_SENTRY.__repr__N)r   r   )r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   K   s    
r   c                   @  s   e Zd ZdZ		d2d3ddZd4d5ddZd6ddZd7ddZd7ddZd8d"d#Z	d9d:d&d'Z
d7d(d)Zd;d*d+Zd<d,d-Zd=d0d1ZdS )>_Matcherz4A class for making assertions on expectation values.Nr   Optional[ls_client.Client]keyr   valuer   	_executor,Optional[ls_utils.ContextThreadPoolExecutor]run_idOptional[str]c                 C  sF   || _ || _|| _|ptjdd| _t }|r|j| _	d S || _	d S )N   max_workers)
_clientr$   r%   ls_utilsContextThreadPoolExecutorr&   rhget_current_run_treetrace_id_run_id)r   r   r$   r%   r&   r(   rtr   r   r   __init__]   s   z_Matcher.__init__scoreintmessager   Nonec                 C  s<   t  s| jst | _| jj| jj| jd||d d S d S )NZexpectation)r(   r$   r6   comment)	r.   test_tracking_is_disabledr-   r4   get_cached_clientr&   submitcreate_feedbackr3   )r   r6   r8   r   r   r   _submit_feedbackl   s   

z_Matcher._submit_feedback	conditionboolmethod_namec              
   C  s^   z|sJ || j dd| j d| d W d S  ty. } z|  dt| |d d }~ww )N   z	Success: .)r8   r   )r?   r$   AssertionErrorrepr)r   r@   r8   rB   er   r   r   _assertx   s   "z_Matcher._assertfloatc              	   C  s.   |  | j|k d| j d| d| j d dS )zAssert that the expectation value is less than the given value.

        Args:
            value: The value to compare against.

        Raises:
            AssertionError: If the expectation value is not less than the given value.
        	Expected z to be less than 
, but got to_be_less_thanNrH   r%   r$   r   r%   r   r   r   rL      s
   	z_Matcher.to_be_less_thanc              	   C  s.   |  | j|kd| j d| d| j d dS )a  Assert that the expectation value is greater than the given value.

        Args:
            value: The value to compare against.

        Raises:
            AssertionError: If the expectation value is not
            greater than the given value.
        rJ   z to be greater than rK   to_be_greater_thanNrM   rN   r   r   r   rO      
   
z_Matcher.to_be_greater_than	min_value	max_valuec                 C  sD   |  || j  k o|k n  d| j d| d| d| j d dS )aJ  Assert that the expectation value is between the given min and max values.

        Args:
            min_value: The minimum value (exclusive).
            max_value: The maximum value (exclusive).

        Raises:
            AssertionError: If the expectation value
                is not between the given min and max.
        rJ   z to be between z and rK   to_be_betweenNrM   )r   rQ   rR   r   r   r   rS      s   z_Matcher.to_be_between   	precisionc              	   C  s:   |  t| j|t||kd| j d| d| j d dS )ak  Assert that the expectation value is approximately equal to the given value.

        Args:
            value: The value to compare against.
            precision: The number of decimal places to round to for comparison.

        Raises:
            AssertionError: If the rounded expectation value
                does not equal the rounded given value.
        rJ   z to be approximately rK   to_be_approximatelyN)rH   roundr%   r$   )r   r%   rU   r   r   r   rV      s
   z_Matcher.to_be_approximatelyc              	   C  s.   |  | j|kd| j d| d| j d dS )a   Assert that the expectation value equals the given value.

        Args:
            value: The value to compare against.

        Raises:
            AssertionError: If the expectation value does
                not exactly equal the given value.
        rJ   z to be equal to rK   to_equalNrM   rN   r   r   r   rX      rP   z_Matcher.to_equalc                 C  s(   |  | jdu d| j d| j d dS )zAssert that the expectation value is None.

        Raises:
            AssertionError: If the expectation value is not None.
        NrJ   z to be None, but got 
to_be_nonerM   r   r   r   r   rY      s
   z_Matcher.to_be_nonec                 C  s(   |  || jv d| j d| dd dS )zAssert that the expectation value contains the given value.

        Args:
            value: The value to check for containment.

        Raises:
            AssertionError: If the expectation value does not contain the given value.
        rJ   z to contain z, but it does not
to_containNrM   rN   r   r   r   rZ      s
   	z_Matcher.to_containfuncr   c                C  s0   t |}| || jd| d| j d dS )zAssert the expectation value against a custom function.

        Args:
            func: A custom function that takes the expectation value as input.

        Raises:
            AssertionError: If the custom function returns False.
        z
Assertion z failed for againstN)inspect	signaturerH   r%   r$   )r   r[   Zfunc_signaturer   r   r   r\      s   
	z_Matcher.against)NN)
r   r#   r$   r   r%   r   r&   r'   r(   r)   N)r6   r7   r8   r)   r   r9   )r@   rA   r8   r   rB   r   r   r9   )r%   rI   r   r9   )rQ   rI   rR   rI   r   r9   )rT   )r%   rI   rU   r7   r   r9   )r   r9   )r%   r   r   r9   )r[   r   r   r9   )r   r   r    r!   r5   r?   rH   rL   rO   rS   rV   rX   rY   rZ   r\   r   r   r   r   r"   Z   s    






r"   c                   @  s   e Zd ZdZddd.ddZddd/ddZddd0ddZd1ddZddddd2d!d"Ze	d1d#d$Z
e	d3d&d$Z
edfd4d)d$Z
d5d,d-ZdS )6_Expectz1A class for setting expectations on test results.Nr
   r   r#   c                C  s*   || _ tjdd| _tj| jjdd d S )Nr*   r+   T)wait)r-   r.   r/   executoratexitregistershutdownr   r   r   r   r   r5     s   z_Expect.__init__config
predictionr   	referencerh   Optional[EmbeddingConfig]r   r"   c          	   	   C  s   ddl m} |p	i }|drdnd}||d}|j||d}||jd}| d	||d
| d|j d t| jd	|| jdS )a  Compute the embedding distance between the prediction and reference.

        This logs the embedding distance to LangSmith and returns a `_Matcher` instance
        for making assertions on the distance value.

        By default, this uses the OpenAI API for computing embeddings.

        Args:
            prediction: The predicted string to compare.
            reference: The reference string to compare against.
            config: Optional configuration for the embedding distance evaluator.
                Supported options:
                - `encoder`: A custom encoder function to encode the list of input
                     strings to embeddings. Defaults to the OpenAI API.
                - `metric`: The distance metric to use for comparison.
                    Supported values: "cosine", "euclidean", "manhattan",
                    "chebyshev", "hamming".

        Returns:
            A `_Matcher` instance for the embedding distance value.


        Examples:
            >>> expect.embedding_distance(
            ...     prediction="hello",
            ...     reference="hi",
            ... ).to_be_less_than(1.0)
        r   )EmbeddingDistanceencoderZcustomZopenairg   ri   rj   )rm   metricembedding_distanceUsing z
, Metric: r6   source_infor:   r&   )	'langsmith._internal._embedding_distancerl   getevaluateZdistancer?   r"   r-   rb   )	r   ri   rj   rh   rl   Zencoder_func	evaluatorr6   src_infor   r   r   rp     s    #
z_Expect.embedding_distanceOptional[EditDistanceConfig]c          
   	   C  s   ddl m} |p	i }|dpd}|dd}||d}|j||d}||d	}	| d
||	d| d| d t| jd
|| jdS )a  Compute the string distance between the prediction and reference.

        This logs the string distance (Damerau-Levenshtein) to LangSmith and returns
        a `_Matcher` instance for making assertions on the distance value.

        This depends on the `rapidfuzz` package for string distance computation.

        Args:
            prediction: The predicted string to compare.
            reference: The reference string to compare against.
            config: Optional configuration for the string distance evaluator.
                Supported options:
                - `metric`: The distance metric to use for comparison.
                    Supported values: "damerau_levenshtein", "levenshtein",
                    "jaro", "jaro_winkler", "hamming", "indel".
                - `normalize_score`: Whether to normalize the score between 0 and 1.

        Returns:
            A `_Matcher` instance for the string distance value.

        Examples:
            >>> expect.edit_distance("hello", "helo").to_be_less_than(1)
        r   )EditDistancero   Zdamerau_levenshteinZnormalize_scoreTrg   rn   )ro   	normalizeedit_distancerq   z, Normalize: rr   rt   )"langsmith._internal._edit_distancer{   rv   rw   r?   r"   r-   rb   )
r   ri   rj   rh   r{   ro   r|   rx   r6   ry   r   r   r   r}   =  s(   

z_Expect.edit_distancer%   r   c                 C  s   t | jd|| jdS )a"  Create a `_Matcher` instance for making assertions on the given value.

        Args:
            value: The value to make assertions on.

        Returns:
            A `_Matcher` instance for the given value.

        Examples:
           >>> expect.value(10).to_be_less_than(20)
        r%   rt   )r"   r-   rb   rN   r   r   r   r%   r  s   z_Expect.valuer6   )r$   source_run_idr:   Union[float, int, bool]r$   r   Optional[ls_client.ID_TYPE]r:   r)   c                C  s.   |  ||ddi||d t| j||| jdS )a  Log a numeric score to LangSmith.

        Args:
            score: The score value to log.
            key: The key to use for logging the score. Defaults to "score".

        Examples:
            >>> expect.score(0.8)  # doctest: +ELLIPSIS
            <langsmith._expect._Matcher object at ...>

            >>> expect.score(0.8, key="similarity").to_be_greater_than(0.7)
        methodzexpect.score)r6   rs   r   r:   rt   )r?   r"   r-   rb   )r   r6   r$   r   r:   r   r   r   r6     s   	z_Expect.scorec                C     d S r_   r   rN   r   r   r   __call__  r   z_Expect.__call__ls_client.Clientc               C  r   r_   r   rf   r   r   r   r     r   Optional[Any]Union[_Expect, _Matcher]c                C  s    t |d}|tur||S |S )Nr
   )r`   r   r%   )r   r%   r   expectedr   r   r   r     s   

resultsdictc                 C  sT   t  }|r	|jnd }t s(| jst | _| jj	| jj
f||d| d S d S )N)r(   r$   )r0   r1   r2   r.   r;   r-   r4   r<   rb   r=   r>   )r   r$   r   Zcurrent_runr(   r   r   r   r?     s   

z_Expect._submit_feedback)r   r#   )ri   r   rj   r   rh   rk   r   r"   )ri   r   rj   r   rh   rz   r   r"   )r%   r   r   r"   )
r6   r   r$   r   r   r   r:   r)   r   r"   )r   r   r   r`   )r   r#   r%   r   r   r   )r$   r   r   r   )r   r   r    r!   r5   rp   r}   r%   r6   r	   r   r   r?   r   r   r   r   r`      s(    
;
5!r`   expect) r!   
__future__r   rc   r]   typingr   r   r   r   r   r   r	   Z	langsmithr   Z	ls_clientr   r0   r   r4   r   r.   r~   r   ru   r   r   r   r"   r`   r   __all__r   r   r   r   <module>   s(    1$
 & ?
