跳到主要内容
本指南将向您展示如何本地运行 LangGraph 应用程序。

先决条件

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

1. 安装 LangGraph CLI

# Python >= 3.11 is required.
pip install -U "langgraph-cli[inmem]"

2. 创建 LangGraph 应用 🌱

new-langgraph-project-python 模板创建新应用。此模板演示了一个单节点应用程序,您可以根据自己的逻辑进行扩展。
langgraph new path/to/your/app --template new-langgraph-project-python
其他模板 如果您使用 langgraph new 而不指定模板,系统将显示一个交互式菜单,允许您从可用模板列表中进行选择。

3. 安装依赖项

在您的新 LangGraph 应用程序的根目录中,以 edit 模式安装依赖项,以便服务器使用您的本地更改。
cd path/to/your/app
pip install -e .

4. 创建 .env 文件

您会在新的 LangGraph 应用的根目录中找到一个 .env.example 文件。在新的 LangGraph 应用的根目录中创建一个 .env 文件,并将 .env.example 文件的内容复制到其中,填入必要的 API 密钥。
LANGSMITH_API_KEY=lsv2...

5. 启动 LangGraph 服务器 🚀

在本地启动 LangGraph API 服务器
langgraph dev
示例输出
>    Ready!
>
>    - API: [https://:2024](https://:2024/)
>
>    - Docs: https://:2024/docs
>
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
langgraph dev 命令以内存模式启动 LangGraph Server。此模式适用于开发和测试目的。对于生产使用,请部署可访问持久存储后端的 LangGraph Server。有关更多信息,请参阅托管概述

6. 在 Studio 中测试您的应用程序

Studio 是一个专门的 UI,您可以将其连接到 LangGraph API 服务器,以本地可视化、交互和调试您的应用程序。通过访问 langgraph dev 命令输出中提供的 URL,在 Studio 中测试您的图表。
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
对于在自定义主机/端口上运行的 LangGraph Server,请更新 baseURL 参数。
在命令中使用 --tunnel 标志以创建安全隧道,因为 Safari 在连接到 localhost 服务器时存在限制。
langgraph dev --tunnel

7. 测试 API

  • Python SDK (异步)
  • Python SDK (同步)
  • Rest API
  1. 安装 LangGraph Python SDK
pip install langgraph-sdk
  1. 向助手发送消息(无线程运行)
from langgraph_sdk import get_client
import asyncio

client = get_client(url="https://:2024")

async def main():
    async for chunk in client.runs.stream(
        None,  # Threadless run
        "agent", # Name of assistant. Defined in langgraph.json.
        input={
        "messages": [{
            "role": "human",
            "content": "What is LangGraph?",
            }],
        },
    ):
        print(f"Receiving new event of type: {chunk.event}...")
        print(chunk.data)
        print("\n\n")

asyncio.run(main())

后续步骤

既然您已在本地运行 LangGraph 应用,请通过探索部署和高级功能进一步您的旅程。
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.