mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-21 17:18:09 +02:00
This PR removes the following changes: * notebooks that were converted to markdown * mkdocs.yml file to reference the ipython notebooks rather than the markdown files * Makefile install vercel reverted * hooks for markdown-exec * notebook conversion jinja2 templates (for converting notebooks to markdown exec format)
112 KiB
112 KiB
In [1]:
%%capture --no-stderr
%pip install -U langgraphIn [1]:
import operator
from typing import Annotated, Any
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
# The operator.add reducer fn makes this append-only
aggregate: Annotated[list, operator.add]
def a(state: State):
print(f'Adding "A" to {state["aggregate"]}')
return {"aggregate": ["A"]}
def b(state: State):
print(f'Adding "B" to {state["aggregate"]}')
return {"aggregate": ["B"]}
def c(state: State):
print(f'Adding "C" to {state["aggregate"]}')
return {"aggregate": ["C"]}
def d(state: State):
print(f'Adding "D" to {state["aggregate"]}')
return {"aggregate": ["D"]}
builder = StateGraph(State)
builder.add_node(a)
builder.add_node(b)
builder.add_node(c)
builder.add_node(d)
builder.add_edge(START, "a")
builder.add_edge("a", "b")
builder.add_edge("a", "c")
builder.add_edge("b", "d")
builder.add_edge("c", "d")
builder.add_edge("d", END)
graph = builder.compile()In [2]:
from IPython.display import Image, display
display(Image(graph.get_graph().draw_mermaid_png()))In [8]:
graph.invoke({"aggregate": []}, {"configurable": {"thread_id": "foo"}})Out [8]:
Adding "A" to [] Adding "B" to ['A'] Adding "C" to ['A'] Adding "D" to ['A', 'B', 'C']
{'aggregate': ['A', 'B', 'C', 'D']}In [4]:
def b_2(state: State):
print(f'Adding "B_2" to {state["aggregate"]}')
return {"aggregate": ["B_2"]}
builder = StateGraph(State)
builder.add_node(a)
builder.add_node(b)
builder.add_node(b_2)
builder.add_node(c)
builder.add_node(d)
builder.add_edge(START, "a")
builder.add_edge("a", "b")
builder.add_edge("a", "c")
builder.add_edge("b", "b_2")
# highlight-next-line
builder.add_edge(["b_2", "c"], "d")
builder.add_edge("d", END)
graph = builder.compile()In [5]:
from IPython.display import Image, display
display(Image(graph.get_graph().draw_mermaid_png()))In [6]:
graph.invoke({"aggregate": []})Out [6]:
Adding "A" to [] Adding "B" to ['A'] Adding "C" to ['A'] Adding "B_2" to ['A', 'B', 'C'] Adding "D" to ['A', 'B', 'C', 'B_2']
{'aggregate': ['A', 'B', 'C', 'B_2', 'D']}In [8]:
import operator
from typing import Annotated, Sequence
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
class State(TypedDict):
aggregate: Annotated[list, operator.add]
# Add a key to the state. We will set this key to determine
# how we branch.
which: str
def a(state: State):
print(f'Adding "A" to {state["aggregate"]}')
return {"aggregate": ["A"]}
def b(state: State):
print(f'Adding "B" to {state["aggregate"]}')
return {"aggregate": ["B"]}
def c(state: State):
print(f'Adding "C" to {state["aggregate"]}')
return {"aggregate": ["C"]}
def d(state: State):
print(f'Adding "D" to {state["aggregate"]}')
return {"aggregate": ["D"]}
def e(state: State):
print(f'Adding "E" to {state["aggregate"]}')
return {"aggregate": ["E"]}
builder = StateGraph(State)
builder.add_node(a)
builder.add_node(b)
builder.add_node(c)
builder.add_node(d)
builder.add_node(e)
builder.add_edge(START, "a")
def route_bc_or_cd(state: State) -> Sequence[str]:
if state["which"] == "cd":
return ["c", "d"]
return ["b", "c"]
intermediates = ["b", "c", "d"]
builder.add_conditional_edges(
"a",
route_bc_or_cd,
intermediates,
)
for node in intermediates:
builder.add_edge(node, "e")
builder.add_edge("e", END)
graph = builder.compile()In [9]:
from IPython.display import Image, display
display(Image(graph.get_graph().draw_mermaid_png()))In [10]:
graph.invoke({"aggregate": [], "which": "bc"})Out [10]:
Adding "A" to [] Adding "B" to ['A'] Adding "C" to ['A'] Adding "E" to ['A', 'B', 'C']
{'aggregate': ['A', 'B', 'C', 'E'], 'which': 'bc'}In [11]:
graph.invoke({"aggregate": [], "which": "cd"})Out [11]:
Adding "A" to [] Adding "C" to ['A'] Adding "D" to ['A'] Adding "E" to ['A', 'C', 'D']
{'aggregate': ['A', 'C', 'D', 'E'], 'which': 'cd'}