_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)