File: //usr/local/lib/python3.10/dist-packages/langsmith/__pycache__/_expect.cpython-310.pyc
o
���g{: � @ s� d Z ddlmZ ddlZddlZddlmZmZmZm Z m
Z
mZmZ ddl
mZ ddl
mZ ddl
mZ ddl
mZ erHdd lmZ dd
lmZ G dd� d�Ze� ZG d
d� d�ZG dd� d�Ze� ZdgZdS )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 Zd ZdZd
dd�Zddd�Zd S )�_NULL_SENTRYz�A 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 � dS )NF� ��selfr r �</usr/local/lib/python3.10/dist-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 Zd ZdZ d2d3d
d�Zd4d5dd�Zd6dd�Zd7dd�Zd7dd�Zd8d"d#�Z d9d:d&d'�Z
d7d(d)�Zd;d*d+�Zd<d,d-�Z
d=d0d1�ZdS )>�_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 || _ || _|| _|ptjdd�| _t�� }|r|j| _ d S || _ d S )N� ��max_workers)
�_clientr$ r% �ls_utils�ContextThreadPoolExecutorr&