以编程方式使用文档

欢迎贡献代码!无论您是在修复错误、添加功能还是提升性能,您的贡献都将帮助成千上万的开发者获得更好的开发体验。

入门指南

如果您正在寻找可以参与的工作,请查看我们仓库中标记为"需要帮助"的问题:

LangChain

标签

LangGraph

标签

Deep Agents

标签

快速修复:提交错误修复

对于简单的错误修复,您可以立即开始:

Reproduce the issue

在克隆仓库之前,请确保您可以可靠地复现该错误。这有助于确认问题,并为您的修复提供起点。维护者和其他贡献者应该能够根据您的描述复现问题,而无需额外的设置或修改。

Fork the repository

LangChain, LangGraph, or Deep Agents 仓库分叉到您的 个人 GitHub 账户

Clone and setup

        git clone https://github.com/your-username/name-of-forked-repo.git

        # For instance, for LangChain:
        git clone https://github.com/parrot123/langchainjs.git

        # For LangGraph:
        git clone https://github.com/parrot123/langgraphjs.git
        
        # Inside your repo, install dependencies
        pnpm install
        # Create a build for all packages to resolve workspace dependencies
        pnpm build
        

Create a branch

为您的修复创建一个新分支。这有助于保持您的更改井然有序,并使以后提交拉取请求更加容易。

        git checkout -b your-username/short-bugfix-name
        

Write failing tests

添加 单元测试 来确保修复后能通过。这样我们可以验证问题已解决并防止回归

Make your changes

修复错误时遵循我们的 代码质量标准。进行 **最小必要更改** 来解决问题。我们强烈鼓励贡献者在开始编码之前先在问题上评论。例如:

> *"我想处理这个问题。我的预期方法是[...简要描述...]。这是否符合维护者的期望?"*

30 秒的评论通常可以防止在您最初的方法错误时浪费精力。

Run build

运行构建命令以确保包仍然正确构建

        pnpm build
        # or build a specific workspace package
        pnpm --filter @langchain/core build
        

Verify the fix

确保测试通过且没有引入回归。在提交您的 PR 之前,确保所有测试在本地通过

        pnpm lint
        pnpm test

        # For bugfixes involving integrations, also run:
        pnpm test:int

        # Or run tests in a specific workspace package
        cd libs/langchain-core
        pnpm test
        pnpm lint

        # Or run tests for a specific package from the root of the repo
        pnpm --filter @langchain/core test
        pnpm --filter @langchain/core lint
        

Document the change

Update docstrings and/or inline comments if behavior changes

Submit a pull request

