以编程方式使用文档

本快速入门指南向您展示如何使用 langgraph deploy command.

前提条件

在开始之前,请确保您已具备:

  uv tool install langgraph-cli
  

1. 创建 LangGraph 应用

从以下 new-langgraph-project-python 模板:

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

2. 设置您的 API 密钥

将您的 LangSmith API 密钥添加到项目根目录的 .env 文件中:

LANGSMITH_API_KEY=lsv2_...

langgraph deploy 命令会自动读取此文件。另外,您也可以内联传递:

LANGSMITH_API_KEY=lsv2_... langgraph deploy

## 3. 部署 直接从 CLI 或通过 UI 进行部署。

Deploy from CLI

从项目目录运行部署命令:

    langgraph deploy
    

这会创建一个默认以项目目录命名的 dev 部署。使用 --name or --deployment-type prod 进行覆盖。

您还可以使用 langgraph deploy list 查看所有部署, langgraph deploy logs 查看运行时日志,以及 langgraph deploy delete 删除部署。详情请参阅 CLI 参考.

Deploy from Studio

要从 Studio 部署:

  1. 启动 本地开发服务器。这将自动打开 Studio,一个交互式代理 IDE。
    langgraph dev
    

2. 点击 deploy button. !从 Studio 部署

4. 在 Studio 中测试

Studio 是一个交互式智能体 IDE,直接连接到您的部署。使用它发送消息、检查每个节点的中间状态、在运行过程中编辑状态,以及从任何先前的检查点重放,无需编写代码。

部署就绪后:

  1. Go to LangSmith 并选择 **部署** 在左侧边栏中。
  2. 选择您的部署以查看其详情。
  3. 点击 **Studio** 在右上角打开 Studio.

5. 测试 API

复制 **API URL** 从部署详情视图,然后使用它来调用您的应用:

Python SDK (Async)

1. 安装 LangGraph Python SDK:

        pip install langgraph-sdk
        

2. 向助手发送消息(无状态运行):

        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")
        

Python SDK (Sync)

1. 安装 LangGraph Python SDK:

        pip install langgraph-sdk
        

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

        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")
        

JavaScript SDK

1. 安装 LangGraph JS SDK:

        npm install @langchain/langgraph-sdk
        

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

        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");
        }
        

Rest API

    curl -s --request POST \
        --url /runs/stream \
        --header 'Content-Type: application/json' \
        --header "X-Api-Key: " \
        --data "{
            \"assistant_id\": \"agent\",
            \"input\": {
                \"messages\": [
                    {
                        \"role\": \"human\",
                        \"content\": \"What is LangGraph?\"
                    }
                ]
            },
            \"stream_mode\": \"updates\"
        }"
    

后续步骤

Assistants

使用不同的模型、提示词或工具为每个助手部署相同的图。

Threads

在多次运行之间保持状态,使您的智能体记住交互之间的上下文。

Runs

启动后台运行以处理长时间运行的任务,并将结果流式返回给您的客户端。