跳到内容

Pregel

Pregel

基础:PregelProtocol

Pregel 管理 LangGraph 应用程序的运行时行为。

概述

Pregel 将 actor通道 结合到一个应用程序中。Actor 从通道读取数据并向通道写入数据。Pregel 按照 Pregel 算法/Bulk Synchronous Parallel (BSP) 模型,将应用程序的执行组织成多个步骤。

每个步骤包含三个阶段

  • 计划:确定在此步骤中要执行哪些 actor。例如,在第一步中,选择订阅特殊 输入 通道的 actor;在后续步骤中,选择订阅前一步骤中更新的通道的 actor
  • 执行:并行执行所有选定的 actor,直到全部完成、其中一个失败或达到超时。在此阶段,通道更新对于 actor 是不可见的,直到下一个步骤。
  • 更新:使用此步骤中 actor 写入的值更新通道。

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

Actor

一个 actor 是一个 PregelNode。它订阅通道,从通道读取数据,并向通道写入数据。它可以被认为是 Pregel 算法中的一个 actorPregelNodes 实现了 LangChain 的 Runnable 接口。

通道

通道用于 actor (PregelNodes) 之间的通信。每个通道都有一个值类型、一个更新类型和一个更新函数——该函数接收一系列更新并修改存储的值。通道可用于将数据从一个链发送到另一个链,或将数据从一个链发送到自身以供未来步骤使用。LangGraph 提供了许多内置通道

基本通道:LastValue 和 Topic
  • LastValue:默认通道,存储发送到通道的最后一个值,适用于输入和输出值,或用于将数据从一步发送到下一步
  • Topic:一个可配置的 PubSub Topic,适用于在 actor 之间发送多个值,或用于累积输出。可以配置为对值进行去重,和/或在多个步骤过程中累积值。
高级通道:Context 和 BinaryOperatorAggregate
  • Context:暴露上下文管理器的值,管理其生命周期。适用于需要设置和/或拆解的外部资源的访问。例如:client = Context(httpx.Client)
  • BinaryOperatorAggregate:存储一个持久化值,通过将二元运算符应用于当前值和发送到通道的每个更新来更新,适用于计算多个步骤的聚合。例如:total = BinaryOperatorAggregate(int, operator.add)

示例

大多数用户将通过 StateGraph(图 API) 或通过 entrypoint(函数式 API)与 Pregel 交互。

但是,对于高级用例,可以直接使用 Pregel。如果您不确定是否需要直接使用 Pregel,那么答案可能是否定的——您应该改用图 API 或函数式 API。这些是更高级别的接口,它们将在底层编译为 Pregel。

这里有一些示例,让您了解它是如何工作的

单节点应用程序
from langgraph.channels import EphemeralValue
from langgraph.pregel import Pregel, Channel, ChannelWriteEntry

