> ## 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.

# 如何使用 requirements.txt 设置应用程序

应用程序必须使用[配置文件](/langsmith/cli#configuration-file)进行配置，才能部署到 LangSmith（或进行自托管）。本操作指南讨论了使用 `requirements.txt` 指定项目依赖项来设置应用程序以进行部署的基本步骤。

此示例基于[此仓库](https://github.com/langchain-ai/langgraph-example)，该仓库使用 LangGraph 框架。

最终的仓库结构将如下所示：

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

<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>

你也可以使用以下方式设置：

* `pyproject.toml`：如果你更喜欢使用 poetry 进行依赖管理，请查看[此操作指南](/langsmith/setup-app-requirements-txt)了解如何在 LangSmith 中使用 `pyproject.toml`。
* 单仓库（monorepo）：如果你有兴趣部署位于单仓库内的图，请查看[此仓库](https://github.com/langchain-ai/langgraph-example-monorepo)了解如何操作的示例。

每个步骤之后，都会提供一个示例文件目录，以演示代码的组织方式。

## 指定依赖项

依赖项可以选择性地在以下文件之一中指定：`pyproject.toml`、`setup.py` 或 `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.6.3
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.78.0,<1.79.0
grpcio-tools>=1.78.0,<1.79.0
grpcio-health-checking>=1.78.0,<1.79.0
opentelemetry-api>=0.0.1
opentelemetry-sdk>=0.0.1
opentelemetry-exporter-otlp-proto-http>=0.0.1
```

示例 `requirements.txt` 文件：

```
langgraph
langchain_anthropic
tavily-python
langchain_community
langchain_openai

```

示例文件目录：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
my-app/
├── my_agent # 所有项目代码都在这里
│   └── requirements.txt # 包依赖项
```

## 指定环境变量

环境变量可以选择性地在文件中指定（例如 `.env`）。请参阅[环境变量参考](/langsmith/env-var)以配置部署的其他变量。

示例 `.env` 文件：

```
MY_ENV_VAR_1=foo
MY_ENV_VAR_2=bar
OPENAI_API_KEY=key
```

示例文件目录：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
my-app/
├── my_agent # 所有项目代码都在这里
│   └── requirements.txt # 包依赖项
└── .env # 环境变量
```

<Tip>
  默认情况下，LangSmith 遵循 `uv`/`pip` 的行为，**不会**安装预发布版本，除非明确允许。如果您想使用预发布版本，有以下选项：

  * 使用 `pyproject.toml`：在 `[tool.uv]` 部分添加 `allow-prereleases = true`。
  * 使用 `requirements.txt` 或 `setup.py`：您必须明确指定每个预发布依赖项，包括传递性依赖。例如，如果您声明 `a==0.0.1a1` 且 `a` 依赖于 `b==0.0.1a1`，那么您还必须在依赖项中明确包含 `b==0.0.1a1`。
</Tip>

## 定义图

实现你的图。图可以在单个文件或多个文件中定义。请注意每个要包含在应用程序中的 [CompiledStateGraph](https://reference.langchain.com/python/langgraph/graph/state/CompiledStateGraph) 的变量名。变量名将在稍后创建 [LangGraph 配置文件](/langsmith/cli#configuration-file)时使用。

示例 `agent.py` 文件，展示了如何从你定义的其他模块导入（此处未显示模块的代码，请参阅[此仓库](https://github.com/langchain-ai/langgraph-example)查看其实现）：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 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 # 导入节点
from my_agent.utils.state import AgentState # 导入状态

# 定义运行时上下文
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()
```

示例文件目录：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
my-app/
├── my_agent # 所有项目代码都在这里
│   ├── utils # 图的实用工具
│   │   ├── __init__.py
│   │   ├── tools.py # 图的工具
│   │   ├── nodes.py # 图的节点函数
│   │   └── state.py # 图的状态定义
│   ├── requirements.txt # 包依赖项
│   ├── __init__.py
│   └── agent.py # 构建图的代码
└── .env # 环境变量
```

## 创建配置文件

创建一个名为 `langgraph.json` 的[配置文件](/langsmith/cli#configuration-file)。有关配置文件 JSON 对象中每个键的详细说明，请参阅[配置文件参考](/langsmith/cli#configuration-file)。

示例 `langgraph.json` 文件：

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "dependencies": ["./my_agent"],
  "graphs": {
    "agent": "./my_agent/agent.py:graph"
  },
  "env": ".env"
}
```

请注意，`CompiledGraph` 的变量名出现在顶级 `graphs` 键的每个子键值的末尾（即 `:<variable_name>`）。

<Warning>
  **配置文件位置**
  配置文件必须放置在与包含编译图及其关联依赖项的 Python 文件同级或更高级别的目录中。
</Warning>

示例文件目录：

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

## 下一步

设置好项目并将其放入 GitHub 仓库后，就该[部署你的应用](/langsmith/deployment-quickstart)了。

***

<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/setup-app-requirements-txt.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
