应用程序结构¶
概述¶
LangGraph 应用程序由一个或多个图、LangGraph API 配置文件 (langgraph.json
)、一个指定依赖项的文件和一个可选的 .env 文件(指定环境变量)组成。
本指南展示了 LangGraph 应用程序的典型结构,并展示了如何指定使用 LangGraph 平台部署 LangGraph 应用程序所需的信息。
关键概念¶
要使用 LangGraph 平台进行部署,应提供以下信息
- 一个 LangGraph API 配置文件 (
langgraph.json
),用于指定应用程序的依赖项、图和环境变量。 - 实现应用程序逻辑的图。
- 一个指定运行应用程序所需的依赖项的文件。
- 应用程序运行所需的环境变量。
文件结构¶
以下是 Python 和 JavaScript 应用程序的目录结构示例
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 you 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
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 you 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
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 you 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
注意
LangGraph 应用程序的目录结构可能因编程语言和使用的包管理器而异。
配置文件¶
langgraph.json
文件是一个 JSON 文件,用于指定部署 LangGraph 应用程序所需的依赖项、图、环境变量和其他设置。
该文件支持指定以下信息
键 | 描述 |
---|---|
dependencies |
必需。LangGraph API 服务器的依赖项数组。依赖项可以是以下之一:(1)"." ,它将查找本地 Python 包,(2)应用程序目录 "./local_package" 中的 pyproject.toml 、setup.py 或 requirements.txt ,或(3)包名称。 |
graphs |
必需。从图 ID 到定义编译图或创建图的函数的路径的映射。示例
|
env |
.env 文件的路径或从环境变量到其值的映射。 |
python_version |
3.11 或 3.12 。默认为 3.11 。 |
pip_config_file |
pip 配置文件的路径。 |
dockerfile_lines |
要添加到 Dockerfile 中父镜像导入之后的其他行数组。 |
提示
LangGraph CLI 默认使用当前目录中的配置文件 langgraph.json。
示例¶
- 依赖项涉及自定义本地包和
langchain_openai
包。 - 单个图将从文件
./your_package/your_file.py
加载,变量为variable
。 - 环境变量从
.env
文件加载。
依赖项¶
LangGraph 应用程序可能依赖于其他 Python 包或 JavaScript 库(取决于编写应用程序的编程语言)。
通常需要指定以下信息才能正确设置依赖项
- 目录中指定依赖项的文件(例如,
requirements.txt
、pyproject.toml
或package.json
)。 - LangGraph 配置文件中的
dependencies
键,用于指定运行 LangGraph 应用程序所需的依赖项。 - 任何额外的二进制文件或系统库都可以使用 LangGraph 配置文件中的
dockerfile_lines
键来指定。
图¶
使用 LangGraph 配置文件中的 graphs
键来指定哪些图将在已部署的 LangGraph 应用程序中可用。
您可以在配置文件中指定一个或多个图。每个图都由一个名称(应该是唯一的)和一个路径标识,该路径可以是:(1)编译后的图,或(2)定义创建图的函数。
环境变量¶
如果您在本地使用已部署的 LangGraph 应用程序,则可以在 LangGraph 配置文件的 env
键中配置环境变量。
对于生产部署,您通常希望在部署环境中配置环境变量。
相关内容¶
请参阅以下资源以获取更多信息
- 应用程序结构的操作指南。