AI Agents with LangGraph

Nov 12 2024 · Python 3.12, LangGraph 0.2.x, JupyterLab 4.2.4

Lesson 02: Fundamentals of LangGraph

State Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

State Demo

Open the empty state-demo.ipynb notebook in the Starter folder. You’ll make a graph that adds toppings to a pizza. First, define your State class:

from typing import TypedDict

class PizzaState(TypedDict):
  toppings: list[str]
  quantity: int
def add_cheese(state):
  quantity = state["quantity"] + 1
  return {"toppings": ["cheese"], "quantity": quantity}
from langgraph.graph import StateGraph

graph = StateGraph(PizzaState)

graph.add_node("cheese", add_cheese)

graph.set_entry_point("cheese")
graph.set_finish_point("cheese")

app = graph.compile()

initial_state = {"toppings": [], "quantity": 0}
app.invoke(initial_state)
def add_meat(state):
  quantity = state["quantity"] + 1
  return {"toppings": ["meat"], "quantity": quantity}
graph.add_node("meat", add_meat)
graph.add_edge("cheese", "meat")
graph.set_finish_point("meat")
from typing import TypedDict, Annotated
from operator import add

class PizzaState(TypedDict):
  toppings: Annotated[list[str], add]
  quantity: int
See forum comments
Cinema mode Download course materials from Github
Previous: State Next: Project Demo