贡献集成
¥Contribute Integrations
首先,请确保你已安装 贡献代码 指南中列出的所有依赖。
¥To begin, make sure you have all the dependencies outlined in guide on Contributing Code.
你可以在几个不同的地方为 LangChain 贡献集成:
¥There are a few different places you can contribute integrations for LangChain:
社区:对于主要由 LangChain 和开源社区维护的轻量级集成。
¥Community: For lighter-weight integrations that are primarily maintained by LangChain and the Open Source Community.
合作伙伴软件包:对于由 LangChain 和合作伙伴共同维护的独立软件包。
¥Partner Packages: For independent packages that are co-maintained by LangChain and a partner.
大多数情况下,新的集成应该添加到社区包中。合作伙伴包作为单独的包需要更多维护,因此在创建新的合作伙伴包之前,请与 LangChain 团队确认。
¥For the most part, new integrations should be added to the Community package. Partner packages require more maintenance as separate packages, so please confirm with the LangChain team before creating a new partner package.
在接下来的部分中,我们将逐步介绍如何从一家名为 Parrot Link AI
的虚拟公司为每个软件包做出贡献。
¥In the following sections, we'll walk through how to contribute to each of these packages from a fake company, Parrot Link AI
.
社区软件包
¥Community package
@langchain/community
软件包位于 libs/langchain-community
中,包含大多数集成。
¥The @langchain/community
package is in libs/langchain-community
and contains most integrations.
它可以与例如 npm install @langchain/community
一起安装,导出的成员可以通过以下代码导入:
¥It can be installed with e.g. npm install @langchain/community
, and exported members can be imported with code like
import { ChatParrotLink } from "@langchain/community/chat_models/parrot_link";
import { ParrotLinkLLM } from "@langchain/community/llms/parrot_link";
import { ParrotLinkVectorStore } from "@langchain/community/vectorstores/parrot_link";
@langchain/community
软件包依赖于手动安装的依赖包,因此如果你尝试导入未安装的依赖包,将会出现错误。在我们的模拟示例中,如果你尝试在未安装 parrot-link-sdk
的情况下导入 ParrotLinkLLM
,则会看到一条错误消息,提示包导入失败。
¥The @langchain/community
package relies on manually-installed dependent packages, so you will see errors
if you try to import a package that is not installed. In our fake example, if you tried to import ParrotLinkLLM
without installing parrot-link-sdk
, you would see an error telling you that the package failed to import.
假设我们想要为 Parrot Link AI 实现一个聊天模型。我们将在 libs/langchain-community/src/chat_models/parrot_link.ts
中创建一个新文件,其中包含类似以下代码:
¥Let's say we wanted to implement a chat model for Parrot Link AI. We would create a new file in libs/langchain-community/src/chat_models/parrot_link.ts
with something like the following code:
import {
SimpleChatModel,
} from "@langchain/core/language_models/chat_models";
export class ChatParrotLink extends SimpleChatModel {
...
测试位于 src/
目录中,因此你可以将它们写入类似以下的文件:
¥Tests are colocated in the src/
directory, so you could write them in files like the below:
单元测试:
libs/langchain-community/src/chat_models/tests/parrot_link.test.ts
¥Unit tests:
libs/langchain-community/src/chat_models/tests/parrot_link.test.ts
集成测试:
libs/langchain-community/src/chat_models/tests/parrot_link.int.test.ts
¥Integration tests:
libs/langchain-community/src/chat_models/tests/parrot_link.int.test.ts
单元测试不应该包含任何外部 API 调用或需要任何环境变量。
¥Unit tests should not have any external API calls or require any environment variables.
你应该添加文档到:
¥You should add documentation to:
docs/core_docs/docs/integrations/chat/parrot_link.mdx
LangChain 仓库中的合作伙伴软件包
¥Partner package in LangChain repo
合作伙伴包可以托管在 LangChain
单一仓库中。
¥Partner packages can be hosted in the LangChain
monorepo.
LangChain
仓库中的合作伙伴包应放置在 libs/langchain-{partner}
下。
¥Partner packages in the LangChain
repo should be placed under libs/langchain-{partner}
该包由使用 npm install @langchain/{partner}
的用户安装,包成员可以使用如下代码导入:
¥A package is
installed by users with npm install @langchain/{partner}
, and the package members
can be imported with code like:
import { X } from "@langchain/{partner}";
设置新软件包
¥Set up a new package
要设置新的合作伙伴软件包,你可以使用 create-langchain-integration
,这是一个实用程序,它将自动构建一个支持 ESM 和 CJS 入口点的仓库。你可以在 libs/
文件夹中像这样运行它:
¥To set up a new partner package, you can use create-langchain-integration
,
a utility that will automatically scaffold a repo with support for both ESM + CJS entrypoints. You can run it like this within the libs/
folder:
cd libs/
npx create-langchain-integration
然后,按照提示为你的软件包命名。默认软件包将包含聊天模型、LLM 和/或向量存储的存根。你应该删除所有不再使用的文件,并将其从 index.ts
中移除。
¥Then, follow the prompts to name your package.
The default package will include stubs for a Chat Model, an LLM, and/or a Vector Store. You should delete any of the files you won't use and remove them from index.ts
.
依赖
¥Dependencies
如果你的软件包需要依赖,例如你公司的 SDK,你可以照常将它们添加到软件包的 package.json
文件中:
¥If your package needs dependencies, such as your company's SDK, you can add them to your package's package.json
file as normal:
npm install parrot-link-sdk
编写单元测试和集成测试
¥Write Unit and Integration Tests
src/tests/
目录中提供了一些基本测试。你应该添加更多测试来涵盖软件包的功能。
¥Some basic tests are presented in the src/tests/
directory. You should add more tests to cover your package's functionality.
有关运行和实现测试的信息,请参阅 测试指南。
¥For information on running and implementing tests, see the Testing guide.
编写文档
¥Write documentation
请从此处复制并使用相应的模板:
¥Please copy and use the appropriate template from here:
https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-scripts/src/cli/docs/templates
你应该将包含示例的注意本放在 monorepo 根目录中相关的 docs/core_docs/docs/integrations
目录中。
¥You should place the notebooks with examples
in the relevant docs/core_docs/docs/integrations
directory in the monorepo root.
(如有必要)弃用社区集成
¥(If Necessary) Deprecate community integration
注意:仅当你将现有社区集成迁移到合作伙伴包中时才需要这样做。如果你要集成的组件对于 LangChain 来说是全新的(即尚未包含在 community
包中),则可以跳过此步骤。
¥Note: this is only necessary if you're migrating an existing community integration into
a partner package. If the component you're integrating is net-new to LangChain (i.e.
not already in the community
package), you can skip this step.
假设我们将 ChatParrotLink
聊天模型从社区包迁移到了合作伙伴包。我们需要在社区包中弃用旧模型。我们可以使用 @deprecated
TSDoc 注释来实现这一点。
¥Let's pretend we migrated our ChatParrotLink
chat model from the community package to
the partner package. We would need to deprecate the old model in the community package.
We can do this using a @deprecated
TSDoc comment.
在进行更改之前,我们的聊天模型可能如下所示:
¥Before our change, our chat model might look like this:
class ChatParrotLink extends SimpleChatModel {
...
更改后,它将如下所示:
¥After our change, it would look like this:
/** @deprecated Install and import from `@langchain/parrot-link` instead. */
class ChatParrotLink extends SimpleChatModel {
...
你应该对要迁移到合作伙伴包的每个组件执行此操作。
¥You should do this for each component that you're migrating to the partner package.