Merge pull request #256 from langchain-ai/nc/1apr/docstrings-multi-edges

Add some more docstrings around multi edges
This commit is contained in:
Nuno Campos
2024-04-01 16:16:17 -07:00
committed by GitHub
3 changed files with 71 additions and 1 deletions
+4 -1
View File
@@ -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))
+8
View File
@@ -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
+59
View File
@@ -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"}