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

# 运行本地服务器

<警告>
  **Alpha 版本提示：** 本文档涵盖的是 **v1-alpha** 版本。内容尚不完整，且可能随时更改。

  如需查阅最新稳定版本，请参阅当前的 [LangGraph Python 文档](https://langchain-ai.github.io/langgraph/) 或 [LangGraph JavaScript 文档](https://langchain-ai.github.io/langgraphjs/)。
</警告>

本指南将向您展示如何在本地运行 LangGraph 应用程序。

## 前提条件

在开始之前，请确保您已具备以下条件：

* 一个 [LangSmith](https://smith.langchain.com/settings) 的 API 密钥 — 注册免费

## 1. 安装 LangGraph CLI

<CodeGroup>
  ```bash pip theme={null}
  # 需要 Python >= 3.11。
  pip install -U "langgraph-cli[inmem]"
  ```

  ```bash uv theme={null}
  # 需要 Python >= 3.11。
  uv add langgraph-cli[inmem]
  ```
</CodeGroup>

## 2. 创建一个 LangGraph 应用 🌱

从 [`new-langgraph-project-python` 模板](https://github.com/langchain-ai/new-langgraph-project) 创建一个新应用。该模板展示了一个单节点应用程序，您可以在此基础上扩展自己的逻辑。

```shell theme={null}
langgraph new path/to/your/app --template new-langgraph-project-python
```

<Tip>
  **更多模板**
  如果您使用 `langgraph new` 但未指定模板，系统将显示一个交互式菜单，供您从可用模板列表中选择。
</Tip>

## 3. 安装依赖项

在您的新 LangGraph 应用根目录中，以“编辑”模式安装依赖项，以便服务器使用您的本地更改：

<CodeGroup>
  ```bash pip theme={null}
  cd path/to/your/app
  pip install -e .
  ```

  ```bash uv theme={null}
  cd path/to/your/app
  uv add .
  ```
</CodeGroup>

## 4. 创建 `.env` 文件

您将在新 LangGraph 应用的根目录中找到一个 `.env.example` 文件。请在根目录中创建一个 `.env` 文件，并将 `.env.example` 文件的内容复制进去，填写必要的 API 密钥：

```bash theme={null}
LANGSMITH_API_KEY=lsv2...
```

## 5. 启动 LangGraph 服务器 🚀

在本地启动 LangGraph API 服务器：

```shell theme={null}
langgraph dev
```

示例输出：

```
>    准备就绪！
>
>    - API: [http://localhost:2024](http://localhost:2024/)
>
>    - 文档: http://localhost:2024/docs
>
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
```

`langgraph dev` 命令以内存模式启动 LangGraph 服务器。此模式适用于开发和测试目的。如需生产环境使用，请部署支持持久化存储后端的 LangGraph 服务器。更多信息请参阅 [部署选项](/langgraph-platform/deployment-options)。

## 6. 在 LangGraph Studio 中测试您的应用

[LangGraph Studio](/langgraph-platform/langgraph-studio) 是一个专用 UI，您可以将其连接到 LangGraph API 服务器，以在本地可视化、交互和调试您的应用。通过访问 `langgraph dev` 命令输出中提供的 URL，在 LangGraph Studio 中测试您的图：

```
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
```

对于运行在自定义主机/端口上的 LangGraph 服务器，请更新 baseURL 参数。

<Accordion title="Safari 兼容性">
  由于 Safari 在连接 localhost 服务器时存在限制，请使用 `--tunnel` 标志创建安全隧道：

  ```shell theme={null}
  langgraph dev --tunnel
  ```
</Accordion>

## 7. 测试 API

<Tabs>
  <Tab title="Python SDK (异步)">
    1. 安装 LangGraph Python SDK：

    ```shell theme={null}
    pip install langgraph-sdk
    ```

    2. 向助手发送消息（无会话运行）：

    ```python theme={null}
    from langgraph_sdk import get_client
    import asyncio

    client = get_client(url="http://localhost:2024")

    async def main():
        async for chunk in client.runs.stream(
            None,  # 无会话运行
            "agent", # 助手名称。在 langgraph.json 中定义。
            input={
            "messages": [{
                "role": "human",
                "content": "什么是 LangGraph？",
                }],
            },
        ):
            print(f"正在接收类型为 {chunk.event} 的新事件...")
            print(chunk.data)
            print("\n\n")

    asyncio.run(main())
    ```
  </Tab>

  <Tab title="Python SDK (同步)">
    1. 安装 LangGraph Python SDK：

    ```shell theme={null}
    pip install langgraph-sdk
    ```

    2. 向助手发送消息（无会话运行）：

    ```python theme={null}
    from langgraph_sdk import get_sync_client

    client = get_sync_client(url="http://localhost:2024")

    for chunk in client.runs.stream(
        None,  # 无会话运行
        "agent", # 助手名称。在 langgraph.json 中定义。
        input={
            "messages": [{
                "role": "human",
                "content": "什么是 LangGraph？",
            }],
        },
        stream_mode="messages-tuple",
    ):
        print(f"正在接收类型为 {chunk.event} 的新事件...")
        print(chunk.data)
        print("\n\n")
    ```
  </Tab>

  <Tab title="Rest API">
    ```bash theme={null}
    curl -s --request POST \
        --url "http://localhost:2024/runs/stream" \
        --header 'Content-Type: application/json' \
        --data "{
            \"assistant_id\": \"agent\",
            \"input\": {
                \"messages\": [
                    {
                        \"role\": \"human\",
                        \"content\": \"什么是 LangGraph?\"
                    }
                ]
            },
            \"stream_mode\": \"messages-tuple\"
        }"
    ```
  </Tab>
</Tabs>

## 后续步骤

现在您已在本地成功运行 LangGraph 应用，可通过探索部署和高级功能进一步深入：

* [部署快速入门](/langgraph-platform/deployment-quickstart)：使用 LangGraph 平台部署您的 LangGraph 应用。

* [LangGraph 平台](/langgraph-platform)：了解 LangGraph 平台的基础概念。

* [Python SDK 参考文档](https://langchain-ai.github.io/langgraph/cloud/reference/sdk/python_sdk_ref/)：探索 Python SDK API 参考文档。
