如何为部署设置LangGraph应用程序¶
为了将LangGraph应用程序部署到LangGraph云(或自托管),必须使用LangGraph API配置文件对其进行配置。本指南将讨论使用pyproject.toml
定义项目依赖项的基本步骤,以设置LangGraph应用程序进行部署。
此演练基于此仓库,你可以通过它来学习如何设置你的LangGraph应用程序进行部署。
使用requirements.txt进行设置
如果你更喜欢使用requirements.txt
进行依赖管理,请参阅此指南。
使用单体仓库进行设置
如果你有兴趣部署位于单体仓库内的图,请查看此仓库中的示例。
最终的仓库结构将类似于以下形式:
my-app/
├── my_agent # 所有项目代码都位于此处
│ ├── utils # 图的工具
│ │ ├── __init__.py
│ │ ├── tools.py # 图的工具
│ │ ├── nodes.py # 图的节点函数
│ │ └── state.py # 图的状态定义
│ ├── __init__.py
│ └── agent.py # 构建图的代码
├── .env # 环境变量
├── langgraph.json # LangGraph配置文件
└── pyproject.toml # 项目的依赖项
在每一步之后,都会提供一个示例文件目录,以展示代码可以如何组织。
指定依赖项¶
依赖项可以可选地在以下文件之一中指定: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 = "一个优秀的 LangGraph 云代理构建。"
authors = ["鹦鹉波利 <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 API配置文件时使用。
示例agent.py
文件,展示了如何从你定义的其他模块导入(此处未显示模块代码,请参见此仓库查看其实现):
API Reference: 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 # 导入节点
from my_agent.utils.state import AgentState # 导入状态
# 定义配置
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 # 所有项目代码都位于这里
│ ├── utils # 图的工具函数
│ │ ├── __init__.py
│ │ ├── tools.py # 图的工具
│ │ ├── nodes.py # 图的节点函数
│ │ └── state.py # 图的状态定义
│ ├── __init__.py
│ └── agent.py # 构建图的代码
├── .env
└── pyproject.toml
创建LangGraph API配置¶
创建一个名为langgraph.json
的LangGraph API配置文件。有关配置文件中JSON对象每个键的详细解释,请参阅LangGraph CLI参考。
示例langgraph.json
文件:
请注意,CompiledGraph
变量名出现在顶级graphs
键下每个子键值的末尾(即::<variable_name>
)。
配置文件位置
LangGraph API配置文件必须放置在与包含编译图和相关依赖项的Python文件处于同一级别或更高级别的目录中。
示例文件目录:
my-app/
├── my_agent # 所有项目代码均位于此处
│ ├── utils # 图的工具
│ │ ├── __init__.py
│ │ ├── tools.py # 图的工具
│ │ ├── nodes.py # 图的节点函数
│ │ └── state.py # 图的状态定义
│ ├── __init__.py
│ └── agent.py # 构建图的代码
├── .env # 环境变量
├── langgraph.json # LangGraph配置文件
└── pyproject.toml # 项目的依赖项
下一步¶
在设置好项目并将它放入GitHub仓库后,是时候部署你的应用了。