跳到主要内容
Open on GitHub

如何向集成添加标准测试

当为自己创建自定义类或在 LangChain 集成中发布时,添加标准测试以确保其按预期工作非常重要。本指南将向您展示如何为每种集成类型添加标准测试。

设置

首先,让我们安装 2 个依赖项

  • langchain-core 将定义我们要导入以定义自定义工具的接口。
  • langchain-tests 将提供我们要使用的标准测试,以及运行它们所需的 pytest 插件。建议固定到最新版本:
注意

由于新版本的 langchain-tests 中添加的测试可能会破坏您的 CI/CD 管道,我们建议固定 langchain-tests 的版本以避免意外更改。

如果您遵循了之前的指南,您应该已经安装了这些依赖项!

poetry add langchain-core
poetry add --group test langchain-tests==<latest_version>
poetry install --with test

添加和配置标准测试

langchain-tests 包中有 2 个命名空间

  • 单元测试 (langchain_tests.unit_tests):旨在用于隔离测试组件,且无法访问外部服务
  • 集成测试 (langchain_tests.integration_tests):旨在用于测试可以访问外部服务的组件(特别是组件旨在与之交互的外部服务)。

两种类型的测试都作为 pytest 基于类的测试套件 实现。

通过子类化每种类型的标准测试的基类(见下文),您将获得该类型的所有标准测试,并且可以覆盖测试套件用于配置测试的属性。

为了以与本指南相同的方式运行测试,我们建议在两个测试子目录下的测试文件中子类化这些类

  • tests/unit_tests 用于单元测试
  • tests/integration_tests 用于集成测试

实施标准测试

在以下选项卡中,我们将展示如何为每种组件类型实施标准测试

要为聊天模型配置标准测试,我们子类化 ChatModelUnitTestsChatModelIntegrationTests。在每个子类上,我们覆盖以下 @property 方法,以指定要测试的聊天模型和聊天模型的配置

属性描述
chat_model_class要测试的聊天模型的类
chat_model_params传递给聊天的参数
模型的构造函数

此外,聊天模型标准测试还测试一系列行为,从最基本的要求(生成对查询的响应)到可选功能,如多模态支持和工具调用。为了使测试运行成功

  1. 如果某个功能旨在由模型支持,则应通过测试;
  2. 如果某个功能不打算由模型支持,则应跳过。

“可选”功能的测试通过可以在测试模型子类上覆盖的一组属性来控制。

您可以在 单元测试集成测试 的 API 参考中查看可配置功能的完整列表

例如,要为图像输入启用集成测试,我们可以实现

@property
def supports_image_inputs(self) -> bool:
return True

在集成测试类上。

注意

有关运行哪些测试、如何跳过每个测试以及每个测试的故障排除技巧的详细信息,请参见 API 参考。查看详细信息

单元测试示例

tests/unit_tests/test_chat_models.py
"""Test chat model integration."""

from typing import Type

from langchain_parrot_link.chat_models import ChatParrotLink
from langchain_tests.unit_tests import ChatModelUnitTests


class TestChatParrotLinkUnit(ChatModelUnitTests):
@property
def chat_model_class(self) -> Type[ChatParrotLink]:
return ChatParrotLink

@property
def chat_model_params(self) -> dict:
# These should be parameters used to initialize your integration for testing
return {
"model": "bird-brain-001",
"temperature": 0,
"parrot_buffer_length": 50,
}

集成测试示例

tests/integration_tests/test_chat_models.py
"""Test ChatParrotLink chat model."""

from typing import Type

from langchain_parrot_link.chat_models import ChatParrotLink
from langchain_tests.integration_tests import ChatModelIntegrationTests


class TestChatParrotLinkIntegration(ChatModelIntegrationTests):
@property
def chat_model_class(self) -> Type[ChatParrotLink]:
return ChatParrotLink

@property
def chat_model_params(self) -> dict:
# These should be parameters used to initialize your integration for testing
return {
"model": "bird-brain-001",
"temperature": 0,
"parrot_buffer_length": 50,
}

运行测试

您可以使用以下命令从项目根目录运行这些测试

# run unit tests without network access
poetry run pytest --disable-socket --allow-unix-socket --asyncio-mode=auto tests/unit_tests

# run integration tests
poetry run pytest --asyncio-mode=auto tests/integration_tests

测试套件信息和故障排除

有关可用的标准测试套件的完整列表,以及有关包含哪些测试以及如何排除常见问题的信息,请参阅 标准测试 API 参考

您可以在该 API 参考中列出的各个测试套件下查看故障排除指南。例如,此处是 ChatModelIntegrationTests.test_usage_metadata 的指南


此页内容是否对您有帮助?