应用程序必须使用 配置文件 进行配置才能部署到 LangSmith(或进行自托管)。本操作指南讨论了使用 pyproject.toml 来定义包依赖项的基本部署设置步骤。
此示例基于 此仓库,它使用 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
│ ├── __init__.py
│ └── agent.py # code for constructing your graph
├── .env # environment variables
├── langgraph.json # configuration file for LangGraph
└── pyproject.toml # dependencies for your project
您也可以通过以下方式设置:
- -
requirements.txt:关于依赖项管理,请查看 此操作指南 了解如何在 LangSmith 中使用requirements.txt。 - - monorepo:要部署位于 monorepo 内的图,请查看 此仓库 以获取相关示例。
每个步骤后都提供了一个示例文件目录,以演示代码的组织方式。
指定依赖项
依赖项可以选择在以下文件中指定: pyproject.toml, setup.py, or requirements.txt。如果未创建这些文件,则可以稍后在 配置文件中.
指定依赖项。以下依赖项将包含在镜像中,您也可以在代码中使用它们,只要版本范围兼容:
langgraph>=0.4.10,<2
langgraph-sdk>=0.3.5
langgraph-checkpoint>=3.0.1,<5
langchain-core>=0.3.66
langsmith>=0.7.31
orjson>=3.9.7
httpx>=0.25.0
tenacity>=8.0.0
uvicorn>=0.26.0
sse-starlette>=2.1.3,<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.80.0,<1.81.0
grpcio-tools>=1.80.0,<1.81.0
grpcio-health-checking>=1.80.0,<1.81.0
opentelemetry-api>=0.0.1
opentelemetry-sdk>=0.0.1
opentelemetry-exporter-otlp-proto-http>=0.0.1
示例 pyproject.toml file:
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "my-agent"
version = "0.0.1"
description = "An excellent agent build for LangSmith."
authors = [
{name = "Assistant", email = "1223+assistant@users.noreply.github.com"}
]
license = {text = "MIT"}
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
"langgraph>=0.6.0",
"langchain-fireworks>=0.1.3"
]
[tool.hatch.build.targets.wheel]
packages = ["my_agent"]
示例文件目录:
my-app/
└── pyproject.toml # Python packages required for your graph
指定环境变量
环境变量可以选择在文件中指定(例如 .env)。请参阅 环境变量参考 以配置部署的其他变量。
示例 .env file:
MY_ENV_VAR_1=foo
MY_ENV_VAR_2=bar
FIREWORKS_API_KEY=key
示例文件目录:
my-app/
├── .env # file with environment variables
└── pyproject.toml
定义图
实现您的图。图可以在单个文件或多个文件中定义。请注意每个要包含在应用程序中的 CompiledStateGraph 的变量名称。这些变量名称将在后续创建 配置文件.
时使用。 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
│ ├── __init__.py
│ └── agent.py # code for constructing your graph
├── .env
└── pyproject.toml
创建配置文件
创建一个名为 的 配置文件 langgraph.json。请参阅 配置文件参考 以获取配置文件中 JSON 对象每个键的详细说明。
示例 langgraph.json file:
{
"dependencies": ["."],
"graphs": {
"agent": "./my_agent/agent.py:graph"
},
"env": ".env"
}
请注意,变量名称 CompiledGraph 出现在顶层 graphs 键的每个子键值的末尾(即 :<variable_name>).
示例文件目录:
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
│ ├── __init__.py
│ └── agent.py # code for constructing your graph
├── .env # environment variables
├── langgraph.json # configuration file for LangGraph
└── pyproject.toml # dependencies for your project
下一步
在您设置好项目并将其放置在 GitHub 仓库后,就该 部署您的应用了.