跳到内容

如何使用 requirements.txt 设置 LangGraph 应用程序

一个 LangGraph 应用程序必须配置一个 LangGraph 配置文件,才能部署到 LangGraph 平台(或进行自托管)。本操作指南讨论了使用 requirements.txt 指定项目依赖项来设置 LangGraph 应用程序以进行部署的基本步骤。

本演练基于此代码仓库,您可以实际操作以了解更多关于如何设置 LangGraph 应用程序以进行部署的信息。

使用 pyproject.toml 进行设置

如果您倾向于使用 poetry 进行依赖管理,请查看关于为 LangGraph 平台使用 pyproject.toml这篇操作指南

使用 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
   ├── requirements.txt # package dependencies
   ├── __init__.py
   └── agent.py # code for constructing your graph
├── .env # environment variables
└── langgraph.json # configuration file for LangGraph

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

指定依赖项

依赖项可以任选地在以下文件之一中指定:pyproject.tomlsetup.pyrequirements.txt。如果这些文件都未创建,那么依赖项可以在稍后在LangGraph 配置文件中指定。

以下依赖项将包含在镜像中,您也可以在代码中使用它们,只要版本范围兼容即可。

langgraph>=0.3.27
langgraph-sdk>=0.1.66
langgraph-checkpoint>=2.0.23
langchain-core>=0.2.38
langsmith>=0.1.63
orjson>=3.9.7,<3.10.17
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>=24.1.0
cloudpickle>=3.0.0

requirements.txt 文件示例

langgraph
langchain_anthropic
tavily-python
langchain_community
langchain_openai

示例文件目录

my-app/
├── my_agent # all project code lies within here
   └── requirements.txt # package dependencies

指定环境变量

环境变量可以选择在文件中指定(例如.env)。请参阅环境变量参考以配置部署的其他变量。

示例 .env 文件

MY_ENV_VAR_1=foo
MY_ENV_VAR_2=bar
OPENAI_API_KEY=key

示例文件目录

my-app/
├── my_agent # all project code lies within here
   └── requirements.txt # package dependencies
└── .env # environment variables

定义图

实现您的图!图可以在单个文件或多个文件中定义。请记下要包含在 LangGraph 应用程序中的每个 @ 的变量名。这些变量名稍后在创建 LangGraph 配置文件时会用到。

agent.py 文件示例,展示了如何从您定义的其他模块导入(这里未显示模块的代码,请参阅此代码仓库查看其实现)

# 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 runtime context
class GraphContext(TypedDict):
    model_name: Literal["anthropic", "openai"]

workflow = StateGraph(AgentState, context_schema=GraphContext)
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()

示例文件目录

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
   ├── requirements.txt # package dependencies
   ├── __init__.py
   └── agent.py # code for constructing your graph
└── .env # environment variables

创建 LangGraph 配置文件

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

langgraph.json 文件示例

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

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

配置文件位置

LangGraph 配置文件必须放置在与包含已编译图及其相关依赖项的 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
   ├── requirements.txt # package dependencies
   ├── __init__.py
   └── agent.py # code for constructing your graph
├── .env # environment variables
└── langgraph.json # configuration file for LangGraph

下一步

设置好项目并将其放置在 GitHub 存储库中后,是时候部署您的应用程序了。