mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-20 06:35:46 +02:00
Merge pull request #256 from langchain-ai/nc/1apr/docstrings-multi-edges
Add some more docstrings around multi edges
This commit is contained in:
@@ -151,7 +151,10 @@ class Graph:
|
||||
if not self.support_multiple_edges and start_key in set(
|
||||
start for start, _ in self.edges
|
||||
):
|
||||
raise ValueError(f"Already found path for {start_key}")
|
||||
raise ValueError(
|
||||
f"Already found path for node '{start_key}'.\n"
|
||||
"For multiple edges, use StateGraph with an annotated state key."
|
||||
)
|
||||
|
||||
self.edges.add((start_key, end_key))
|
||||
|
||||
|
||||
@@ -21,6 +21,14 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StateGraph(Graph):
|
||||
"""A graph whose nodes communicate by reading and writing to a shared state.
|
||||
The signature of each node is State -> Partial<State>.
|
||||
|
||||
Each state key can optionally be annotated with a reducer function that
|
||||
will be used to aggregate the values of that key received from multiple nodes.
|
||||
The signature of a reducer function is (Value, Value) -> Value.
|
||||
"""
|
||||
|
||||
def __init__(self, schema: Type[Any]) -> None:
|
||||
super().__init__()
|
||||
self.schema = schema
|
||||
|
||||
@@ -2996,3 +2996,62 @@ def test_in_one_fan_out_state_graph_waiting_edge_multiple() -> None:
|
||||
},
|
||||
{"qa": {"answer": "doc1,doc1,doc2,doc2,doc3,doc3,doc4,doc4"}},
|
||||
]
|
||||
|
||||
|
||||
def test_simple_multi_edge() -> None:
|
||||
class State(TypedDict):
|
||||
my_key: Annotated[str, operator.add]
|
||||
|
||||
def up(state: State):
|
||||
pass
|
||||
|
||||
def side(state: State):
|
||||
pass
|
||||
|
||||
def down(state: State):
|
||||
pass
|
||||
|
||||
graph = StateGraph(State)
|
||||
|
||||
graph.add_node("up", up)
|
||||
graph.add_node("side", side)
|
||||
graph.add_node("down", down)
|
||||
|
||||
graph.set_entry_point("up")
|
||||
graph.add_edge("up", "side")
|
||||
graph.add_edge("up", "down")
|
||||
graph.add_edge(["up", "side"], "down")
|
||||
graph.set_finish_point("down")
|
||||
|
||||
app = graph.compile()
|
||||
|
||||
assert app.get_graph().draw_ascii() == (
|
||||
""" +-----------+
|
||||
| __start__ |
|
||||
+-----------+
|
||||
*
|
||||
*
|
||||
*
|
||||
+----+
|
||||
| up |
|
||||
+----+
|
||||
** **
|
||||
* *
|
||||
* *
|
||||
+------+ *
|
||||
| side | *
|
||||
+------+ *
|
||||
** **
|
||||
* *
|
||||
* *
|
||||
+------+
|
||||
| down |
|
||||
+------+
|
||||
*
|
||||
*
|
||||
*
|
||||
+---------+
|
||||
| __end__ |
|
||||
+---------+ """
|
||||
)
|
||||
assert app.invoke({"my_key": "my_value"}) == {"my_key": "my_value"}
|
||||
|
||||
Reference in New Issue
Block a user