Skip to content

如何为部署设置LangGraph应用程序

为了将LangGraph应用程序部署到LangGraph Cloud(或自行托管),必须使用LangGraph API配置文件对其进行配置。本指南将讨论使用requirements.txt指定项目依赖项来设置LangGraph应用程序的基本步骤。

本教程基于此仓库,你可以通过它了解更多关于如何设置你的LangGraph应用程序以供部署的信息。

使用pyproject.toml进行设置

如果你更喜欢使用poetry进行依赖管理,请查看此教程中有关如何使用pyproject.toml进行LangGraph Cloud配置的内容。

使用单仓库进行设置

如果你有兴趣在单仓库内部署图,请参阅仓库中的示例。

最终的仓库结构将类似于以下内容:

my-app/
├── my_agent # 所有项目代码都位于此处
   ├── utils # 图的工具
      ├── __init__.py
      ├── tools.py # 图的工具
      ├── nodes.py # 图的节点函数
      └── state.py # 图的状态定义
   ├── requirements.txt # 包依赖项
   ├── __init__.py
   └── agent.py # 构建图的代码
├── .env # 环境变量
└── langgraph.json # LangGraph配置文件

每一步之后都会提供一个示例文件目录,以展示代码可以如何组织。

指定依赖项

依赖项可以可选地在以下文件之一中指定:pyproject.tomlsetup.pyrequirements.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

示例 requirements.txt 文件:

langgraph
langchain_anthropic
tavily-python
langchain_community
langchain_openai

示例文件目录:

my-app/
├── my_agent # 所有项目代码都在这里
   └── requirements.txt # 包依赖项

指定环境变量

环境变量可以可选地在文件中指定(例如 .env)。有关如何配置部署时的附加变量,请参阅环境变量参考

示例 .env 文件:

MY_ENV_VAR_1=foo
MY_ENV_VAR_2=bar
OPENAI_API_KEY=key

示例文件目录结构:

my-app/
├── my_agent # 所有项目代码均位于此处
   └── requirements.txt # 包依赖项
└── .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 # 图的状态定义
   ├── requirements.txt # 包依赖项
   ├── __init__.py
   └── agent.py # 构建图的代码
└── .env # 环境变量

创建LangGraph API配置

创建一个名为langgraph.jsonLangGraph API配置文件。有关配置文件中JSON对象每个键的详细解释,请参阅LangGraph CLI参考

示例langgraph.json文件:

{
  "dependencies": ["./my_agent"],
  "graphs": {
    "agent": "./my_agent/agent.py:graph"
  },
  "env": ".env"
}

请注意,在顶级键graphs下的每个子键值末尾出现了CompiledGraph变量名(即:<variable_name>)。

配置文件位置

LangGraph API配置文件必须放置在与包含编译图和相关依赖项的Python文件处于同一级别或更高级别的目录中。

示例文件目录结构:

my-app/
├── my_agent # 所有项目代码均位于此处
   ├── utils # 图的工具包
      ├── __init__.py
      ├── tools.py # 图的工具函数
      ├── nodes.py # 图的节点函数
      └── state.py # 图的状态定义
   ├── requirements.txt # 包依赖项
   ├── __init__.py
   └── agent.py # 构建图的代码
├── .env # 环境变量
└── langgraph.json # LangGraph配置文件

下一步

在设置好项目并将它放入github仓库后,是时候部署你的应用了。

Comments