如何设置 LangGraph 应用程序以便部署¶
LangGraph 应用程序必须配置一个 LangGraph API 配置文件 才能部署到 LangGraph Cloud(或进行自托管)。本操作指南将讨论使用 pyproject.toml
定义包依赖项来设置 LangGraph 应用程序以便部署的基本步骤。
本教程基于 此仓库,您可以进行尝试,以便详细了解如何设置 LangGraph 应用程序以便部署。
使用 requirements.txt 设置
如果您更喜欢使用 requirements.txt
进行依赖管理,请查看 此操作指南。
使用 Monorepo 设置
如果您有兴趣部署位于 monorepo 中的图,请查看 此仓库,以获取如何操作的示例。
最终的仓库结构将类似于这样
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
在每个步骤之后,都会提供一个示例文件目录,以演示代码如何组织。
指定依赖¶
依赖项可以选择在以下文件中指定:pyproject.toml
、setup.py
或 requirements.txt
。如果未创建这些文件中的任何一个,则稍后可以在 LangGraph API 配置文件 中指定依赖项。
以下依赖项将包含在镜像中,您也可以在代码中使用它们,只要版本范围兼容即可
langgraph>=0.2.56,<0.4.0
langgraph-sdk>=0.1.53
langgraph-checkpoint>=2.0.15,<3.0
langchain-core>=0.2.38,<0.4.0
langsmith>=0.1.63
orjson>=3.9.7
httpx>=0.25.0
tenacity>=8.0.0
uvicorn>=0.26.0
sse-starlette>=2.1.0,<2.2.0
uvloop>=0.18.0
httptools>=0.5.0
jsonschema-rs>=0.20.0
structlog>=23.1.0
示例 pyproject.toml
文件
[tool.poetry]
name = "my-agent"
version = "0.0.1"
description = "An excellent agent build for LangGraph cloud."
authors = ["Polly the parrot <1223+polly@users.noreply.github.com>"]
license = "MIT"
readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.9"
langgraph = "^0.2.0"
langchain-fireworks = "^0.1.3"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
示例文件目录
指定环境变量¶
环境变量可以选择在一个文件(例如 .env
)中指定。请参阅 环境变量参考 以配置部署的其他变量。
示例 .env
文件
示例文件目录
定义图¶
实现您的图!图可以在单个文件或多个文件中定义。请记下要包含在 LangGraph 应用程序中的每个 CompiledGraph 的变量名。这些变量名将在稍后创建 LangGraph API 配置文件 时使用。
示例 agent.py
文件,展示了如何从您定义的其他模块中导入(此处未显示模块的代码,请参阅 此仓库 查看其实现)
API 参考:StateGraph | END | START
# my_agent/agent.py
from typing import Literal
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, END, START
from my_agent.utils.nodes import call_model, should_continue, tool_node # import nodes
from my_agent.utils.state import AgentState # import state
# Define the config
class GraphConfig(TypedDict):
model_name: Literal["anthropic", "openai"]
workflow = StateGraph(AgentState, config_schema=GraphConfig)
workflow.add_node("agent", call_model)
workflow.add_node("action", tool_node)
workflow.add_edge(START, "agent")
workflow.add_conditional_edges(
"agent",
should_continue,
{
"continue": "action",
"end": END,
},
)
workflow.add_edge("action", "agent")
graph = workflow.compile()
将 CompiledGraph
赋值给变量
LangGraph Cloud 的构建过程要求将 CompiledGraph
对象赋值给 Python 模块顶层的一个变量。
示例文件目录
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
└── pyproject.toml
创建 LangGraph API 配置¶
创建一个名为 langgraph.json
的 LangGraph API 配置文件。请参阅 LangGraph CLI 参考,详细了解配置文件的 JSON 对象中的每个键。
示例 langgraph.json
文件
请注意,CompiledGraph
的变量名出现在顶层 graphs
键中每个子键的值的末尾(即 :<变量名>
)。
配置位置
LangGraph API 配置文件必须放置在与包含编译图和相关依赖项的 Python 文件处于同一级别或更高级别的目录中。
示例文件目录
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
下一页¶
设置好项目并将其放到 github 仓库后,就可以 部署您的应用程序 了。