Skip to main content
要将应用程序部署到 LangSmith(或进行自托管),必须使用配置文件进行配置。本操作指南讨论了使用 requirements.txt 指定项目依赖项来设置应用程序进行部署的基本步骤。 此示例基于此仓库,该仓库使用了 LangGraph 框架。 最终的仓库结构将类似于:
my-app/
├── my_agent # 所有项目代码位于此处
   ├── utils # 图的实用工具
   ├── __init__.py
   ├── tools.py # 图的工具
   ├── nodes.py # 图的节点函数
   └── state.py # 图的状态定义
   ├── requirements.txt # 包依赖项
   ├── __init__.py
   └── agent.py # 构建图的代码
├── .env # 环境变量
└── langgraph.json # LangGraph 配置文件
LangSmith Deployment supports deploying a LangGraph graph. However, the implementation of a node of a graph can contain arbitrary code. This means any framework can be implemented within a node and deployed on LangSmith Deployment. This lets you implement your core application logic without using additional LangGraph OSS APIs while still using LangSmith for deployment, scaling, and observability. For more details, refer to Use any framework with LangSmith Deployment.
你也可以通过以下方式设置:
  • pyproject.toml:如果你更喜欢使用 poetry 进行依赖管理,请查看此操作指南,了解如何在 LangSmith 中使用 pyproject.toml
  • 单体仓库:如果你有兴趣部署位于单体仓库内的图,请查看此仓库以获取示例。
每个步骤后,都会提供一个示例文件目录,以演示代码的组织方式。

指定依赖项

依赖项可以选择在以下文件之一中指定:pyproject.tomlsetup.pyrequirements.txt。如果未创建这些文件,则可以在稍后的配置文件中指定依赖项。 以下依赖项将包含在镜像中,你也可以在代码中使用它们,只要版本范围兼容即可:
langgraph>=0.4.10,<2
langgraph-sdk>=0.3.5
langgraph-checkpoint>=3.0.1,<5
langchain-core>=0.3.66
langsmith>=0.6.3
orjson>=3.9.7
httpx>=0.25.0
tenacity>=8.0.0
uvicorn>=0.26.0
sse-starlette>=2.1.0,<3.4.0
uvloop>=0.18.0
httptools>=0.5.0
jsonschema-rs>=0.20.0
structlog>=24.1.0
cloudpickle>=3.0.0
truststore>=0.1
protobuf>=6.32.1,<7.0.0
grpcio>=1.78.0,<1.79.0
grpcio-tools>=1.78.0,<1.79.0
grpcio-health-checking>=1.78.0,<1.79.0
opentelemetry-api>=0.0.1
opentelemetry-sdk>=0.0.1
opentelemetry-exporter-otlp-proto-http>=0.0.1
示例 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 # 环境变量
By default, LangSmith follows the uv/pip behavior of not installing prerelease versions unless explicitly allowed. If want to use prereleases, you have the following options:
  • With pyproject.toml: add allow-prereleases = true to your [tool.uv] section.
  • With requirements.txt or setup.py: you must explicitly specify every prerelease dependency, including transitive ones. For example, if you declare a==0.0.1a1 and a depends on b==0.0.1a1, then you must also explicitly include b==0.0.1a1 in your dependencies.

定义图

实现你的图。图可以在单个文件或多个文件中定义。请注意每个要包含在应用程序中的 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 # 导入节点
from my_agent.utils.state import AgentState # 导入状态

# 定义运行时上下文
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 # 所有项目代码位于此处
   ├── utils # 图的实用工具
   ├── __init__.py
   ├── tools.py # 图的工具
   ├── nodes.py # 图的节点函数
   └── state.py # 图的状态定义
│   ├── requirements.txt # 包依赖项
│   ├── __init__.py
│   └── agent.py # 构建图的代码
└── .env # 环境变量

创建配置文件

创建一个名为 langgraph.json配置文件。有关配置文件中 JSON 对象每个键的详细说明,请参阅配置文件参考 示例 langgraph.json 文件:
{
  "dependencies": ["./my_agent"],
  "graphs": {
    "agent": "./my_agent/agent.py:graph"
  },
  "env": ".env"
}
请注意,CompiledGraph 的变量名出现在顶级 graphs 键的每个子键值的末尾(即 :<变量名>)。
配置文件位置 配置文件必须放置在与包含已编译图及相关依赖项的 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 仓库后,就可以部署你的应用程序了。