跳至主要内容

贡献集成

首先,请确保您已安装 贡献代码 指南中概述的所有依赖项。

您可以通过多种方式为 LangChain 贡献集成。

  • 社区:针对主要由 LangChain 和开源社区维护的轻量级集成。
  • 合作伙伴包:针对由 LangChain 和合作伙伴共同维护的独立包。

在大多数情况下,新的集成应添加到社区包中。合作伙伴包需要作为单独的包进行更多维护,因此在创建新的合作伙伴包之前,请与 LangChain 团队确认。

在以下部分中,我们将逐步介绍如何从一个名为 Parrot Link AI 的假公司为每个包做出贡献。

社区包

langchain-community 包位于 libs/community 中,包含大多数集成。

它可以通过 pip install langchain-community 安装,并且导出的成员可以通过以下代码导入

from langchain_community.chat_models import ChatParrotLink
from langchain_community.llms import ParrotLinkLLM
from langchain_community.vectorstores import ParrotLinkVectorStore

community 包依赖于手动安装的依赖包,因此如果您尝试导入未安装的包,将会看到错误。在我们的虚构示例中,如果您尝试导入 ParrotLinkLLM 而未安装 parrot-link-sdk,则在尝试使用它时,将会看到一个 ImportError,提示您安装它。

假设我们想要为 Parrot Link AI 实现一个聊天模型。我们将在 libs/community/langchain_community/chat_models/parrot_link.py 中创建一个新文件,其中包含以下代码

from langchain_core.language_models.chat_models import BaseChatModel

class ChatParrotLink(BaseChatModel):
"""ChatParrotLink chat model.

Example:
.. code-block:: python

from langchain_community.chat_models import ChatParrotLink

model = ChatParrotLink()
"""

...
API 参考:BaseChatModel

并且我们将在以下位置编写测试

  • 单元测试:libs/community/tests/unit_tests/chat_models/test_parrot_link.py
  • 集成测试:libs/community/tests/integration_tests/chat_models/test_parrot_link.py

并向以下位置添加文档

  • docs/docs/integrations/chat/parrot_link.ipynb

LangChain 代码库中的合作伙伴包

注意

在开始创建合作伙伴包之前,请与 LangChain 团队确认您的意图。合作伙伴包需要作为单独的包进行更多维护,因此我们会关闭在未经事先讨论的情况下添加新合作伙伴包的 PR。请参阅上一节了解如何添加社区集成。

合作伙伴包可以托管在 LangChain 单体存储库或外部存储库中。

LangChain 代码库中的合作伙伴包位于 libs/partners/{partner} 中,并且包源代码位于 libs/partners/{partner}/langchain_{partner} 中。

用户可以通过 pip install langchain-{partner} 安装包,并且可以通过以下代码导入包成员

from langchain_{partner} import X

设置新包

要设置新的合作伙伴包,请使用最新版本的 LangChain CLI。您可以使用以下命令安装或更新它:

pip install -U langchain-cli

假设您要为一家名为 Parrot Link AI 的公司创建一个新的合作伙伴包。

然后,运行以下命令以创建一个新的合作伙伴包

cd libs/partners
langchain-cli integration new
> Name: parrot-link
> Name of integration in PascalCase [ParrotLink]: ParrotLink

这将在 libs/partners/parrot-link 中创建一个具有以下结构的新包

libs/partners/parrot-link/
langchain_parrot_link/ # folder containing your package
...
tests/
...
docs/ # bootstrapped docs notebooks, must be moved to /docs in monorepo root
...
scripts/ # scripts for CI
...
LICENSE
README.md # fill out with information about your package
Makefile # default commands for CI
pyproject.toml # package metadata, mostly managed by Poetry
poetry.lock # package lockfile, managed by Poetry
.gitignore

实现您的包

首先,添加您的包所需的任何依赖项,例如您公司的 SDK

poetry add parrot-link-sdk

如果您需要单独的依赖项进行类型检查,则可以使用以下命令将它们添加到 typing 组中:

poetry add --group typing types-parrot-link-sdk

然后,在 libs/partners/parrot-link/langchain_parrot_link 中实现您的包。

默认情况下,这将包括聊天模型、LLM 和/或向量存储的存根。您应该删除任何不使用的文件,并将其从 __init__.py 中删除。

编写单元和集成测试

tests/ 目录中提供了一些基本测试。您应该添加更多测试以涵盖您的包的功能。

有关运行和实现测试的信息,请参阅 测试指南

编写文档

文档是从 docs/ 目录中的 Jupyter 笔记本生成的。您应该将包含示例的笔记本放置到单体存储库根目录中的相关 docs/docs/integrations 目录中。

(如有必要)弃用社区集成

注意:这仅在您将现有的社区集成迁移到合作伙伴包时才需要。如果您集成的组件对 LangChain 来说是全新的(即尚未包含在 community 包中),则可以跳过此步骤。

假设我们将 ChatParrotLink 聊天模型从社区包迁移到合作伙伴包。我们需要在社区包中弃用旧模型。

我们可以通过如下所示在 libs/community/langchain_community/chat_models/parrot_link.py 中向旧模型添加 @deprecated 装饰器来实现这一点。

在更改之前,我们的聊天模型可能如下所示

class ChatParrotLink(BaseChatModel):
...

更改后,它将如下所示

from langchain_core._api.deprecation import deprecated

@deprecated(
since="0.0.<next community version>",
removal="0.2.0",
alternative_import="langchain_parrot_link.ChatParrotLink"
)
class ChatParrotLink(BaseChatModel):
...
API 参考:deprecated

您应该对您迁移到合作伙伴包的每个组件执行此操作。

其他步骤

贡献者步骤

  • .github/workflows/_integration_test.yml 中的“手动集成工作流”中添加密钥名称。
  • .github/workflows/_release.yml 中的“发布工作流”(用于预发布测试)中添加密钥。

维护者步骤(贡献者不应执行这些步骤)

  • 设置 pypi 和测试 pypi 项目。
  • 向 Github Actions 添加凭据密钥。
  • 将包添加到 conda-forge。

外部存储库中的合作伙伴包

外部存储库中的合作伙伴包必须由 LangChain 团队和合作伙伴组织协调,以确保它们得到维护和更新。

如果您有兴趣在外部存储库中创建合作伙伴包,请先在 LangChain 代码库中创建一个,然后与 LangChain 团队联系,讨论如何将其迁移到外部存储库。


此页面是否有帮助?


您还可以留下详细的反馈 在 GitHub 上.