> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 应用结构

> LangGraph 应用由一个或多个图、配置文件（langgraph.json）、指定依赖关系的文件以及可选的 .env 文件（用于指定环境变量）组成。

LangGraph 应用由一个或多个图、配置文件（`langgraph.json`）、指定依赖关系的文件以及可选的 `.env` 文件（用于指定环境变量）组成。

本指南展示了一个典型应用的架构，并指导您如何为使用 [LangSmith 部署](https://docs.langchain.com/langsmith/deployment) 部署应用提供所需配置。

> [!NOTE]
>
> LangSmith 部署是一个受管理的托管平台，用于部署和扩展 LangGraph 智能体。它处理基础设施、扩展性和运维问题，使您可以直接从仓库部署您的持久化、长时间运行的智能体。更多详情请参考 [部署文档](https://docs.langchain.com/langsmith/deployment)。

## 核心概念

使用 LangSmith 部署时，需提供以下信息：

1. 一个 [LangGraph 配置文件](#configuration-file-concepts)（`langgraph.json`），用于指定应用所需的依赖项、图和环境变量。
2. 实现 应用逻辑的 [图](#图)。
3. 指定运行应用所需的 [依赖项](#依赖项) 的文件。
4. 应用运行所需的 [环境变量](#环境变量)。

## 文件结构

以下是应用目录结构的示例：

:::python

> [tab] Python (requirements.txt)
>
> ```plaintext theme={null}
> my-app/
> ├── my_agent # all project code lies within here
> │   ├── utils # utilities for your graph
> │   │   ├── __init__.py
> │   │   ├── tools.py # tools for your graph
> │   │   ├── nodes.py # node functions for your graph
> │   │   └── state.py # state definition of your graph
> │   ├── __init__.py
> │   └── agent.py # code for constructing your graph
> ├── .env # environment variables
> ├── requirements.txt # package dependencies
> └── langgraph.json # configuration file for LangGraph
> ```

> [tab] Python (pyproject.toml)
>
> ```plaintext theme={null}
> my-app/
> ├── my_agent # all project code lies within here
> │   ├── utils # utilities for your graph
> │   │   ├── __init__.py
> │   │   ├── tools.py # tools for your graph
> │   │   ├── nodes.py # node functions for your graph
> │   │   └── state.py # state definition of your graph
> │   ├── __init__.py
> │   └── agent.py # code for constructing your graph
> ├── .env # environment variables
> ├── langgraph.json  # configuration file for LangGraph
> └── pyproject.toml # dependencies for your project
> ```

:::

:::js

```plaintext theme={null}
my-app/
├── src # all project code lies within here
│   ├── utils # optional utilities for your graph
│   │   ├── tools.ts # tools for your graph
│   │   ├── nodes.ts # node functions for your graph
│   │   └── state.ts # state definition of your graph
│   └── agent.ts # code for constructing your graph
├── package.json # package dependencies
├── .env # environment variables
└── langgraph.json # configuration file for LangGraph
```

:::

> [!NOTE]
>
> LangGraph 应用的目录结构可能因编程语言和包管理器而异。

## 配置文件

`langgraph.json` 文件是一个 JSON 文件，用于指定部署 LangGraph 应用所需的依赖项、图、环境变量和其他设置。

详见 [LangGraph 配置文件参考](https://docs.langchain.com/langsmith/cli#configuration-file) 查看所有支持的 JSON 键信息。

> [!TIP]
>
> [LangGraph CLI](https://docs.langchain.com/langsmith/cli) 默认使用当前目录的 `langgraph.json` 配置文件。

### 示例

:::python

* 依赖项包含一个本地自定义包和 `langchain_openai` 包。
* 从 `./your_package/your_file.py` 文件加载单个图，并使用变量 `variable`。
* 环境变量从 `.env` 文件加载。

```json theme={null}
{
  "dependencies": ["langchain_openai", "./your_package"],
  "graphs": {
    "my_agent": "./your_package/your_file.py:agent"
  },
  "env": "./.env"
}
```

:::

:::js

* 依赖项从本地目录的依赖文件加载（例如 `package.json`）。
* 从 `./your_package/your_file.js` 文件加载单个图，并使用函数 `agent`。
* 环境变量 `OPENAI_API_KEY` 内置设置。

```json theme={null}
{
  "dependencies": ["."],
  "graphs": {
    "my_agent": "./your_package/your_file.js:agent"
  },
  "env": {
    "OPENAI_API_KEY": "secret-key"
  }
}
```

:::

## 依赖项

:::python\
LangGraph 应用可能依赖其他 Python 包。\
:::

:::js\
LangGraph 应用可能依赖 TypeScript/JavaScript 库。\
:::

为正确配置依赖项，通常需要以下信息：

:::python

1. 指定依赖项的文件（例如 `requirements.txt`、`pyproject.toml` 或 `package.json`）。\
   :::

:::js

1. 指定依赖项的文件（例如 `package.json`）。\
   :::

2. [LangGraph 配置文件](#configuration-file-concepts) 中的 `dependencies` 键，用于指定运行 LangGraph 应用所需的依赖项。

3. 可通过 [LangGraph 配置文件](#configuration-file-concepts) 中的 `dockerfile_lines` 键指定额外的二进制文件或系统库。

## 图

在 [LangGraph 配置文件](#configuration-file-concepts) 中使用 `graphs` 键指定部署 LangGraph 应用中可用的图。

您可以在配置文件中指定一个或多个图。每个图需通过唯一名称和以下路径之一标识：\
（1）编译后的图路径\
（2）定义图的功能函数

## 环境变量

若本地部署 LangGraph 应用，可在 [LangGraph 配置文件](#configuration-file-concepts) 的 `env` 键中配置环境变量。

对于生产环境部署，通常建议在部署环境中配置环境变量。

***

> 官方原文：[src/oss/langgraph/application-structure.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/langgraph/application-structure.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.