> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 应用结构

要在 LangSmith 上部署，应用程序必须由一个或多个图、一个配置文件 (`langgraph.json`)、一个指定依赖项的文件以及一个可选的指定环境变量的 `.env` 文件组成。

本页说明 LangSmith 应用程序的组织方式以及如何提供部署所需的配置详情。

## 核心概念

要使用 LangSmith 进行部署，请提供以下信息：

1. 一个[配置文件](#configuration-file-concepts) (`langgraph.json`)，用于指定应用程序使用的依赖项、图和环境变量。
2. 实现应用程序逻辑的[图](#graphs)。
3. 一个指定运行应用程序所需[依赖项](#dependencies)的文件。
4. 应用程序运行所需的[环境变量](#environment-variables)。

<Tip>
  **框架无关**

  LangSmith 部署支持部署 [LangGraph](/oss/python/langgraph/overview) *图*。然而，图的\_节点\_实现可以包含任意代码。这意味着任何框架都可以在节点内实现并部署在 LangSmith 部署上。这使您无需使用额外的 LangGraph 开源 API 即可实现核心应用逻辑，同时仍可使用 LangSmith 进行[部署](/langsmith/deployment)、扩展和[可观测性](/langsmith/observability)。更多详情，请参阅[在 LangSmith 部署中使用任何框架](/langsmith/application-structure#use-any-framework-with-langsmith-deployment)。
</Tip>

## 文件结构

以下是 Python 和 JavaScript 应用程序的目录结构示例：

<Tabs>
  <Tab title="Python (requirements.txt)">
    ```plaintext theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    my-app/
    ├── my_agent # 所有项目代码都在这里
    │   ├── utils # 图的实用工具
    │   │   ├── __init__.py
    │   │   ├── tools.py # 图的工具
    │   │   ├── nodes.py # 图的节点函数
    │   │   └── state.py # 图的状态定义
    │   ├── __init__.py
    │   └── agent.py # 构建图的代码
    ├── .env # 环境变量
    ├── requirements.txt # 包依赖项
    └── langgraph.json # LangGraph 的配置文件
    ```
  </Tab>

  <Tab title="Python (pyproject.toml)">
    ```plaintext theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    my-app/
    ├── my_agent # 所有项目代码都在这里
    │   ├── utils # 图的实用工具
    │   │   ├── __init__.py
    │   │   ├── tools.py # 图的工具
    │   │   ├── nodes.py # 图的节点函数
    │   │   └── state.py # 图的状态定义
    │   ├── __init__.py
    │   └── agent.py # 构建图的代码
    ├── .env # 环境变量
    ├── langgraph.json  # LangGraph 的配置文件
    └── pyproject.toml # 项目的依赖项
    ```
  </Tab>

  <Tab title="JS (package.json)">
    ```plaintext theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    my-app/
    ├── src # 所有项目代码都在这里
    │   ├── utils # 图的可选实用工具
    │   │   ├── tools.ts # 图的工具
    │   │   ├── nodes.ts # 图的节点函数
    │   │   └── state.ts # 图的状态定义
    │   └── agent.ts # 构建图的代码
    ├── package.json # 包依赖项
    ├── .env # 环境变量
    └── langgraph.json # LangGraph 的配置文件
    ```
  </Tab>
</Tabs>

<Note>
  应用程序的目录结构可能因所使用的编程语言和包管理器而异。
</Note>

<a id="configuration-file-concepts" />

## 配置文件

`langgraph.json` 文件是一个 JSON 文件，用于指定部署应用程序所需的依赖项、图、环境变量和其他设置。

有关 JSON 文件中所有支持键的详细信息，请参阅 [LangGraph 配置文件参考](/langsmith/cli#configuration-file)。

<Tip>
  [LangGraph CLI](/langsmith/cli) 默认使用当前目录中的配置文件 `langgraph.json`。
</Tip>

### 示例

<Tabs>
  <Tab title="Python">
    * 依赖项涉及一个自定义本地包和 `langchain_openai` 包。
    * 将从文件 `./your_package/your_file.py` 加载一个图，变量为 `agent`。
    * 环境变量从 `.env` 文件加载。

    ```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {
        "dependencies": [
            "langchain_openai",
            "./your_package"
        ],
        "graphs": {
            "my_agent": "./your_package/your_file.py:agent"
        },
        "env": "./.env"
    }
    ```
  </Tab>

  <Tab title="JavaScript">
    * 依赖项将从本地目录中的依赖文件（例如 `package.json`）加载。
    * 将从文件 `./your_package/your_file.js` 加载一个图，函数为 `agent`。
    * 环境变量 `OPENAI_API_KEY` 内联设置。

    ```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {
        "dependencies": [
            "."
        ],
        "graphs": {
            "my_agent": "./your_package/your_file.js:agent"
        },
        "env": {
            "OPENAI_API_KEY": "secret-key"
        }
    }
    ```
  </Tab>
</Tabs>

## 依赖项

应用程序可能依赖于其他 Python 包或 JavaScript 库（取决于编写应用程序的编程语言）。

通常需要指定以下信息才能正确设置依赖项：

1. 目录中指定依赖项的文件（例如 `requirements.txt`、`pyproject.toml` 或 `package.json`）。
2. [配置文件](#configuration-file-concepts)中的 `dependencies` 键，用于指定运行应用程序所需的依赖项。
3. 任何额外的二进制文件或系统库可以使用 [LangGraph 配置文件](#configuration-file-concepts)中的 `dockerfile_lines` 键指定。

## 图

使用[配置文件](#configuration-file-concepts)中的 `graphs` 键来指定部署的应用程序中将可用哪些图。

您可以在配置文件中指定一个或多个图。每个图由一个唯一名称和一个路径标识，该路径指向 (1) 一个已编译的图或 (2) 一个定义图的函数。

### 在 LangSmith 部署中使用任何框架

虽然 LangSmith 部署要求应用程序结构化为 LangGraph 图，但该图中的各个节点可以包含任意代码。这意味着您可以在节点中使用任何框架或库，同时仍然受益于 LangSmith 的部署基础设施。

图结构作为部署接口，但您的核心应用程序逻辑可以使用最适合您需求的任何工具和框架。

要使用 LangSmith 进行部署，您需要：

<Tabs>
  <Tab title="Python">
    1. **一个 LangGraph 图结构**：使用 [`StateGraph`](https://reference.langchain.com/python/langgraph/graph/state/StateGraph) 定义一个图，配合使用 [`add_node`](https://reference.langchain.com/python/langgraph/graph/state/StateGraph/add_node) 和 [`add_edge`](https://reference.langchain.com/python/langgraph/pregel/_draw/add_edge)。
    2. **包含任意逻辑的节点函数**：您的节点函数可以调用任何框架或库。
    3. **一个已编译的图**：[编译](https://reference.langchain.com/python/langgraph/graph/state/StateGraph/compile)该图以创建可部署的应用程序。

    以下示例展示了如何将您现有的应用程序逻辑包装在一个最小的 LangGraph 结构中：

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.graph import StateGraph, START, END
    from typing import TypedDict

    # 您使用任何框架的现有应用程序逻辑
    from app_logic import process_data
    from app_logic import fetch_data

    class State(TypedDict):
        input: str
        result: str

    def my_app_node(state: State) -> State:
        """包含任意框架代码的节点。"""
        # 在这里使用任何框架或库
        raw_data = fetch_data(state["input"])
        processed = process_data(raw_data)
        return {"result": processed}

    # 定义图结构
    graph = StateGraph(State)
    graph.add_node("process", my_app_node)  # 添加包含您逻辑的节点
    graph.add_edge(START, "process")  # 将起点连接到您的节点
    graph.add_edge("process", END)  # 将您的节点连接到终点

    # 编译以进行部署
    app = graph.compile()
    ```
  </Tab>

  <Tab title="JavaScript">
    1. **一个 LangGraph 图结构**：使用 [`StateGraph`](https://reference.langchain.com/javascript/classes/_langchain_langgraph.index.StateGraph.html) 定义一个图，配合使用 [`addNode`](https://reference.langchain.com/javascript/classes/_langchain_langgraph.index.StateGraph.html#addnode) 和 [`addEdge`](https://reference.langchain.com/javascript/classes/_langchain_langgraph.index.StateGraph.html#addedge)。
    2. **包含任意逻辑的节点函数**：您的节点函数可以调用任何框架或库。
    3. **一个已编译的图**：[编译](https://reference.langchain.com/javascript/classes/_langchain_langgraph.index.StateGraph.html#compile)该图以创建可部署的应用程序。

    以下示例展示了如何将您现有的应用程序逻辑包装在一个最小的 LangGraph 结构中：

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { StateGraph, START, END } from "@langchain/langgraph";
    import { Annotation } from "@langchain/langgraph";

    // 您使用任何框架的现有应用程序逻辑
    import { processData } from "./app-logic";
    import { fetchData } from "./app-logic";

    const State = Annotation.Root({
      input: Annotation<string>,
      result: Annotation<string>
    });

    async function myAppNode(state: typeof State.State) {
      // 在这里使用任何框架或库
      const rawData = await fetchData(state.input);
      const processed = await processData(rawData);
      return { result: processed };
    }

    // 定义图结构
    const graph = new StateGraph(State)
      .addNode("process", myAppNode)  // 添加包含您逻辑的节点
      .addEdge(START, "process")  // 将起点连接到您的节点
      .addEdge("process", END);  // 将您的节点连接到终点

    // 编译以进行部署
    export const app = graph.compile();
    ```
  </Tab>
</Tabs>

在此示例中，节点函数（Python 的 `my_app_node` 和 JavaScript 的 `myAppNode`）可以包含对任何框架或库的调用。LangGraph 结构仅提供部署接口和编排层。

## 环境变量

如果您在[本地](/langsmith/local-dev-testing)处理已部署的 LangGraph 应用程序，可以在[配置文件](#configuration-file-concepts)的 `env` 键中配置环境变量。

对于生产部署，您通常需要在部署环境中配置环境变量。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/application-structure.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
