
    Ng9                       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rdd	lmZ dd
lmZ  G d d          Z e            Z G d d          Z G 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 `@test` decorator
It allows you to log scores about a test case and optionally make assertions that log as
"expectation" feedback to LangSmith.

Example usage:

    from langsmith import expect, test

    @test
    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                  "    e Zd ZdZddZ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                    dS )NF selfs    M/var/www/html/ai-engine/env/lib/python3.11/site-packages/langsmith/_expect.py__bool__z_NULL_SENTRY.__bool__L   s    u    strc                    dS )N	NOT_GIVENr   r   s    r   __repr__z_NULL_SENTRY.__repr__O   s    {r   N)r   r   )r   r   )__name__
__module____qualname____doc__r   r    r   r   r   r   r   G   sF                 r   r   c                  t    e Zd ZdZ	 	 d'd(dZd)d*dZd+dZd,dZd,dZd-dZ	d.d/d Z
d,d!Zd0d"Zd1d#Zd2d&ZdS )3_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                    || _         || _        || _        |pt          j        d          | _        t          j                    }|r|j        n|| _	        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,   rts          r   __init__z_Matcher.__init__Y   sX     
"Wh&HUV&W&W&W$&&&(4r{{fr   scoreintmessager   Nonec                    t          j                    sO| j        st          j                    | _        | j                            | j        j        | j        d||           d S d S )Nexpectation)r,   r(   r;   comment)	r3   test_tracking_is_disabledr2   r9   get_cached_clientr*   submitcreate_feedbackr8   )r   r;   r=   s      r   _submit_feedbackz_Matcher._submit_feedbackh   sv    133 		< 6!355N!!,|! "     		 		r   	conditionboolmethod_namec                    	 |s
J |            |                      dd| j         d|            d S # t          $ r+}|                      dt          |                     |d d }~ww xY w)N   z	Success: .)r=   r   )rF   r(   AssertionErrorrepr)r   rG   r=   rI   es        r   _assertz_Matcher._assertt   s    	%%g%%%!!!-Q-Q-QK-Q-Q!RRRRR 	 	 	!!!T!WW---	s   .2 
A'&A""A'floatc           	     l    |                      | 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rP   r)   r(   r   r)   s     r   rU   z_Matcher.to_be_less_than|   sP     	JPPP5PPDJPP	
 	
 	
 	
 	
r   c           	     l    |                      | 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.
        rS   z to be greater than rT   to_be_greater_thanNrV   rW   s     r   rY   z_Matcher.to_be_greater_than   sP     	JSSSeSStzSS 	
 	
 	
 	
 	
r   	min_value	max_valuec                    |                      || j        cxk     o|k     nc 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.
        rS   z to be between z and rT   to_be_betweenNrV   )r   rZ   r[   s      r   r]   z_Matcher.to_be_between   s     	
....Y....% % % % % % %
% %		
 	
 	
 	
 	
r      	precisionc           	         |                      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.
        rS   z to be approximately rT   to_be_approximatelyN)rP   roundr)   r(   )r   r)   r_   s      r   ra   z_Matcher.to_be_approximately   sd     	$*i((E%,C,CCTTTuTT
TT!	
 	
 	
 	
 	
r   c           	     l    |                      | 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.
        rS   z to be equal to rT   to_equalNrV   rW   s     r   rd   z_Matcher.to_equal   sP     	J%OOO%OO4:OO	
 	
 	
 	
 	
r   c                b    |                      | 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.
        NrS   z to be None, but got 
to_be_nonerV   r   s    r   rf   z_Matcher.to_be_none   sH     	J$CCCtzCC	
 	
 	
 	
 	
r   c                Z    |                      || 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.
        rS   z to contain z, but it does not
to_containNrV   rW   s     r   rh   z_Matcher.to_contain   sI     	TZFFFeFFF	
 	
 	
 	
 	
r   funcr   c                   t          j        |          }|                      || 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	signaturerP   r)   r(   )r   ri   func_signatures      r   rk   z_Matcher.against   s[     !*400D???TX??	
 	
 	
 	
 	
r   )NN)
r   r'   r(   r   r)   r   r*   r+   r,   r-   N)r;   r<   r=   r-   r   r>   )rG   rH   r=   r   rI   r   r   r>   )r)   rQ   r   r>   )rZ   rQ   r[   rQ   r   r>   )r^   )r)   rQ   r_   r<   r   r>   )r   r>   )r)   r   r   r>   )ri   r   r   r>   )r!   r"   r#   r$   r:   rF   rP   rU   rY   r]   ra   rd   rf   rh   rk   r   r   r   r&   r&   V   s       >> CG $5 5 5 5 5
 
 
 
 
   
 
 
 

 
 
 
 
 
 
 
$
 
 
 
 
"
 
 
 
 

 

 

 


 
 
 
 
 
 
 
 
 
r   r&   c                      e Zd ZdZddd'dZddd(dZddd)dZd*dZddddd+dZe	d*d            Z
e	d,d             Z
edfd-d#Z
d.d&ZdS )/_Expectz1A class for setting expectations on test results.Nr   r   r'   c                   || _         t          j        d          | _        t	          j        | j        j        d           d S )Nr/   r0   T)wait)r2   r3   r4   executoratexitregistershutdownr   r   s     r   r:   z_Expect.__init__   s>     :qIII.T::::::r   config
predictionr   	referencerz   Optional[EmbeddingConfig]r   r&   c          	     &   ddl m} |pi }|                    d          rdnd} ||          }|                    ||          }||j        d}|                     d	||d
| d|j         d           t          | j        d	|| j                  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encodercustomopenairy   r{   r|   )r   metricembedding_distanceUsing z
, Metric: r;   source_inforA   r*   )	'langsmith._internal._embedding_distancer   getevaluatedistancerF   r&   r2   rt   )	r   r{   r|   rz   r   encoder_func	evaluatorr;   src_infos	            r   r   z_Expect.embedding_distance  s    F 	NMMMMM2#)::i#8#8Fxxh%%V444	""jI"NN+y7IJJ 'PLPPI<NPP 	
 	
 	
 L.
 
 
 	
