跳到主要內容
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 的指南


此頁面是否有幫助?