跳到内容

快速入门:在 LangGraph Cloud 上部署

前提条件

在开始之前,请确保您拥有以下各项

在 GitHub 上创建仓库

要将 LangGraph 应用程序部署到 LangGraph Cloud,您的应用程序代码必须位于 GitHub 仓库中。支持公共和私有仓库。

您可以将任何 LangGraph 应用程序 部署到 LangGraph Cloud。

对于本指南,我们将使用预构建的 Python ReAct Agent 模板。

获取 ReAct Agent 模板所需的 API Key

这个 ReAct Agent 应用程序需要来自 AnthropicTavily 的 API key。您可以在各自网站上注册获取这些 API key。

替代方案:如果您更喜欢不需要 API key 的支架应用程序,请使用 New LangGraph Project 模板,而不是 ReAct Agent 模板。

  1. 转到 ReAct Agent 仓库。
  2. 点击右上角的 Fork 按钮,将仓库 Fork 到您的 GitHub 账号。

部署到 LangGraph Cloud

1. 登录 LangSmith

Login to LangSmith
转到 LangSmith 并登录。如果您没有账号,可以免费注册。

2. 点击 LangGraph Platform(左侧边栏)

Login to LangSmith
从左侧边栏选择 LangGraph Platform

3. 点击 + 新建部署(右上角)

Login to LangSmith
点击 + New Deployment 创建新部署。此按钮位于右上角。它将打开一个新模态框,您可以在其中填写所需字段。

4. 点击 从 GitHub 导入(首次用户)

image
点击 Import from GitHub 并按照说明连接您的 GitHub 账号。此步骤适用于首次用户或添加之前未连接的私有仓库。

5. 选择仓库,配置环境变量等

image
选择仓库,添加环境变量和秘密,并设置其他配置选项。

  • 仓库:选择您之前 Fork 的仓库(或您想要部署的任何其他仓库)。
  • 设置应用程序所需的秘密和环境变量。对于 ReAct Agent 模板,您需要设置以下秘密:
6. 点击提交以部署!

image
请注意,此步骤可能需要约 15 分钟才能完成。您可以在部署视图中检查部署状态。点击右上角的提交按钮以部署您的应用程序。

LangGraph Studio Web UI

应用程序部署后,您可以在 LangGraph Studio 中测试它。

1. 点击现有部署

image
点击您刚刚创建的部署以查看更多详细信息。

2. 点击 LangGraph Studio

image
点击 LangGraph Studio 按钮以打开 LangGraph Studio。

image

LangGraph Studio 中的示例图运行。

测试 API

注意

以下 API 调用适用于 ReAct Agent 模板。如果您正在部署不同的应用程序,可能需要相应调整 API 调用。

使用前,您需要获取 LangGraph 部署的 URL。您可以在 部署 视图中找到此信息。点击 URL 可将其复制到剪贴板。

您还需要确保已正确设置 API key,以便使用 LangGraph Cloud 进行身份验证。

export LANGSMITH_API_KEY=...

安装 LangGraph Python SDK

pip install langgraph-sdk

向助手发送消息(无线程运行)

from langgraph_sdk import get_client

client = get_client(url="your-deployment-url", api_key="your-langsmith-api-key")

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?",
        }],
    },
    stream_mode="updates",
):
    print(f"Receiving new event of type: {chunk.event}...")
    print(chunk.data)
    print("\n\n")

安装 LangGraph Python SDK

pip install langgraph-sdk

向助手发送消息(无线程运行)

from langgraph_sdk import get_sync_client

client = get_sync_client(url="your-deployment-url", api_key="your-langsmith-api-key")

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="updates",
):
    print(f"Receiving new event of type: {chunk.event}...")
    print(chunk.data)
    print("\n\n")

安装 LangGraph JS SDK

npm install @langchain/langgraph-sdk

向助手发送消息(无线程运行)

const { Client } = await import("@langchain/langgraph-sdk");

const client = new Client({ apiUrl: "your-deployment-url", apiKey: "your-langsmith-api-key" });

const streamResponse = client.runs.stream(
    null, // Threadless run
    "agent", // Assistant ID
    {
        input: {
            "messages": [
                { "role": "user", "content": "What is LangGraph?"}
            ]
        },
        streamMode: "messages",
    }
);

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 <DEPLOYMENT_URL> \
    --header 'Content-Type: application/json' \
    --data "{
        \"assistant_id\": \"agent\",
        \"input\": {
            \"messages\": [
                {
                    \"role\": \"human\",
                    \"content\": \"What is LangGraph?\"
                }
            ]
        },
        \"stream_mode\": \"updates\"
    }" 

下一步

恭喜!如果您已完成本教程,那么您正走在成为 LangGraph Cloud 专家的路上。这里有一些其他资源可供参考,帮助您走向专业之路。

LangGraph 框架

📚 了解更多关于 LangGraph Platform 的信息

利用这些资源扩展您的知识