r   Optional[EditDistanceConfig]c          	     :   ddl m} |pi }|                    d          pd}|                    dd          } ||          }|                    ||          }||d	}	|                     d
||	d| d| d           t          | j        d
|| j                  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   )EditDistancer   damerau_levenshteinnormalize_scoreTry   r   )r   	normalizeedit_distancer   z, Normalize: r   r   )"langsmith._internal._edit_distancer   r   r   rF   r&   r2   rt   )
r   r{   r|   rz   r   r   r   r   r;   r   s
             r   r   z_Expect.edit_distance9  s    < 	DCCCCC2H%%>)>JJ0$77	 L///	""jI"NN$9=='DFDDDD 	
 	
 	
 Lm	
 
 
 	
r   r)   r   c                <    t          | j        d|| j                  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)   r   )r&   r2   rt   rW   s     r   r)   z_Expect.valuen  s     guNNNNr   r;   )r(   source_run_idrA   Union[float, int]r(   r   Optional[ls_client.ID_TYPE]rA   r-   c               v    |                      ||ddi||d           t          | j        ||| j                  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)r;   r   r   rA   r   )rF   r&   r2   rt   )r   r;   r(   r   rA   s        r   r;   z_Expect.score|  sW    ( 	 (.9!."	 	
 	
 	
 c5DMJJJJr   c                   d S ro   r   rW   s     r   __call__z_Expect.__call__  s    363r   ls_client.Clientc                  d S ro   r   rx   s     r   r   z_Expect.__call__  s    CF3r   Optional[Any]Union[_Expect, _Matcher]c               b    t          |          }|t          ur|                    |          S |S )Nr   )rq   r   r)   )r   r)   r   expecteds       r   r   z_Expect.__call__  s6     &)))	!!>>%(((r   resultsdictc                    t          j                    }|r|j        nd }t          j                    sA| j        st          j                    | _         | j        j	        | j        j
        f||d| d S d S )N)r,   r(   )r5   r6   r7   r3   rB   r2   r9   rC   rt   rD   rE   )r   r(   r   current_runr,   s        r   rF   z_Expect._submit_feedback  s    -//)4>%%$133 	< 6!355 DM ,5; HO    	 	r   )r   r'   )r{   r   r|   r   rz   r}   r   r&   )r{   r   r|   r   rz   r   r   r&   )r)   r   r   r&   )
r;   r   r(   r   r   r   rA   r-   r   r&   )r   r   r   rq   )r   r'   r)   r   r   r   )r(   r   r   r   )r!   r"   r#   r$   r:   r   r   r)   r;   r
   r   r   rF   r   r   r   rq   rq      s.       ;;?C ; ; ; ; ; ; -14
 4
 4
 4
 4
 4
v 043
 3
 3
 3
 3
 3
jO O O O$ 59!%K K K K K KB 666 X6FFF XF  )-1		 	 	 	 	     r   rq   expect) r$   
__future__r   ru   rl   typingr   r   r   r   r   r	   r
   	langsmithr   	ls_clientr   r5   r   r9   r   r3   r   r   r   r   r   r   r&   rq   r   __all__r   r   r   <module>r      s  + +Z # " " " " "                    * ) ) ) ) ) ' ' ' ' ' ' % % % % % % ' ' ' ' ' ' HEEEEEEGGGGGG	 	 	 	 	 	 	 	 LNN	b
 b
 b
 b
 b
 b
 b
 b
J{ { { { { { { {| 
*r   