本快速入门向您展示如何在本地设置 LangGraph 应用程序以进行测试和开发。

先决条件

在开始之前,请确保您有 LangSmith 的 API 密钥(免费注册)。

1. 安装 LangGraph CLI

  • Python server
  • Node server
# Python >= 3.11 is required.

pip install -U "langgraph-cli[inmem]"

2. 创建 LangGraph 应用 🌱

new-langgraph-project-python 模板new-langgraph-project-js 模板创建新应用。此模板演示了一个单节点应用程序,您可以使用自己的逻辑扩展它。
  • Python server
  • Node server
langgraph new path/to/your/app --template new-langgraph-project-python
Additional templates
If you use langgraph new without specifying a template, you will be presented with an interactive menu that will allow you to choose from a list of available templates.

3. 安装依赖

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

4. 创建 .env 文件

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

5. Launch Agent Server 🚀

Start the Agent Server locally:
  • Python server
  • Node server
langgraph dev
Sample output:
>    Ready!
>
>    - API: [http://localhost:2024](http://localhost:2024/)
>
>    - Docs: http://localhost:2024/docs
>
>    - Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
langgraph dev 命令以内存模式启动 Agent Server。此模式适用于开发和测试目的。
对于生产使用,请使用持久存储后端部署 Agent Server。有关更多信息,请参阅 LangSmith 平台选项

6. 测试 API

  • Python SDK (async)
  • Python SDK (sync)
  • Javascript SDK
  • Rest API
  1. 安装 LangGraph Python SDK:
pip install langgraph-sdk
  1. 向助手发送消息(无线程运行):
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,  # 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 应用,可以准备部署它了: 选择 LangSmith 的托管选项:
  • Cloud:最快的设置,完全托管(推荐)。
  • Hybrid在您的云中,由 LangChain 管理。
  • Self-hosted:在您的基础设施中完全控制。
有关更多详细信息,请参阅平台设置比较 然后部署您的应用: 探索功能:
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.