Skip to content

快速入门:在LangGraph Cloud上部署

先决条件

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

在 GitHub 上创建代码库

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

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

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

获取 ReAct Agent 模板所需的 API 密钥

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

替代方案:如果您更喜欢一个不需要 API 密钥的框架应用,请使用 New LangGraph Project 模板,而不是 ReAct Agent 模板。

  1. 访问 ReAct Agent 代码库。
  2. 点击右上角的 Fork 按钮,将代码库 fork 到您的 GitHub 账户中。

部署到LangGraph云

1. 登录到LangSmith

登录到LangSmith
访问LangSmith并登录。如果您还没有账户,可以免费注册。

2. 单击LangGraph平台(左侧边栏)

登录到LangSmith
从左侧边栏中选择**LangGraph平台**。

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

登录到LangSmith
单击**+新建部署**以创建新的部署。此按钮位于右上角。 它将打开一个新模态窗口,您可以在其中填写所需的字段。

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

image
单击**从GitHub导入**并按照说明连接您的GitHub帐户。此步骤对于**首次使用用户**或添加之前未连接的私有仓库是必需的。

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

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

  • 仓库:选择您之前分叉的仓库(或任何其他您想部署的仓库)。
  • 设置应用程序所需的密钥和环境变量。对于**ReAct Agent**模板,您需要设置以下密钥:
    • ANTHROPIC_API_KEY:从Anthropic获取API密钥。
    • TAVILY_API_KEY:在Tavily网站上获取API密钥。
6. 单击提交以部署!

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

LangGraph Studio Web UI

一旦您的应用程序部署完成,您就可以在**LangGraph Studio**中进行测试。

1. 点击现有的部署

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

2. 点击LangGraph Studio

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

image

在LangGraph Studio中运行的示例图。

测试API

Note

下面的API调用是针对**ReAct Agent**模板的。如果你部署的是不同的应用,可能需要相应地调整API调用。

在使用之前,你需要获取你的LangGraph部署的URL。你可以在“部署”视图中找到这个URL。点击“URL”将其复制到剪贴板。

你还需要确保你的API密钥设置正确,以便能够使用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,  # 无线程运行
    "agent", # 助手名称。在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,  # 无线程运行
    "agent", # 助手名称。在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, // 无线程运行
    "agent", // 助手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 平台的知识

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

Comments