_Expect#

class langsmith._expect._Expect(*, client: Client | None = None)[source]#

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

方法

__init__(*[, client])

edit_distance(prediction, reference, *[, config])

计算预测值和参考值之间的字符串距离。

embedding_distance(prediction, reference, *)

计算预测值和参考值之间的嵌入距离。

score(score, *[, key, source_run_id, comment])

将数值分数记录到 LangSmith。

value(value)

创建 _Matcher 实例,用于对给定值进行断言。

参数:

client (Optional[ls_client.Client])

__init__(*, client: Client | None = None)[source]#
参数:

client (Client | None)

edit_distance(prediction: str, reference: str, *, config: EditDistanceConfig | None = None) _Matcher[source]#

计算预测值和参考值之间的字符串距离。

此方法将字符串距离 (Damerau-Levenshtein) 记录到 LangSmith,并返回一个 _Matcher 实例,用于对距离值进行断言。

这依赖于 rapidfuzz 包进行字符串距离计算。

参数:
  • prediction (str) – 要比较的预测字符串。

  • reference (str) – 要与之比较的参考字符串。

  • config (Optional[EditDistanceConfig]) –

    字符串距离评估器的可选配置。支持的选项: - metric: 用于比较的距离度量。

    支持的值:“damerau_levenshtein”、“levenshtein”、“jaro”、“jaro_winkler”、“hamming”、“indel”。

    • normalize_score: 是否将分数归一化到 0 和 1 之间。

返回:

字符串距离值的 _Matcher 实例。

返回类型:

_Matcher

示例

>>> expect.edit_distance("hello", "helo").to_be_less_than(1)
embedding_distance(prediction: str, reference: str, *, config: EmbeddingConfig | None = None) _Matcher[source]#

计算预测值和参考值之间的嵌入距离。

此方法将嵌入距离记录到 LangSmith,并返回一个 _Matcher 实例,用于对距离值进行断言。

默认情况下,这使用 OpenAI API 计算嵌入。

参数:
  • prediction (str) – 要比较的预测字符串。

  • reference (str) – 要与之比较的参考字符串。

  • config (Optional[EmbeddingConfig]) –

    嵌入距离评估器的可选配置。支持的选项: - encoder: 用于将输入字符串列表编码为嵌入的自定义编码器函数。

    默认为 OpenAI API。

    • metric: 用于比较的距离度量。

      支持的值:“cosine”、“euclidean”、“manhattan”、“chebyshev”、“hamming”。

返回:

嵌入距离值的 _Matcher 实例。

返回类型:

_Matcher

示例

>>> expect.embedding_distance(
...     prediction="hello",
...     reference="hi",
... ).to_be_less_than(1.0)
score(score: float | int | bool, *, key: str = 'score', source_run_id: UUID | str | None = None, comment: str | None = None) _Matcher[source]#

将数值分数记录到 LangSmith。

参数:
  • score (float | int | bool) – 要记录的分数值。

  • key (str) – 用于记录分数的键。默认为“score”。

  • source_run_id (UUID | str | None)

  • comment (str | None)

返回类型:

_Matcher

示例

>>> expect.score(0.8)
<langsmith._expect._Matcher object at ...>
>>> expect.score(0.8, key="similarity").to_be_greater_than(0.7)
value(value: Any) _Matcher[source]#

创建 _Matcher 实例,用于对给定值进行断言。

参数:

value (Any) – 要对其进行断言的值。

返回:

给定值的 _Matcher 实例。

返回类型:

_Matcher

示例

>>> expect.value(10).to_be_less_than(20)