跳转至

运行本地服务器

本指南向你展示如何在本地运行 LangGraph 应用。

前提条件

在开始之前,请确保你具备以下条件:

1. 安装 LangGraph CLI

# 需要 Python >= 3.11。

pip install --upgrade "langgraph-cli[inmem]"
npx @langchain/langgraph-cli

2. 创建 LangGraph 应用 🌱

new-langgraph-project-python 模板创建一个新应用。此模板演示了一个单节点应用,你可以使用自己的逻辑扩展它。

langgraph new path/to/your/app --template new-langgraph-project-python

其他模板

如果你在使用 langgraph new 时不指定模板,将显示一个交互式菜单,允许你从可用模板列表中选择。

new-langgraph-project-js 模板创建一个新应用。此模板演示了一个单节点应用,你可以使用自己的逻辑扩展它。

npm create langgraph

3. 安装依赖

在新 LangGraph 应用的根目录中,以 edit 模式安装依赖,以便服务器使用你的本地更改:

cd path/to/your/app
pip install -e .
cd path/to/your/app
npm install

4. 创建 .env 文件

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

LANGSMITH_API_KEY=lsv2...

5. 启动 LangGraph 服务器 🚀

在本地启动 LangGraph API 服务器:

langgraph dev
npx @langchain/langgraph-cli dev

示例输出:

>    Ready!
>
>    - API: [http://localhost:2024](http://localhost:2024/)
>
>    - Docs: http://localhost:2024/docs
>
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024

langgraph dev 命令以内存模式启动 LangGraph 服务器。此模式适用于开发和测试目的。对于生产用途,请部署具有持久化存储后端访问权限的 LangGraph 服务器。有关更多信息,请参阅部署选项

6. 在 LangGraph Studio 中测试你的应用

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 参数。

Safari 兼容性

在你的命令中使用 --tunnel 标志创建安全隧道,因为 Safari 在连接到 localhost 服务器时有限制:

langgraph dev --tunnel

7. 测试 API

  1. 安装 LangGraph Python SDK:

    pip install langgraph-sdk
    
  2. 向助手发送消息(无线程运行):

    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())
    
  1. 安装 LangGraph Python SDK:

    pip install langgraph-sdk
    
  2. 向助手发送消息(无线程运行):

    from langgraph_sdk import get_sync_client
    
    client = get_sync_client(url="http://localhost:2024")
    
    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?",
            }],
        },
        stream_mode="messages-tuple",
    ):
        print(f"Receiving new event of type: {chunk.event}...")
        print(chunk.data)
        print("\n\n")
    
curl -s --request POST \
    --url "http://localhost:2024/runs/stream" \
    --header 'Content-Type: application/json' \
    --data "{
        \"assistant_id\": \"agent\",
        \"input\": {
            \"messages\": [
                {
                    \"role\": \"human\",
                    \"content\": \"What is LangGraph?\"
                }
            ]
        },
        \"stream_mode\": \"messages-tuple\"
    }"
  1. 安装 LangGraph JS SDK:

    npm install @langchain/langgraph-sdk
    
  2. 向助手发送消息(无线程运行):

    const { Client } = await import("@langchain/langgraph-sdk");
    
    // only set the apiUrl if you changed the default port when calling langgraph dev
    const client = new Client({ apiUrl: "http://localhost:2024"});
    
    const streamResponse = client.runs.stream(
        null, // Threadless run
        "agent", // Assistant ID
        {
            input: {
                "messages": [
                    { "role": "user", "content": "What is LangGraph?"}
                ]
            },
            streamMode: "messages-tuple",
        }
    );
    
    for await (const chunk of streamResponse) {
        console.log(`Receiving new event of type: ${chunk.event}...`);
        console.log(JSON.stringify(chunk.data));
        console.log("\n\n");
    }
    
curl -s --request POST \
    --url "http://localhost:2024/runs/stream" \
    --header 'Content-Type: application/json' \
    --data "{
        \"assistant_id\": \"agent\",
        \"input\": {
            \"messages\": [
                {
                    \"role\": \"human\",
                    \"content\": \"What is LangGraph?\"
                }
            ]
        },
        \"stream_mode\": \"messages-tuple\"
    }"

下一步

现在你已经在本地运行了 LangGraph 应用,可以通过探索部署和高级功能来进一步深化你的旅程: