AI Agents with LangGraph

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

Lesson 04: Enhancing Agent Capabilities

Memory 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

Memory Demo

Open the empty memory.ipynb notebook in the Starter project.

from typing import TypedDict, Annotated, Sequence
from operator import add

class State(TypedDict):
  messages: Annotated[Sequence[str], add]

def function_1(state):
  return {"messages": ["uno"]}

def function_2(state):
  return {"messages": ["dos"]}

def function_3(state):
  return {"messages": ["tres"]}
from langgraph.graph import StateGraph, START, END

graph = StateGraph(State)
graph.add_node("node_1", function_1)
graph.add_node("node_2", function_2)
graph.add_node("node_3", function_3)
graph.add_edge(START, "node_1")
graph.add_edge("node_1", "node_2")
graph.add_edge("node_2", "node_3")
graph.add_edge("node_3", END)
from langgraph.checkpoint.memory import MemorySaver

memory = MemorySaver()
app = graph.compile(checkpointer=memory)
user_input = {"messages": ["hola"]}
thread = {"configurable": {"thread_id": "1"}}
app.invoke(user_input, thread)
{'messages': ['hola', 'uno', 'dos', 'tres']}
for state in app.get_state_history(thread):
  print(state)
  print("--")
See forum comments
Cinema mode Download course materials from Github
Previous: Memory Next: Structured Output