mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-30 03:39:38 +02:00
75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
from typing import Any
|
|
|
|
CONFIG_KEY_SEND = "__pregel_send"
|
|
CONFIG_KEY_READ = "__pregel_read"
|
|
INTERRUPT = "__interrupt__"
|
|
TASKS = "__pregel_tasks"
|
|
RESERVED = {INTERRUPT, TASKS, CONFIG_KEY_SEND, CONFIG_KEY_READ}
|
|
TAG_HIDDEN = "langsmith:hidden"
|
|
|
|
START = "__start__"
|
|
END = "__end__"
|
|
|
|
|
|
class Send:
|
|
"""A message or packet to send to a specific node in the graph.
|
|
|
|
The `Send` class is used within a `StateGraph`'s conditional edges to dynamically
|
|
route states to different nodes based on certain conditions. This enables
|
|
creating "map-reduce" like workflows, where a node can be invoked multiple times
|
|
in parallel on different states, and the results can be aggregated back into the
|
|
main graph's state.
|
|
|
|
Attributes:
|
|
node (str): The name of the target node to send the message to.
|
|
arg (Any): The state or message to send to the target node.
|
|
|
|
Examples:
|
|
>>> from typing import Annotated
|
|
>>> import operator
|
|
>>> class OverallState(TypedDict):
|
|
... subjects: list[str]
|
|
... jokes: Annotated[list[str], operator.add]
|
|
...
|
|
>>> from langgraph.constants import Send
|
|
>>> from langgraph.graph import END, START
|
|
>>> def continue_to_jokes(state: OverallState):
|
|
... return [Send("generate_joke", {"subject": s}) for s in state['subjects']]
|
|
...
|
|
>>> from langgraph.graph import StateGraph
|
|
>>> builder = StateGraph(OverallState)
|
|
>>> builder.add_node("generate_joke", lambda state: {"jokes": [f"Joke about {state['subject']}"]})
|
|
>>> builder.add_conditional_edges(START, continue_to_jokes)
|
|
>>> builder.add_edge("generate_joke", END)
|
|
>>> graph = builder.compile()
|
|
>>> graph.invoke({"subjects": ["cats", "dogs"]})
|
|
{'subjects': ['cats', 'dogs'], 'jokes': ['Joke about cats', 'Joke about dogs']}
|
|
"""
|
|
|
|
node: str
|
|
arg: Any
|
|
|
|
def __init__(self, /, node: str, arg: Any) -> None:
|
|
"""
|
|
Initialize a new instance of the Send class.
|
|
|
|
Args:
|
|
node (str): The name of the target node to send the message to.
|
|
arg (Any): The state or message to send to the target node.
|
|
"""
|
|
self.node = node
|
|
self.arg = arg
|
|
|
|
def __hash__(self) -> int:
|
|
return hash((self.node, self.arg))
|
|
|
|
def __repr__(self) -> str:
|
|
return f"Send(node={self.node!r}, arg={self.arg!r})"
|
|
|
|
def __eq__(self, value: object) -> bool:
|
|
return (
|
|
isinstance(value, Send)
|
|
and self.node == value.node
|
|
and self.arg == value.arg
|
|
)
|