Skip to content
LangSmith 文档版本 跟随上游 源快照 17cac5c7 更新于 2026-10-18

Monorepo 支持

LangSmith 支持从 monorepo 设置部署 agent,其中 agent 代码可能依赖仓库其他位置的共享包。本指南展示如何组织 monorepo 并配置 langgraph.json 文件以使用共享依赖。

仓库结构

完整可运行的示例,请参见:

plaintext
my-monorepo/
├── shared-utils/           # Shared Python package
│   ├── __init__.py
│   ├── common.py
│   └── pyproject.toml      # Or setup.py
├── agents/
│   └── customer-support/   # Agent directory
│       ├── agent/
│       │   ├── __init__.py
│       │   └── graph.py
│       ├── langgraph.json  # Config file in agent directory
│       ├── .env
│       └── pyproject.toml  # Agent dependencies
└── other-service/
    └── ...
plaintext
my-monorepo/
├── package.json            # Root package.json with workspaces
├── shared-utils/           # Shared TypeScript package
│   ├── package.json
│   ├── src/
│   │   └── index.ts
│   └── tsconfig.json
├── agents/
│   └── customer-support/   # Agent directory
│       ├── src/
│       │   └── agent.ts
│       ├── langgraph.json  # Config file in agent directory
│       ├── package.json    # Agent dependencies
│       ├── .env
│       └── tsconfig.json
└── other-service/
    └── ...

LangGraph.json 配置

将 langgraph.json 文件放在 agent 目录中(不要放在 monorepo 根目录)。确保该文件遵循所需结构:

json
{
  "dependencies": [
    ".",                    # Current agent package
    "../../shared-utils"    # Relative path to shared package
  ],
  "graphs": {
    "customer_support": "./agent/graph.py:graph"
  },
  "env": ".env"
}
json
{
  "node_version": "20",
  "graphs": {
    "customer_support": "./src/agent.ts:graph"
  },
  "env": ".env"
}

Python 实现会自动处理父目录中的包,方式如下:

  • 检测以 "." 开头的相对路径。
  • 按需将父目录添加到 Docker 构建上下文。
  • 同时支持真实包(带有 pyproject.toml/setup.py)和简单的 Python 模块。

对于 JavaScript monorepo:

  • 共享工作区依赖会由你的包管理器自动解析。
  • 你的 package.json 应使用 workspace 语法引用共享包。

agent 目录中的 package.json 示例:

json
{
  "name": "customer-support-agent",
  "dependencies": {
    "@company/shared-utils": "workspace:*",
    "@langchain/langgraph": "^0.2.0"
  }
}

构建应用

运行 langgraph build:

bash
cd agents/customer-support
langgraph build -t my-customer-support-agent
bash
# Run from the root of the monorepo
langgraph build -t my-customer-support-agent -c agents/customer-support/langgraph.json

Python 构建过程:

  1. 自动检测相对依赖路径。
  2. 将共享包复制到 Docker 构建上下文。
  3. 按正确顺序安装所有依赖。
  4. 无需特殊标志或命令。

JavaScript 构建过程:

  1. 使用你调用 langgraph build 时所在的目录(本例中为 monorepo 根目录)作为构建上下文。
  2. 自动检测你的包管理器(yarn、npm、pnpm、bun)。
  3. 根据项目配置运行相应的安装流程。
  4. 使用包含 langgraph.json 的目录定位要构建的应用。

提示与最佳实践

  1. 将 agent 配置放在 agent 目录中:将 langgraph.json 文件放在具体的 agent 目录中,而不是 monorepo 根目录。这样可以在同一个 monorepo 中支持多个 agent,而无需将它们全部部署到同一个 LangSmith 部署中。

  2. 为 Python 使用相对路径:对于 Python monorepo,在 dependencies 数组中使用类似 "../../shared-package" 的相对路径。

  3. 为 JS 利用 workspace 功能:对于 JavaScript/TypeScript,使用包管理器的 workspace 功能管理包之间的依赖。

  4. 先在本地测试:部署前始终在本地测试构建,确保所有依赖都正确解析。

  5. 环境变量:将环境文件(.env)保存在 agent 目录中,用于特定环境的配置。


BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。