应用必须通过配置文件进行配置,才能部署到 LangSmith(或进行自托管)。本操作指南介绍了使用 pyproject.toml 定义包的依赖项来为部署配置应用的基本步骤。
此示例基于此仓库,该仓库使用了 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.
您也可以使用以下方式设置:
requirements.txt:用于依赖管理,请查阅此操作指南了解如何在 LangSmith 中使用 requirements.txt。
- 单体仓库:要部署位于单体仓库内的图,请查看此仓库了解如何操作的示例。
每一步之后,都提供了一个示例文件目录,以展示代码的组织方式。
指定依赖项
依赖项可以选择性地在以下文件之一中指定:pyproject.toml、setup.py 或 requirements.txt。如果未创建这些文件,则可以在后面的配置文件中指定依赖项。
以下依赖项将包含在镜像中,只要使用兼容的版本范围,您也可以在代码中使用它们:
示例 pyproject.toml 文件:
示例文件目录:
指定环境变量
环境变量可以选择性地在文件(例如 .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 的变量名。这些变量名稍后将在创建配置文件时使用。
示例 agent.py 文件,展示了如何从您定义的其他模块导入(模块的代码未在此显示,请参阅此仓库查看其实现):
示例文件目录:
创建配置文件
创建一个名为 langgraph.json 的配置文件。请参阅配置文件参考了解配置文件 JSON 对象中每个键的详细说明。
示例 langgraph.json 文件:
请注意,CompiledGraph 的变量名出现在顶级 graphs 键下每个子键值的末尾(即 :<变量名>)。
配置文件位置
配置文件必须放置在与包含已编译图和相关依赖项的 Python 文件同级或更高级的目录中。
示例文件目录:
后续步骤
在设置好项目并将其放入 GitHub 仓库后,就可以部署您的应用了。