跳到主要内容
应用程序必须使用配置文件进行配置,才能部署到 LangSmith(或进行自托管)。本操作指南讨论了使用requirements.txt指定项目依赖项来设置应用程序进行部署的基本步骤。 此示例基于此仓库,该仓库使用 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 your 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
LangSmith 部署支持部署 LangGraph 。然而,图的节点的实现可以包含任意 Python 代码。这意味着任何框架都可以在节点中实现并部署在 LangSmith 部署上。这让您可以将核心应用程序逻辑保留在 LangGraph 之外,同时仍然使用 LangSmith 进行部署、扩展和可观察性
您还可以设置
  • pyproject.toml:如果您更喜欢使用 poetry 进行依赖项管理,请查看此操作指南,了解如何在 LangSmith 中使用pyproject.toml
  • monorepo:如果您有兴趣部署位于 monorepo 中的图,请查看此仓库以获取如何执行此操作的示例。
每一步之后,都会提供一个示例文件目录,以演示代码如何组织。

指定依赖项

依赖项可以选择在以下文件之一中指定:pyproject.tomlsetup.pyrequirements.txt。如果未创建这些文件,则可以在稍后的配置文件中指定依赖项。 下面的依赖项将包含在镜像中,您也可以在代码中使用它们,只要版本范围兼容:
langgraph>=0.6.0
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
默认情况下,LangSmith 遵循 uv/pip 的行为,即除非明确允许,否则安装预发布版本。如果您想使用预发布版本,您有以下选项
  • 使用 pyproject.toml:在您的 [tool.uv] 部分添加 allow-prereleases = true
  • 使用 requirements.txtsetup.py:您必须明确指定每个预发布依赖项,包括传递依赖项。例如,如果您声明 a==0.0.1a1 并且 a 依赖于 b==0.0.1a1,那么您还必须在依赖项中明确包含 b==0.0.1a1

定义图

实现您的图。图可以定义在单个文件或多个文件中。记下要包含在应用程序中的每个 CompiledStateGraph 的变量名。变量名将在稍后创建 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 your 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配置文件。有关配置文件 JSON 对象中每个键的详细说明,请参阅配置文件参考 langgraph.json文件示例:
{
  "dependencies": ["./my_agent"],
  "graphs": {
    "agent": "./my_agent/agent.py:graph"
  },
  "env": ".env"
}
请注意,CompiledGraph 的变量名出现在顶级 graphs 键中每个子键的值的末尾(即 :<变量名>)。
配置文件位置 配置文件必须放在与包含编译图和相关依赖项的 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 your 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 仓库后,就可以部署您的应用程序了。
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.