node1 = (
    Channel.subscribe_to("a")
    | (lambda x: x + x)
    | Channel.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"})
{'b': 'foofoo'}
使用多个节点和多个输出通道
from langgraph.channels import LastValue, EphemeralValue
from langgraph.pregel import Pregel, Channel, ChannelWriteEntry

node1 = (
    Channel.subscribe_to("a")
    | (lambda x: x + x)
    | Channel.write_to("b")
)

node2 = (
    Channel.subscribe_to("b")
    | (lambda x: x + x)
    | Channel.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"})
{'b': 'foofoo', 'c': 'foofoofoofoo'}
使用 Topic 通道
from langgraph.channels import LastValue, EphemeralValue, Topic
from langgraph.pregel import Pregel, Channel, ChannelWriteEntry

node1 = (
    Channel.subscribe_to("a")
    | (lambda x: x + x)
    | {
        "b": Channel.write_to("b"),
        "c": Channel.write_to("c")
    }
)

node2 = (
    Channel.subscribe_to("b")
    | (lambda x: x + x)
    | {
        "c": Channel.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"})
{'c': ['foofoo', 'foofoofoofoo']}
使用 BinaryOperatorAggregate 通道
from langgraph.channels import EphemeralValue, BinaryOperatorAggregate
from langgraph.pregel import Pregel, Channel


node1 = (
    Channel.subscribe_to("a")
    | (lambda x: x + x)
    | {
        "b": Channel.write_to("b"),
        "c": Channel.write_to("c")
    }
)

node2 = (
    Channel.subscribe_to("b")
    | (lambda x: x + x)
    | {
        "c": Channel.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"})
{'c': 'foofoo | foofoofoofoo'}
引入循环

此示例演示了如何在图中引入循环,通过让链写入其订阅的通道来实现。执行将继续,直到向通道写入 None 值。

from langgraph.channels import EphemeralValue
from langgraph.pregel import Pregel, Channel, ChannelWrite, ChannelWriteEntry

example_node = (
    Channel.subscribe_to("value")
    | (lambda x: x + x if len(x) < 10 else None)
    | ChannelWrite(writes=[ChannelWriteEntry(channel="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"})
{'value': 'aaaaaaaaaaaaaaaa'}

方法

名称 描述
stream

为单个输入流式传输图步骤。

astream

为单个输入异步流式传输图步骤。

invoke

使用单个输入和配置运行图。

ainvoke

在单个输入上异步调用图。

get_state

获取图的当前状态。

aget_state

获取图的当前状态。

get_state_history

获取图的状态历史。

aget_state_history

异步获取图的状态历史。

update_state

使用给定值更新图的状态,就如同它们来自

aupdate_state

使用给定值异步更新图的状态,就如同它们来自

bulk_update_state

批量应用更新到图状态。需要设置 checkpointer。

abulk_update_state

异步批量应用更新到图状态。需要设置 checkpointer。

get_graph

返回计算图的可绘制表示。

aget_graph

返回计算图的可绘制表示。

get_subgraphs

获取图的子图。

aget_subgraphs

获取图的子图。

with_config

使用更新的配置创建一个 Pregel 对象的副本。

stream

stream(
    input: dict[str, Any] | Any,
    config: RunnableConfig | None = None,
    *,
    stream_mode: (
        StreamMode | list[StreamMode] | None
    ) = None,
    output_keys: str | Sequence[str] | None = None,
    interrupt_before: All | Sequence[str] | None = None,
    interrupt_after: All | Sequence[str] | None = None,
    checkpoint_during: bool | None = None,
    debug: bool | None = None,
    subgraphs: bool = False
) -> Iterator[dict[str, Any] | Any]

为单个输入流式传输图步骤。

参数

名称 类型 描述 默认值
input dict[str, Any] | Any

图的输入。

必需
config RunnableConfig | None

运行使用的配置。

None
stream_mode StreamMode | list[StreamMode] | None

输出流式传输模式,默认为 self.stream_mode。选项包括

  • "values":在每个步骤后发出状态中的所有值,包括中断。与函数式 API 一起使用时,值在工作流结束时一次性发出。
  • "updates":在每个步骤后仅发出节点或任务名称以及节点或任务返回的更新。如果在同一步骤中进行了多次更新(例如,运行了多个节点),则这些更新将单独发出。
  • "custom":使用 StreamWriter 从节点或任务内部发出自定义数据。
  • "messages":逐个 token 发出 LLM 消息,以及节点或任务内部任何 LLM 调用相关的元数据。
  • "debug":为每个步骤发出包含尽可能多信息的调试事件。
None
output_keys str | Sequence[str] | None

要流式传输的键,默认为所有非上下文通道。

None
interrupt_before All | Sequence[str] | None

在中断前停止的节点,默认为图中的所有节点。

None
interrupt_after All | Sequence[str] | None

在中断后停止的节点,默认为图中的所有节点。

None
checkpoint_during bool | None

是否检查中间步骤,默认为 True。如果为 False,则仅保存最终检查点。

None
debug bool | None

执行期间是否打印调试信息,默认为 False。

None
subgraphs bool

是否流式传输子图,默认为 False。

False

产生

类型 描述
dict[str, Any] | Any

图中每个步骤的输出。输出形状取决于 stream_mode。

使用 stream_mode="values"
import operator
from typing_extensions import Annotated, TypedDict
from langgraph.graph import StateGraph, START

class State(TypedDict):
    alist: Annotated[list, operator.add]
    another_list: Annotated[list, operator.add]

builder = StateGraph(State)
builder.add_node("a", lambda _state: {"another_list": ["hi"]})
builder.add_node("b", lambda _state: {"alist": ["there"]})
builder.add_edge("a", "b")
builder.add_edge(START, "a")
graph = builder.compile()

for event in graph.stream({"alist": ['Ex for stream_mode="values"']}, stream_mode="values"):
    print(event)

# {'alist': ['Ex for stream_mode="values"'], 'another_list': []}
# {'alist': ['Ex for stream_mode="values"'], 'another_list': ['hi']}
# {'alist': ['Ex for stream_mode="values"', 'there'], 'another_list': ['hi']}
使用 stream_mode="updates"
for event in graph.stream({"alist": ['Ex for stream_mode="updates"']}, stream_mode="updates"):
    print(event)

# {'a': {'another_list': ['hi']}}
# {'b': {'alist': ['there']}}
使用 stream_mode="debug"
for event in graph.stream({"alist": ['Ex for stream_mode="debug"']}, stream_mode="debug"):
    print(event)

# {'type': 'task', 'timestamp': '2024-06-23T...+00:00', 'step': 1, 'payload': {'id': '...', 'name': 'a', 'input': {'alist': ['Ex for stream_mode="debug"'], 'another_list': []}, 'triggers': ['start:a']}}
# {'type': 'task_result', 'timestamp': '2024-06-23T...+00:00', 'step': 1, 'payload': {'id': '...', 'name': 'a', 'result': [('another_list', ['hi'])]}}
# {'type': 'task', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'input': {'alist': ['Ex for stream_mode="debug"'], 'another_list': ['hi']}, 'triggers': ['a']}}
# {'type': 'task_result', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'result': [('alist', ['there'])]}}
使用 stream_mode="custom"
from langgraph.types import StreamWriter

def node_a(state: State, writer: StreamWriter):
    writer({"custom_data": "foo"})
    return {"alist": ["hi"]}

builder = StateGraph(State)
builder.add_node("a", node_a)
builder.add_edge(START, "a")
graph = builder.compile()

for event in graph.stream({"alist": ['Ex for stream_mode="custom"']}, stream_mode="custom"):
    print(event)

# {'custom_data': 'foo'}
使用 stream_mode="messages"
from typing_extensions import Annotated, TypedDict
from langgraph.graph import StateGraph, START
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

class State(TypedDict):
    question: str
    answer: str

def node_a(state: State):
    response = llm.invoke(state["question"])
    return {"answer": response.content}

builder = StateGraph(State)
builder.add_node("a", node_a)
builder.add_edge(START, "a")
graph = builder.compile()

for event in graph.stream({"question": "What is the capital of France?"}, stream_mode="messages"):
    print(event)

# (AIMessageChunk(content='The', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], 'langgraph_path': ('__pregel_pull', 'a'), 'langgraph_checkpoint_ns': '...', 'checkpoint_ns': '...', 'ls_provider': 'openai', 'ls_model_name': 'gpt-4o-mini', 'ls_model_type': 'chat', 'ls_temperature': 0.7})
# (AIMessageChunk(content=' capital', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], ...})
# (AIMessageChunk(content=' of', additional_kwargs={}, response_metadata={}, id='...'), {...})
# (AIMessageChunk(content=' France', additional_kwargs={}, response_metadata={}, id='...'), {...})
# (AIMessageChunk(content=' is', additional_kwargs={}, response_metadata={}, id='...'), {...})
# (AIMessageChunk(content=' Paris', additional_kwargs={}, response_metadata={}, id='...'), {...})

astream async

astream(
    input: dict[str, Any] | Any,
    config: RunnableConfig | None = None,
    *,
    stream_mode: (
        StreamMode | list[StreamMode] | None
    ) = None,
    output_keys: str | Sequence[str] | None = None,
    interrupt_before: All | Sequence[str] | None = None,
    interrupt_after: All | Sequence[str] | None = None,
    checkpoint_during: bool | None = None,
    debug: bool | None = None,
    subgraphs: bool = False
) -> AsyncIterator[dict[str, Any] | Any]

为单个输入异步流式传输图步骤。

参数

名称 类型 描述 默认值
input dict[str, Any] | Any

图的输入。

必需
config RunnableConfig | None

运行使用的配置。

None
stream_mode StreamMode | list[StreamMode] | None

输出流式传输模式,默认为 self.stream_mode。选项包括

  • "values":在每个步骤后发出状态中的所有值,包括中断。与函数式 API 一起使用时,值在工作流结束时一次性发出。
  • "updates":在每个步骤后仅发出节点或任务名称以及节点或任务返回的更新。如果在同一步骤中进行了多次更新(例如,运行了多个节点),则这些更新将单独发出。
  • "custom":使用 StreamWriter 从节点或任务内部发出自定义数据。
  • "messages":逐个 token 发出 LLM 消息,以及节点或任务内部任何 LLM 调用相关的元数据。
  • "debug":为每个步骤发出包含尽可能多信息的调试事件。
None
output_keys str | Sequence[str] | None

要流式传输的键,默认为所有非上下文通道。

None
interrupt_before All | Sequence[str] | None

在中断前停止的节点,默认为图中的所有节点。

None
interrupt_after All | Sequence[str] | None

在中断后停止的节点,默认为图中的所有节点。

None
checkpoint_during bool | None

是否检查中间步骤,默认为 True。如果为 False,则仅保存最终检查点。

None
debug bool | None

执行期间是否打印调试信息,默认为 False。

None
subgraphs bool

是否流式传输子图,默认为 False。

False

产生

类型 描述
AsyncIterator[dict[str, Any] | Any]]

图中每个步骤的输出。输出形状取决于 stream_mode。

使用 stream_mode="values"
import operator
from typing_extensions import Annotated, TypedDict
from langgraph.graph import StateGraph, START

class State(TypedDict):
    alist: Annotated[list, operator.add]
    another_list: Annotated[list, operator.add]

builder = StateGraph(State)
builder.add_node("a", lambda _state: {"another_list": ["hi"]})
builder.add_node("b", lambda _state: {"alist": ["there"]})
builder.add_edge("a", "b")
builder.add_edge(START, "a")
graph = builder.compile()

async for event in graph.astream({"alist": ['Ex for stream_mode="values"']}, stream_mode="values"):
    print(event)

# {'alist': ['Ex for stream_mode="values"'], 'another_list': []}
# {'alist': ['Ex for stream_mode="values"'], 'another_list': ['hi']}
# {'alist': ['Ex for stream_mode="values"', 'there'], 'another_list': ['hi']}
使用 stream_mode="updates"
async for event in graph.astream({"alist": ['Ex for stream_mode="updates"']}, stream_mode="updates"):
    print(event)

# {'a': {'another_list': ['hi']}}
# {'b': {'alist': ['there']}}
使用 stream_mode="debug"
async for event in graph.astream({"alist": ['Ex for stream_mode="debug"']}, stream_mode="debug"):
    print(event)

# {'type': 'task', 'timestamp': '2024-06-23T...+00:00', 'step': 1, 'payload': {'id': '...', 'name': 'a', 'input': {'alist': ['Ex for stream_mode="debug"'], 'another_list': []}, 'triggers': ['start:a']}}
# {'type': 'task_result', 'timestamp': '2024-06-23T...+00:00', 'step': 1, 'payload': {'id': '...', 'name': 'a', 'result': [('another_list', ['hi'])]}}
# {'type': 'task', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'input': {'alist': ['Ex for stream_mode="debug"'], 'another_list': ['hi']}, 'triggers': ['a']}}
# {'type': 'task_result', 'timestamp': '2024-06-23T...+00:00', 'step': 2, 'payload': {'id': '...', 'name': 'b', 'result': [('alist', ['there'])]}}
使用 stream_mode="custom"
from langgraph.types import StreamWriter

async def node_a(state: State, writer: StreamWriter):
    writer({"custom_data": "foo"})
    return {"alist": ["hi"]}

builder = StateGraph(State)
builder.add_node("a", node_a)
builder.add_edge(START, "a")
graph = builder.compile()

async for event in graph.astream({"alist": ['Ex for stream_mode="custom"']}, stream_mode="custom"):
    print(event)

# {'custom_data': 'foo'}
使用 stream_mode="messages"
from typing_extensions import Annotated, TypedDict
from langgraph.graph import StateGraph, START
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

class State(TypedDict):
    question: str
    answer: str

async def node_a(state: State):
    response = await llm.ainvoke(state["question"])
    return {"answer": response.content}

builder = StateGraph(State)
builder.add_node("a", node_a)
builder.add_edge(START, "a")
graph = builder.compile()

async for event in graph.astream({"question": "What is the capital of France?"}, stream_mode="messages"):
    print(event)

# (AIMessageChunk(content='The', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], 'langgraph_path': ('__pregel_pull', 'a'), 'langgraph_checkpoint_ns': '...', 'checkpoint_ns': '...', 'ls_provider': 'openai', 'ls_model_name': 'gpt-4o-mini', 'ls_model_type': 'chat', 'ls_temperature': 0.7})
# (AIMessageChunk(content=' capital', additional_kwargs={}, response_metadata={}, id='...'), {'langgraph_step': 1, 'langgraph_node': 'a', 'langgraph_triggers': ['start:a'], ...})
# (AIMessageChunk(content=' of', additional_kwargs={}, response_metadata={}, id='...'), {...})
# (AIMessageChunk(content=' France', additional_kwargs={}, response_metadata={}, id='...'), {...})
# (AIMessageChunk(content=' is', additional_kwargs={}, response_metadata={}, id='...'), {...})
# (AIMessageChunk(content=' Paris', additional_kwargs={}, response_metadata={}, id='...'), {...})

invoke

invoke(
    input: dict[str, Any] | Any,
    config: RunnableConfig | None = None,
    *,
    stream_mode: StreamMode = "values",
    output_keys: str | Sequence[str] | None = None,
    interrupt_before: All | Sequence[str] | None = None,
    interrupt_after: All | Sequence[str] | None = None,
    checkpoint_during: bool | None = None,
    debug: bool | None = None,
    **kwargs: Any
) -> dict[str, Any] | Any

使用单个输入和配置运行图。

参数

名称 类型 描述 默认值
input dict[str, Any] | Any

图的输入数据。它可以是一个字典或任何其他类型。

必需
config RunnableConfig | None

可选。图运行的配置。

None
stream_mode StreamMode

Optional[str]。图运行的流模式。默认为 "values"。

'values'
output_keys str | Sequence[str] | None

可选。从图运行中检索的输出键。

None
interrupt_before All | Sequence[str] | None

可选。在图运行中断前停止的节点。

None
interrupt_after All | Sequence[str] | None

可选。在图运行中断后停止的节点。

None
debug bool | None

可选。为图运行启用调试模式。

None
**kwargs Any

传递给图运行的额外关键字参数。

{}

返回

类型 描述
dict[str, Any] | Any

图运行的输出。如果 stream_mode 是 "values",则返回最新输出。

dict[str, Any] | Any

如果 stream_mode 不是 "values",则返回输出块列表。

ainvoke async

ainvoke(
    input: dict[str, Any] | Any,
    config: RunnableConfig | None = None,
    *,
    stream_mode: StreamMode = "values",
    output_keys: str | Sequence[str] | None = None,
    interrupt_before: All | Sequence[str] | None = None,
    interrupt_after: All | Sequence[str] | None = None,
    checkpoint_during: bool | None = None,
    debug: bool | None = None,
    **kwargs: Any
) -> dict[str, Any] | Any

在单个输入上异步调用图。

参数

名称 类型 描述 默认值
input dict[str, Any] | Any

计算的输入数据。它可以是一个字典或任何其他类型。

必需
config RunnableConfig | None

可选。计算的配置。

None
stream_mode StreamMode

可选。计算的流模式。默认为 "values"。

'values'
output_keys str | Sequence[str] | None

可选。结果中包含的输出键。默认为 None。

None
interrupt_before All | Sequence[str] | None

可选。中断前停止的节点。默认为 None。

None
interrupt_after All | Sequence[str] | None

可选。中断后停止的节点。默认为 None。

None
debug bool | None

可选。是否启用调试模式。默认为 None。

None
**kwargs Any

额外关键字参数。

{}

返回

类型 描述
dict[str, Any] | Any

计算结果。如果 stream_mode 是 "values",则返回最新值。

dict[str, Any] | Any

如果 stream_mode 是 "chunks",则返回块列表。

get_state

get_state(
    config: RunnableConfig, *, subgraphs: bool = False
) -> StateSnapshot

获取图的当前状态。

aget_state async

aget_state(
    config: RunnableConfig, *, subgraphs: bool = False
) -> StateSnapshot

获取图的当前状态。

get_state_history

get_state_history(
    config: RunnableConfig,
    *,
    filter: dict[str, Any] | None = None,
    before: RunnableConfig | None = None,
    limit: int | None = None
) -> Iterator[StateSnapshot]

获取图的状态历史。

aget_state_history async

aget_state_history(
    config: RunnableConfig,
    *,
    filter: dict[str, Any] | None = None,
    before: RunnableConfig | None = None,
    limit: int | None = None
) -> AsyncIterator[StateSnapshot]

异步获取图的状态历史。

update_state

update_state(
    config: RunnableConfig,
    values: dict[str, Any] | Any | None,
    as_node: str | None = None,
) -> RunnableConfig

使用给定值更新图的状态,如同它们来自节点 as_node。如果未提供 as_node,在不模糊的情况下,它将被设置为最后更新状态的节点。

aupdate_state async

aupdate_state(
    config: RunnableConfig,
    values: dict[str, Any] | Any,
    as_node: str | None = None,
) -> RunnableConfig

使用给定值异步更新图的状态,如同它们来自节点 as_node。如果未提供 as_node,在不模糊的情况下,它将被设置为最后更新状态的节点。

bulk_update_state

bulk_update_state(
    config: RunnableConfig,
    supersteps: Sequence[Sequence[StateUpdate]],
) -> RunnableConfig

批量应用更新到图状态。需要设置 checkpointer。

参数

名称 类型 描述 默认值
config RunnableConfig

应用更新的配置。

必需
supersteps Sequence[Sequence[StateUpdate]]

superstep 列表,每个 superstep 包括要按顺序应用于图状态的更新列表。每个更新都是一个形如 (values, as_node) 的元组。

必需

引发

类型 描述
ValueError

如果未设置 checkpointer 或未提供更新。

InvalidUpdateError

如果提供了无效更新。

返回

名称 类型 描述
RunnableConfig RunnableConfig

更新后的配置。

abulk_update_state async

abulk_update_state(
    config: RunnableConfig,
    supersteps: Sequence[Sequence[StateUpdate]],
) -> RunnableConfig

异步批量应用更新到图状态。需要设置 checkpointer。

参数

名称 类型 描述 默认值
config RunnableConfig

应用更新的配置。

必需
supersteps Sequence[Sequence[StateUpdate]]

superstep 列表,每个 superstep 包括要按顺序应用于图状态的更新列表。每个更新都是一个形如 (values, as_node) 的元组。

必需

引发

类型 描述
ValueError

如果未设置 checkpointer 或未提供更新。

InvalidUpdateError

如果提供了无效更新。

返回

名称 类型 描述
RunnableConfig RunnableConfig

更新后的配置。

get_graph

get_graph(
    config: RunnableConfig | None = None,
    *,
    xray: int | bool = False
) -> Graph

返回计算图的可绘制表示。

aget_graph async

aget_graph(
    config: RunnableConfig | None = None,
    *,
    xray: int | bool = False
) -> Graph

返回计算图的可绘制表示。

get_subgraphs

get_subgraphs(
    *, namespace: str | None = None, recurse: bool = False
) -> Iterator[tuple[str, PregelProtocol]]

获取图的子图。

参数

名称 类型 描述 默认值
namespace str | None

用于过滤子图的命名空间。

None
recurse bool

是否递归进入子图。如果为 False,则仅返回直接子图。

False

返回

类型 描述
Iterator[tuple[str, PregelProtocol]]

Iterator[tuple[str, PregelProtocol]]:(namespace, subgraph) 对的迭代器。

aget_subgraphs async

aget_subgraphs(
    *, namespace: str | None = None, recurse: bool = False
) -> AsyncIterator[tuple[str, PregelProtocol]]

获取图的子图。

参数

名称 类型 描述 默认值
namespace str | None

用于过滤子图的命名空间。

None
recurse bool

是否递归进入子图。如果为 False,则仅返回直接子图。

False

返回

类型 描述
AsyncIterator[tuple[str, PregelProtocol]]

AsyncIterator[tuple[str, PregelProtocol]]:(namespace, subgraph) 对的异步迭代器。

with_config

with_config(
    config: RunnableConfig | None = None, **kwargs: Any
) -> Self

使用更新的配置创建一个 Pregel 对象的副本。

评论