请遵循提供的 PR 模板。如果适用,请使用 关闭关键字 (e.g. Fixes #ISSUE_NUMBER以便在您的PR合并时自动关闭该问题。

完整开发环境设置

对于持续开发或较大的贡献:

  1. 查看我们的 贡献指南 了解功能、错误修复和集成
  2. 按照我们的 设置指南 在下方
  3. 了解 仓库结构 和包组织
  4. 学习我们的 开发工作流程 包括测试和代码检查

贡献指南

在您开始为LangChain项目做贡献之前,请花点时间想想您为什么要这样做。如果您唯一的目标是在简历上添加一个"首次贡献"(或者您只是在寻找一个快速的成功),那么您可能更适合参加训练营或在线教程。

为开源项目做贡献需要时间和精力,但这也可以帮助您成为更好的开发者并学习新技能。但是,重要的是要知道,这可能比参加培训课程更难、更慢。也就是说,如果您愿意花时间把事情做好,开源贡献是值得的!

向后兼容性

通过以下方式保持兼容性:

Stable interfaces

始终保留:

- 函数签名和参数名称 - 类接口和方法名称 - 返回值结构和类型 - 公共API的导入路径

Safe changes

可接受的修改:

  • - Adding new optional parameters/type parameters

- 向类添加新方法 - 在不改写行为的情况下提高性能 - 添加新模块或函数

Before making changes

  • 这会破坏现有用户代码吗?
  • - 检查您的目标是否是公共的

- 测试中是否有现有的使用模式?

新功能

我们对新功能保持高标准。我们通常不接受外部贡献者提出的新核心抽象,除非存在一个证明迫切需要的现有问题。这也适用于对基础设施和依赖项的更改。

一般来说,功能贡献要求包括:

Design discussion

打开一个问题描述:

- 您要解决的问题 - 拟议的API设计 - 预期的使用模式

Implementation

- 遵循现有的代码模式 - 包含全面的测试和文档 - 考虑安全影响

Integration considerations

  • - 这如何与现有功能交互?
  • - 是否有性能影响?
  • - 这是否引入新的依赖项?

我们将拒绝可能导致安全漏洞或报告的功能。

安全指南

安全检查清单:

Input validation

  • - 验证并清理所有用户输入
  • - 正确转义模板和查询中的数据
  • - 切勿使用 eval(),因为这可能导致任意代码执行漏洞

Error handling

- 使用特定的异常类型 - 不要在错误消息中暴露敏感信息 - 实施适当的资源清理

Dependencies

- 避免添加硬依赖 - 将可选依赖项保持在最少 - 检查第三方包的安全问题

开发环境

查看完 贡献指南后,在下面的 仓库结构 部分找到您正在处理的组件的包目录。

仓库结构

LangChain

LangChain 被组织为一个包含多个包的单体仓库:

Core packages

  • - **langchain** (位于 libs/langchain/):包含链、代理和检索逻辑的主包
  • - **@langchain/core** (位于 libs/langchain-core/):基础接口和核心抽象

Partner packages

位于 libs/providers/,这些是针对特定集成的独立版本包。例如: - **@langchain/openai**: OpenAI 集成 - **@langchain/anthropic**: Anthropic 集成 - **@langchain/google**: Google 集成

Supporting packages

LangGraph

LangGraph 采用多包 monorepo 结构组织:

Core packages

- **langgraph** (位于 libs/langgraph/):用于构建有状态、多角色代理的核心框架 - **langgraph-prebuilt** (位于 libs/prebuilt/):用于创建和运行代理及工具的高级 API

Checkpoint packages

- **langgraph-checkpoint** (位于 libs/checkpoint/):检查点保存器的基础接口 - **langgraph-checkpoint-postgres** (位于 libs/checkpoint-postgres/):Postgres 实现 - **langgraph-checkpoint-sqlite** (位于 libs/checkpoint-sqlite/):SQLite 实现

SDK and CLI

- **langgraph-sdk** (位于 libs/sdk-py/):Agent Server API 的 Python SDK - **langgraph-cli** (位于 libs/cli/):官方命令行界面

Deep Agents

Deep Agents 采用多包 monorepo 结构组织:

Core packages

- **deepagents** (位于 libs/deepagents/):用于构建具有规划、文件系统和子代理能力的深度代理的核心框架 - **deepagents-code** (位于 libs/code/):Deep Agents Code — 具有对话恢复、网络搜索和沙箱功能的交互式终端界面 - **deepagents-cli** (位于 libs/cli/):部署工具(deepagents deploy, deepagents init, deepagents dev)用于将代理部署到 LangSmith Deployments

Integration packages

- **deepagents-harbor** (位于 libs/harbor/):Harbor 与 LangSmith 追踪的集成 - **deepagents-acp** (位于 libs/acp/):Agent Client Protocol 集成

开发工作流程

预提交钩子

运行测试

我们尽可能优先使用单元测试而非集成测试。单元测试在每次 pull request 时运行,因此应该快速且可靠。集成测试按计划运行,需要更多设置,因此应保留用于确认与外部服务的接口点。

单元测试

位置: src/tests/FILENAME_BEING_TESTED.test.ts

Unit tests cover modular logic that does not require calls to outside APIs. If you add new logic, you should add a unit test. In unit tests, check pre/post processing and mock external dependencies.

要求: - 不允许网络调用 - 测试所有代码路径,包括边缘情况 - 使用 mock 来处理外部依赖

运行单元测试:

# Run the entire test suite
pnpm test

# Or run a specific test file
pnpm test src/tests/FILENAME_BEING_TESTED.test.ts

# Or run a specific test function
pnpm test -t "the test that should be run"

集成测试

位置: src/tests/FILENAME_BEING_TESTED.int.test.ts

集成测试覆盖需要调用外部 API 的逻辑(通常是与其他服务的集成)。

Integration tests require access to external services/provider APIs (which can cost money) and therefore are not run by default.

Not every code change will require an integration test, but keep in mind that we'll require/run integration tests separately as part of our review process.

要求: - 测试与外部服务的真实集成 - 使用环境变量存储 API 密钥 - 如果凭证不可用则优雅地跳过

运行集成测试:

pnpm test:int

代码质量标准

贡献必须符合以下质量要求:

Type hints

必需:为所有函数提供完整类型

        function processDocuments(
            docs: Document[],
            processor: DocumentProcessor,
            batchSize: number = 100
        ): ProcessingResult {
            // ...
        }
        

Documentation

必需: JSDocs 适用于所有导出的函数和接口

        /**
         * Document processing instance.
         */
        interface FooDocumentProcessor {
            /**
             * Process documents in batches.
             *
             * @param docs - List of documents to process.
             * @returns Processing results with success/failure counts.
             */
            process(docs: Document[]): ProcessingResult;
        }

        /**
         * Process documents in batches.
         *
         * @param docs - List of documents to process.
         * @param processor - Document processing instance.
         * @param batchSize - Number of documents per batch.
         * @returns Processing results with success/failure counts.
         */

            docs: Document[],
            processor: DocumentProcessor,
            batchSize: number = 100
        ): ProcessingResult {
            // ...
        }
        

Code style

自动化:格式化和检查:

        pnpm lint    # Check style and types
        pnpm format  # Apply formatting
        

标准: - 描述性变量名 - 拆分复杂函数(目标少于20行) - 遵循代码库中现有的模式

测试编写指南

为了编写有效的测试,需要遵循一些最佳实践:

  • - 将测试封装在一个 describe 块中,以描述被测试的组件
  • - 使用自然语言描述测试名称
  • - 断言要详尽全面
  • - 仅对合理大小的数据对象使用快照

Unit tests

        describe("DocumentProcessor", () => {
            it("Should handle empty document list", () => {
                const processor = new DocumentProcessor();
                const result = processor.process([]);

                expect(result.success).toBe(true);
                expect(result.processedCount).toBe(0);
                expect(result.errors).toHaveLength(0);
            });
        });
        

Integration tests

        describe("ChatOpenAI", () => {
            it("Should test with real API", () => {
                const chat = new ChatOpenAI();
                const response = chat.invoke("Hello");
            });
        });
        

Mock usage

        describe("APIService", () => {
            it("Should call with retry", () => {
                const mockClient = new MockClient();
                const service = new APIService(client: mockClient);
                const result = service.callWithRetry();
            });
        });
        

提交您的 PR

一旦您的测试通过且代码符合质量标准:

  1. 推送您的分支并打开拉取请求
  2. 遵循提供的 PR 模板
  3. 使用 关闭关键字 (e.g., Fixes #123)
  4. 等待 CI 检查完成

获取帮助

我们的目标是打造最易于上手的开发者环境。如果您在搭建过程中遇到任何困难,请在 社区 Slack 或发起 论坛帖子.