ó
    üÞ j,;  ã                  óü   • S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
JrJr  SSKJr  SSKJr  SSKJr  SSKJr  \(       a  SS	KJr  SS
KJr   " S S5      r\" 5       r " S S5      r " S S5      r\" 5       rS/rg)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:
    ```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                  ó,   • \ rS rSrSrSS jrSS jrSrg)	Ú_NULL_SENTRYéL   z—A sentinel singleton class used to distinguish omitted keyword arguments
from those passed in with the value None (which may have different behavior).
c                ó   • g)NF© ©Úselfs    ÚK/var/www/html/gaurav/venv/lib/python3.13/site-packages/langsmith/_expect.pyÚ__bool__Ú_NULL_SENTRY.__bool__Q   s   € Øó    c                ó   • g)NÚ	NOT_GIVENr   r   s    r   Ú__repr__Ú_NULL_SENTRY.__repr__T   s   € Ør   r   N)ÚreturnzLiteral[False])r!   Ústr)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   Ú__static_attributes__r   r   r   r   r   L   s   † ñô÷r   r   c                  ó¨   • \ rS rSrSr  S         SS jjrSSS jjrSS jrSS jrSS jr	SS	 jr
SSS
 jjrSS jrSS jrSS jrSS jrSrg)Ú_Matcheré[   z4A class for making assertions on expectation values.Nc                ó  • Xl         X l        X0l        U=(       d    [        R                  " SS9U l        [        R                  " 5       U l        U R                  (       a  U R                  R                  U l
        g UU l
        g )Né   ©Úmax_workers)Ú_clientÚkeyÚvalueÚls_utilsÚContextThreadPoolExecutorÚ	_executorÚrhÚget_current_run_treeÚ_rtÚtrace_idÚ_run_id)r   r   r1   r2   r5   Úrun_ids         r   Ú__init__Ú_Matcher.__init__^   sX   € ð ŒØŒØŒ
Ø"×W¤h×&HÒ&HÐUVÑ&WˆŒÜ×*Ò*Ó,ˆŒØ,0¯H¯H�t—x‘x×(Ñ(ˆ�¸&ˆ�r   c           
     ó¦  • [         R                  " 5       (       d·  U R                  (       d  [        R                  " 5       U l        U R
                  R                  U R                  R                  U R                  SUUU R                  (       a  U R                  R                  OS U R                  (       a  U R                  R                  OS S9  g g )NÚexpectation)r;   r1   ÚscoreÚcommentÚ
session_idÚ
start_time)r3   Útest_tracking_is_disabledr0   ÚrtÚget_cached_clientr5   ÚsubmitÚcreate_feedbackr:   r8   rB   rC   )r   r@   Úmessages      r   Ú_submit_feedbackÚ_Matcher._submit_feedbackm   s‹   € Ü×1Ò1×3Ñ3Ø—<—<Ü!×3Ò3Ó5�”Ø�N‰N×!Ñ!Ø—‘×,Ñ,Ø—|‘|Ø!ØØØ26·(·(˜4Ÿ8™8×.Ò.ÀØ26·(·(˜4Ÿ8™8×.Ò.Àð "ò ð 4r   c                óÂ   •  U(       d   U5       eU R                  SSU R                   SU 3S9  g ! [         a#  nU R                  S[        U5      5        US eS nAff = f)Né   z	Success: Ú.)rI   r   )rJ   r1   ÚAssertionErrorÚrepr)r   Ú	conditionrI   Úmethod_nameÚes        r   Ú_assertÚ_Matcher._assert{   sd   € ð	ÞÐ%˜gÓ%�9Ø×!Ñ! !¨y¸¿¹¸
À!ÀKÀ=Ð-QÐ!ÒRøÜó 	Ø×!Ñ! !¤T¨!£WÔ-Ø˜Ðûð	ús   ‚.1 ±
A»AÁAc           	     ó~   • U R                  U R                  U:  SU R                   SU SU R                   3S5        g)zÈAssert 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©rT   r2   r1   ©r   r2   s     r   rY   Ú_Matcher.to_be_less_thanƒ   s>   € ð 	�‰Ø�J‰J˜ÑØ˜Ÿ™�zÐ!2°5°'¸ÀDÇJÁJÀ<ÐPØõ	
r   c           	     ó~   • U R                  U R                  U:„  SU R                   SU SU R                   3S5        g)zÒ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.
rW   z to be greater than rX   Úto_be_greater_thanNrZ   r[   s     r   r^   Ú_Matcher.to_be_greater_than’   s>   € ð 	�‰Ø�J‰J˜ÑØ˜Ÿ™�zÐ!5°e°W¸JÀtÇzÁzÀlÐSØ õ	
r   c                ó¢   • U R                  XR                  s=:  =(       a    U:  Os  SU R                   SU SU SU R                   3S5        g)a  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 min and max.
rW   z to be between z and rX   Úto_be_betweenNrZ   )r   Ú	min_valueÚ	max_values      r   ra   Ú_Matcher.to_be_between¢   sP   € ð 	�‰ØŸ
™
×.Ó. YÔ.Ø˜Ÿ™�z °°¸5ÀÀð LØŸ
™
�|ð%àõ		
r   c           	     ó¤   • U R                  [        U R                  U5      [        X5      :H  SU R                   SU SU R                   3S5        g)a3  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.
rW   z to be approximately rX   Úto_be_approximatelyN)rT   Úroundr2   r1   )r   r2   Ú	precisions      r   rf   Ú_Matcher.to_be_approximately³   sJ   € ð 	�‰Ü�$—*‘*˜iÓ(¬E°%Ó,CÑCØ˜Ÿ™�zÐ!6°u°g¸ZÈÏ
É
À|ÐTØ!õ	
r   c           	     ó~   • U R                  U R                  U:H  SU R                   SU SU R                   3S5        g)zÐ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.
rW   z to be equal to rX   Úto_equalNrZ   r[   s     r   rk   Ú_Matcher.to_equalÄ   s>   € ð 	�‰Ø�J‰J˜%ÑØ˜Ÿ™�zÐ!1°%°¸
À4Ç:Á:À,ÐOØõ	
r   c                óv   • U R                  U R                  SL SU R                   SU R                   3S5        g)zrAssert that the expectation value is `None`.

Raises:
    AssertionError: If the expectation value is not `None`.
NrW   z to be None, but got Ú
to_be_nonerZ   r   s    r   rn   Ú_Matcher.to_be_noneÔ   s8   € ð 	�‰Ø�J‰J˜$ÐØ˜Ÿ™�zÐ!6°t·z±z°lÐCØõ	
r   c                ód   • U R                  XR                  ;   SU R                   SU S3S5        g)zÊAssert 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.
rW   z to contain z, but it does notÚ
to_containNrZ   r[   s     r   rq   Ú_Matcher.to_containà   s4   € ð 	�‰Ø—Z‘ZÑØ˜Ÿ™�z ¨e¨WÐ4EÐFØõ	
r   c               ó–   • [         R                  " U5      nU R                  U" U R                  5      SU SU R                   3S5        g)zÉAssert 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Ú	signaturerT   r2   r1   )r   ÚfuncÚfunc_signatures      r   rt   Ú_Matcher.againstð   sD   € ô !×*Ò*¨4Ó0ˆØ�‰Ù�—‘ÓØ˜Ð(¨°T·X±X°JÐ?Øõ	
r   )r0   r5   r8   r:   r1   r2   )NN)
r   úOptional[ls_client.Client]r1   r"   r2   r   r5   z,Optional[ls_utils.ContextThreadPoolExecutor]r;   úOptional[str]©N)r@   ÚintrI   r{   r!   ÚNone)rQ   ÚboolrI   r"   rR   r"   r!   r~   )r2   Úfloatr!   r~   )rb   r€   rc   r€   r!   r~   )é   )r2   r€   rh   r}   r!   r~   )r!   r~   )r2   r   r!   r~   )rw   r   r!   r~   )r#   r$   r%   r&   r'   r<   rJ   rT   rY   r^   ra   rf   rk   rn   rq   rt   r(   r   r   r   r*   r*   [   s}   † Ù>ð CGØ $ðAà*ðAð ðAð ð	Að
 @ðAð õAöôô
ô
ô 
ö"
ô"
ô 

ô
÷ 
r   r*   c                  óê   • \ rS rSrSrSS.SS jjrSS.       SS jjrSS.       SS jjrSS	 jrS
SSS.         SS jjr	\
SS j5       r\
SS j5       r\S4     SS jjrSS jrSrg)Ú_Expecti  z1A class for setting expectations on test results.Nr   c               ó”   • Xl         [        R                  " SS9U l        [        R
                  " U R                  R                  SS9  g )Nr-   r.   T)Úwait)r0   r3   r4   ÚexecutorÚatexitÚregisterÚshutdown©r   r   s     r   r<   Ú_Expect.__init__  s1   € ØŒÜ ×:Ò:ÀqÑIˆŒÜ�Š˜Ÿ™×.Ñ.°TÓ:r   ©Úconfigc          	     ó(  • SSK Jn  U=(       d    0 nUR                  S5      (       a  SOSnU" US9nUR                  XS9nXVR                  S.nU R                  S	UUS
U SUR                   3S.5        [        U R                  S	XpR                  S9$ )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.


Example:
    ```python
    expect.embedding_distance(
        prediction="hello",
        reference="hi",
    ).to_be_less_than(1.0)
    ```
r   )ÚEmbeddingDistanceÚencoderÚcustomÚopenairŒ   ©Ú
predictionÚ	reference)r�   ÚmetricÚembedding_distanceúUsing z
, Metric: ©r@   Úsource_inforA   ©r5   )	Ú'langsmith._internal._embedding_distancer�   ÚgetÚevaluateÚdistancerJ   r*   r0   r†   )	r   r”   r•   r�   r�   Úencoder_funcÚ	evaluatorr@   Úsrc_infos	            r   r—   Ú_Expect.embedding_distance	  s¤   € õT 	Nà—˜2ˆØ#)§:¡:¨i×#8Ñ#8‘x¸hˆÙ%¨VÑ4ˆ	Ø×"Ñ"¨jÐ"ÐNˆØ+×7IÑ7IÑJˆØ×ÑØ àØ'Ø# L >°¸I×<NÑ<NÐ;OÐPñô	
ô Ø�L‰LÐ.°ÇÁñ
ð 	
r   c          	     ó&  • SSK Jn  U=(       d    0 nUR                  S5      =(       d    SnUR                  SS5      nU" US9nUR                  XS9nXVS	.n	U R	                  S
UU	SU SU 3S.5        [        U R                  S
UU R                  S9$ )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:
    ```python
    expect.edit_distance("hello", "helo").to_be_less_than(1)
    ```
r   )ÚEditDistancer–   Údamerau_levenshteinÚnormalize_scoreTrŒ   r“   )r–   Ú	normalizeÚedit_distancer˜   z, Normalize: r™   r›   )Ú"langsmith._internal._edit_distancer¥   r�   rž   rJ   r*   r0   r†   )
r   r”   r•   r�   r¥   r–   r¨   r¡   r@   r¢   s
             r   r©   Ú_Expect.edit_distanceF  s«   € õF 	Dà—˜2ˆØ—‘˜HÓ%×>Ð)>ˆØ—J‘JÐ0°$Ó7ˆ	Ù ¨Ñ/ˆ	Ø×"Ñ"¨jÐ"ÐNˆØ$Ñ=ˆØ×ÑØàØ'Ø# F 8¨=¸¸ÐDñô	
ô Ø�L‰LØØØ—m‘mñ	
ð 	
r   c                ó@   • [        U R                  SXR                  S9$ )zü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.

Example:
    ```python
    expect.value(10).to_be_less_than(20)
    ```
r2   r›   )r*   r0   r†   r[   s     r   r2   Ú_Expect.value€  s   € ô ˜Ÿ™ g¨uÇÁÑNÐNr   r@   )r1   Úsource_run_idrA   c               ór   • U R                  UUSS0UUS.5        [        U R                  X!U R                  S9$ )aN  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'`.

Example:
    ```python
    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›   )rJ   r*   r0   r†   )r   r@   r1   r®   rA   s        r   r@   Ú_Expect.score�  sD   € ð, 	×ÑØàØ (¨.Ð9Ø!.Ø"ñ	ô	
ô ˜Ÿ™ c¸D¿M¹MÑJÐJr   c               ó   • g r|   r   r[   s     r   Ú__call__Ú_Expect.__call__³  s   € Ø36r   c              ó   • g r|   r   rŠ   s     r   r³   r´   ¶  s   € ØCFr   c               óL   • [        US9nU[        La  UR                  U5      $ U$ )Nr   )rƒ   r   r2   )r   r2   r   Úexpecteds       r   r³   r´   ¹  s*   € ô  &Ñ)ˆØœ	Ò!Ø—>‘> %Ó(Ð(Øˆr   c                óJ  • [         R                  " 5       nU(       a  UR                  OS n[        R                  " 5       (       d_  U R
                  (       d  [        R                  " 5       U l        U R                  R                  " U R
                  R                  4XAS.UD6  g g )N)r;   r1   )r6   r7   r9   r3   rD   r0   rE   rF   r†   rG   rH   )r   r1   ÚresultsÚcurrent_runr;   s        r   rJ   Ú_Expect._submit_feedbackÄ  sy   € Ü×-Ò-Ó/ˆÞ)4�×%Ò%¸$ˆÜ×1Ò1×3Ñ3Ø—<—<Ü!×3Ò3Ó5�”Ø�M‰M× Ò Ø—‘×,Ñ,ðØ5;ñØHOóð 4r   )r0   r†   )r   rz   )r”   r"   r•   r"   r�   zOptional[EmbeddingConfig]r!   r*   )r”   r"   r•   r"   r�   zOptional[EditDistanceConfig]r!   r*   )r2   r   r!   r*   )
r@   zUnion[float, int, bool]r1   r"   r®   zOptional[ls_client.ID_TYPE]rA   r{   r!   r*   )r   zls_client.Clientr!   rƒ   )r   rz   r2   zOptional[Any]r!   zUnion[_Expect, _Matcher])r1   r"   r¹   Údict)r#   r$   r%   r&   r'   r<   r—   r©   r2   r@   r
   r³   r   rJ   r(   r   r   r   rƒ   rƒ     s  † Ù;à?C÷ ;ð -1ñ;
àð;
ð ð;
ð
 *ð;
ð 
õ;
ðD 04ñ8
àð8
ð ð8
ð
 -ð8
ð 
õ8
ôtOð( Ø59Ø!%ñKà&ðKð ð	Kð
 3ðKð ðKð 
õKðF Û6ó Ø6àÛFó ØFð  )à-1ð		ð +ð		àð	ð
 
"õ	÷r   rƒ   Úexpect) r'   Ú
__future__r   r‡   ru   Útypingr   r   r   r   r   r	   r
   Ú	langsmithr   Ú	ls_clientr   r6   r   rE   r   r3   rª   r   rœ   r   r   r   r*   rƒ   r½   Ú__all__r   r   r   Ú<module>rÃ      sy   ðñ0õd #ã Û ÷÷ ñ õ *Ý 'Ý %Ý 'æÝEÝG÷	ñ 	ñ ‹N€	÷c
ñ c
÷LKñ Kñ\ 
‹€àˆ*�r   