应用必须配置一个配置文件才能部署到 LangSmith(或进行自托管)。本操作指南讨论了使用 requirements.txt 指定项目依赖项来为部署设置应用的基本步骤。 此示例基于此代码仓库,该仓库使用了 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.toml、setup.py 或 requirements.txt。如果这些文件都未创建,则依赖项可以稍后在配置文件中指定。 以下依赖项将包含在镜像中,你也可以在代码中使用它们,只要版本兼容:
示例 requirements.txt 文件:
示例文件目录:

指定环境变量

环境变量可以选择性地在文件中指定(例如 .env)。请参阅环境变量参考为部署配置其他变量。 示例 .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 文件,展示了如何从你定义的其他模块导入(模块的代码此处未显示,请参阅此代码仓库查看其实现):
示例文件目录:

创建配置文件

创建一个名为 langgraph.json 的配置文件。有关配置文件 JSON 对象中每个键的详细说明,请参阅配置文件参考。 示例 langgraph.json 文件:
请注意,CompiledGraph 的变量名出现在顶层 graphs 键中每个子键的值末尾(即 :<variable_name>)。
配置文件位置 配置文件必须放置在与包含编译图及相关依赖项的 Python 文件同级或更高层的目录中。
示例文件目录:

下一步

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