以编程方式使用文档

应用程序必须配置一个 配置文件 才能部署到 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

您也可以通过以下方式设置:

  • - pyproject.toml:如果您更喜欢使用 poetry 进行依赖管理,请查看 此操作指南 了解如何使用 pyproject.toml 进行 LangSmith 部署。
  • - 单仓库:如果您有兴趣部署位于单仓库内的图,请查看 此仓库 以了解如何操作的示例。

每个步骤后都提供了示例文件目录,以展示代码的组织方式。

指定依赖项

依赖项可以选择在以下任一文件中指定: 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

示例 requirements.txt file:

langgraph
langchain_anthropic
tavily-python
langchain_openai

示例文件目录:

my-app/
├── my_agent # all project code lies within here
│   └── requirements.txt # package dependencies

指定环境变量

环境变量可以选择在文件中指定(例如 .env)。请参阅 环境变量参考 来配置部署的其他变量。

示例 .env file:

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

定义图

实现您的图。图可以在单个文件或多个文件中定义。请注意每个要包含在应用程序中的 @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 file:

{
  "dependencies": ["./my_agent"],
  "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
│   ├── requirements.txt # package dependencies
│   ├── __init__.py
│   └── agent.py # code for constructing your graph
├── .env # environment variables
└── langgraph.json # configuration file for LangGraph

下一步

在您设置好项目并将其放入 GitHub 仓库后,就是 部署您的应用.