_expect#

对测试结果进行近似断言,作为“期望”。

此模块旨在用于使用 @pytest.mark.decorator 装饰器装饰的测试用例中。它允许您记录关于测试用例的分数,并可以选择进行断言,将断言作为“期望”反馈记录到 LangSmith。

示例用法
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)

_expect._Expect(*[, client])

一个用于设置测试结果期望的类。