> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# LangGraph 运行时

[`Pregel`](https://reference.langchain.com/python/langgraph/pregel/main/Pregel) 实现了 LangGraph 的运行时，负责管理 LangGraph 应用程序的执行。

编译一个 [StateGraph](https://reference.langchain.com/python/langgraph/graph/state/StateGraph) 或创建一个 [`@entrypoint`](https://reference.langchain.com/python/langgraph/func/entrypoint) 会产生一个 [`Pregel`](https://reference.langchain.com/python/langgraph/pregel/main/Pregel) 实例，该实例可以通过输入进行调用。

本指南从高层次解释了运行时，并提供了直接使用 Pregel 实现应用程序的说明。

> **注意：** [`Pregel`](https://reference.langchain.com/python/langgraph/pregel/main/Pregel) 运行时以 [Google 的 Pregel 算法](https://research.google/pubs/pub37252/) 命名，该算法描述了一种使用图进行大规模并行计算的高效方法。

## 概述

在 LangGraph 中，Pregel 将 [**执行器**](https://en.wikipedia.org/wiki/Actor_model) 和 **通道** 组合到一个应用程序中。**执行器** 从通道读取数据并向通道写入数据。Pregel 按照 **Pregel 算法**/**批量同步并行** 模型将应用程序的执行组织为多个步骤。

每个步骤包含三个阶段：

* **规划**：确定在此步骤中要执行哪些 **执行器**。例如，在第一步中，选择订阅特殊 **输入** 通道的 **执行器**；在后续步骤中，选择订阅上一步中更新的通道的 **执行器**。
* **执行**：并行执行所有选定的 **执行器**，直到全部完成、某个失败或达到超时。在此阶段，通道更新对执行器不可见，直到下一步。
* **更新**：使用此步骤中 **执行器** 写入的值更新通道。

重复此过程，直到没有 **执行器** 被选中执行，或达到最大步骤数。

## 执行器

一个 **执行器** 是一个 `PregelNode`。它订阅通道，从中读取数据，并向其写入数据。可以将其视为 Pregel 算法中的 **执行器**。`PregelNodes` 实现了 LangChain 的 Runnable 接口。

## 通道

通道用于在执行器（PregelNodes）之间进行通信。每个通道都有一个值类型、一个更新类型和一个更新函数——该函数接收一系列更新并修改存储的值。通道可用于将数据从一个链发送到另一个链，或在未来的步骤中将数据从一个链发送到自身。LangGraph 提供了许多内置通道：

* [`LastValue`](https://reference.langchain.com/python/langgraph/channels/last_value/LastValue)：默认通道，存储发送到通道的最后一个值，适用于输入和输出值，或用于将数据从一个步骤发送到下一个步骤。
* [`Topic`](https://reference.langchain.com/python/langgraph/channels/topic/Topic)：一个可配置的发布-订阅主题，适用于在 **执行器** 之间发送多个值，或用于累积输出。可以配置为去重值或在多个步骤中累积值。
* [`BinaryOperatorAggregate`](https://reference.langchain.com/python/langgraph/channels/binop/BinaryOperatorAggregate)：存储一个持久值，通过对当前值和发送到通道的每个更新应用二元运算符来更新，适用于在多个步骤中计算聚合；例如，`total = BinaryOperatorAggregate(int, operator.add)`

## 示例

虽然大多数用户将通过 [StateGraph](https://reference.langchain.com/python/langgraph/graph/state/StateGraph) API 或 [`@entrypoint`](https://reference.langchain.com/python/langgraph/func/entrypoint) 装饰器与 Pregel 交互，但也可以直接与 Pregel 交互。

以下是几个不同的示例，让您了解 Pregel API。

<Tabs>
  <Tab title="单节点">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.channels import EphemeralValue
    from langgraph.pregel import Pregel, NodeBuilder

    node1 = (
        NodeBuilder().subscribe_only("a")
        .do(lambda x: x + x)
        .write_to("b")
    )

    app = Pregel(
        nodes={"node1": node1},
        channels={
            "a": EphemeralValue(str),
            "b": EphemeralValue(str),
        },
        input_channels=["a"],
        output_channels=["b"],
    )

    app.invoke({"a": "foo"})
    ```

    ```con theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {'b': 'foofoo'}
    ```
  </Tab>

  <Tab title="多节点">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.channels import LastValue, EphemeralValue
    from langgraph.pregel import Pregel, NodeBuilder

    node1 = (
        NodeBuilder().subscribe_only("a")
        .do(lambda x: x + x)
        .write_to("b")
    )

    node2 = (
        NodeBuilder().subscribe_only("b")
        .do(lambda x: x + x)
        .write_to("c")
    )


    app = Pregel(
        nodes={"node1": node1, "node2": node2},
        channels={
            "a": EphemeralValue(str),
            "b": LastValue(str),
            "c": EphemeralValue(str),
        },
        input_channels=["a"],
        output_channels=["b", "c"],
    )

    app.invoke({"a": "foo"})
    ```

    ```con theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {'b': 'foofoo', 'c': 'foofoofoofoo'}
    ```
  </Tab>

  <Tab title="主题">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.channels import EphemeralValue, Topic
    from langgraph.pregel import Pregel, NodeBuilder

    node1 = (
        NodeBuilder().subscribe_only("a")
        .do(lambda x: x + x)
        .write_to("b", "c")
    )

    node2 = (
        NodeBuilder().subscribe_to("b")
        .do(lambda x: x["b"] + x["b"])
        .write_to("c")
    )

    app = Pregel(
        nodes={"node1": node1, "node2": node2},
        channels={
            "a": EphemeralValue(str),
            "b": EphemeralValue(str),
            "c": Topic(str, accumulate=True),
        },
        input_channels=["a"],
        output_channels=["c"],
    )

    app.invoke({"a": "foo"})
    ```

    ```pycon theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {'c': ['foofoo', 'foofoofoofoo']}
    ```
  </Tab>

  <Tab title="二元运算符聚合">
    此示例演示如何使用 [`BinaryOperatorAggregate`](https://reference.langchain.com/python/langgraph/channels/binop/BinaryOperatorAggregate) 通道实现一个归约器。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.channels import EphemeralValue, BinaryOperatorAggregate
    from langgraph.pregel import Pregel, NodeBuilder


    node1 = (
        NodeBuilder().subscribe_only("a")
        .do(lambda x: x + x)
        .write_to("b", "c")
    )

    node2 = (
        NodeBuilder().subscribe_only("b")
        .do(lambda x: x + x)
        .write_to("c")
    )

    def reducer(current, update):
        if current:
            return current + " | " + update
        else:
            return update

    app = Pregel(
        nodes={"node1": node1, "node2": node2},
        channels={
            "a": EphemeralValue(str),
            "b": EphemeralValue(str),
            "c": BinaryOperatorAggregate(str, operator=reducer),
        },
        input_channels=["a"],
        output_channels=["c"],
    )

    app.invoke({"a": "foo"})
    ```
  </Tab>

  <Tab title="循环">
    此示例演示如何通过让一个链写入它订阅的通道来在图中引入循环。执行将持续进行，直到向通道写入一个 `None` 值。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.channels import EphemeralValue
    from langgraph.pregel import Pregel, NodeBuilder, ChannelWriteEntry

    example_node = (
        NodeBuilder().subscribe_only("value")
        .do(lambda x: x + x if len(x) < 10 else None)
        .write_to(ChannelWriteEntry("value", skip_none=True))
    )

    app = Pregel(
        nodes={"example_node": example_node},
        channels={
            "value": EphemeralValue(str),
        },
        input_channels=["value"],
        output_channels=["value"],
    )

    app.invoke({"value": "a"})
    ```

    ```pycon theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {'value': 'aaaaaaaaaaaaaaaa'}
    ```
  </Tab>
</Tabs>

## 高级 API

LangGraph 提供了两个用于创建 Pregel 应用程序的高级 API：[StateGraph（图 API）](/oss/python/langgraph/graph-api) 和 [函数式 API](/oss/python/langgraph/functional-api)。

<Tabs>
  <Tab title="StateGraph（图 API）">
    [StateGraph（图 API）](https://reference.langchain.com/python/langgraph/graph/state/StateGraph) 是一个更高级别的抽象，简化了 Pregel 应用程序的创建。它允许您定义节点和边的图。当您编译图时，StateGraph API 会自动为您创建 Pregel 应用程序。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import TypedDict

    from langgraph.constants import START
    from langgraph.graph import StateGraph

    class Essay(TypedDict):
        topic: str
        content: str | None
        score: float | None

    def write_essay(essay: Essay):
        return {
            "content": f"Essay about {essay['topic']}",
        }

    def score_essay(essay: Essay):
        return {
            "score": 10
        }

    builder = StateGraph(Essay)
    builder.add_node(write_essay)
    builder.add_node(score_essay)
    builder.add_edge(START, "write_essay")
    builder.add_edge("write_essay", "score_essay")

    # 编译图。
    # 这将返回一个 Pregel 实例。
    graph = builder.compile()
    ```

    编译后的 Pregel 实例将与一系列节点和通道相关联。您可以通过打印它们来检查节点和通道。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    print(graph.nodes)
    ```

    您将看到类似这样的内容：

    ```pycon theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {'__start__': <langgraph.pregel.read.PregelNode at 0x7d05e3ba1810>,
     'write_essay': <langgraph.pregel.read.PregelNode at 0x7d05e3ba14d0>,
     'score_essay': <langgraph.pregel.read.PregelNode at 0x7d05e3ba1710>}
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    print(graph.channels)
    ```

    您应该看到类似这样的内容

    ```pycon theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {'topic': <langgraph.channels.last_value.LastValue at 0x7d05e3294d80>,
     'content': <langgraph.channels.last_value.LastValue at 0x7d05e3295040>,
     'score': <langgraph.channels.last_value.LastValue at 0x7d05e3295980>,
     '__start__': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e3297e00>,
     'write_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e32960c0>,
     'score_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d8ab80>,
     'branch:__start__:__self__:write_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e32941c0>,
     'branch:__start__:__self__:score_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d88800>,
     'branch:write_essay:__self__:write_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e3295ec0>,
     'branch:write_essay:__self__:score_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d8ac00>,
     'branch:score_essay:__self__:write_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d89700>,
     'branch:score_essay:__self__:score_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d8b400>,
     'start:write_essay': <langgraph.channels.ephemeral_value.EphemeralValue at 0x7d05e2d8b280>}
    ```
  </Tab>

  <Tab title="函数式 API">
    在 [函数式 API](/oss/python/langgraph/functional-api) 中，您可以使用 [`@entrypoint`](https://reference.langchain.com/python/langgraph/func/entrypoint) 来创建 Pregel 应用程序。`entrypoint` 装饰器允许您定义一个接收输入并返回输出的函数。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import TypedDict

    from langgraph.checkpoint.memory import InMemorySaver
    from langgraph.func import entrypoint

    class Essay(TypedDict):
        topic: str
        content: str | None
        score: float | None


    checkpointer = InMemorySaver()

    @entrypoint(checkpointer=checkpointer)
    def write_essay(essay: Essay):
        return {
            "content": f"Essay about {essay['topic']}",
        }

    print("节点: ")
    print(write_essay.nodes)
    print("通道: ")
    print(write_essay.channels)
    ```

    ```pycon theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    节点:
    {'write_essay': <langgraph.pregel.read.PregelNode object at 0x7d05e2f9aad0>}
    通道:
    {'__start__': <langgraph.channels.ephemeral_value.EphemeralValue object at 0x7d05e2c906c0>, '__end__': <langgraph.channels.last_value.LastValue object at 0x7d05e2c90c40>, '__previous__': <langgraph.channels.last_value.LastValue object at 0x7d05e1007280>}
    ```
  </Tab>
</Tabs>

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\langgraph\pregel.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
