Brief IA

LangGraph: Orchestrating Advanced Artificial Intelligence

🔬 Research·Tom Levy·

LangGraph: Orchestrating Advanced Artificial Intelligence

LangGraph: Orchestrating Advanced Artificial Intelligence
Key Takeaways
1LangGraph provides a framework for orchestrating complex AI workflows, going beyond simple chatbots.
2Developers can model AI applications as graphs, making it easier to manage decision-making processes.
3With conditional nodes and edges, LangGraph allows for flexible and controlled execution of AI tasks.
💡Why it mattersLangGraph simplifies the creation of advanced AI systems, making processes smarter and more adaptive.
Le brief IA que lisent les pros

Le brief IA que les pros lisent chaque soir

Les 7 actus IA du jour, décryptées en 5 min. Gratuit.

Inclus dès l'inscription : notre sélection des meilleurs guides & comparatifs IA.

Choisis ton rythme

Gratuit · Pas de spam · Désabonnement en 1 clic

📄
Full Analysis

LangGraph: A New Era for AI Systems

With the rapid evolution of large language models (LLMs), developers are no longer content with creating simple chatbots. They are now venturing into designing systems capable of reasoning, making decisions, using various tools, retrieving information, interacting with APIs, and even collaborating with other AI agents. This increasing complexity presents a major challenge: how to effectively coordinate and manage this flow of intelligence? This is precisely the question that LangGraph aims to address.

LangGraph positions itself at the heart of this revolution by offering a robust framework for building AI workflows that are not only controllable but also production-ready. It allows developers to precisely define how AI agents should think, make decisions, interact with tools, and progress through complex tasks. While LangChain focuses on connecting AI components, LangGraph specializes in orchestrating the behavior of these components over time.

LangGraph is the result of the work of the team behind LangChain, enabling developers to model AI applications as graphs, thereby facilitating the management of decision-making processes.

Understanding Agent Graphs

To illustrate how LangGraph works, let's consider a simple graph composed of three nodes and a conditional edge. Imagine a food delivery process: each node represents a specific task or action. For example:

  • Receive the order is one node.
  • Prepare the food is another node.
  • Deliver the food is a third node.

Each time a task is completed, we find ourselves at a node. The edges, on the other hand, are the paths that indicate to the system the next step to take. For example:

  • Receive the order ↓ Prepare the food ↓ Deliver the food

These arrows represent the edges. They do not perform any work by themselves but simply indicate the direction to follow after each step. Think of an edge as a road connecting two cities, where the cities are the nodes and the road is the edge.

The state is the information that flows throughout the process. For instance, when a customer places an order, the details of that order (such as the type of pizza, delivery address, and customer name) travel with it through the various stages of the process.

Building a Simple Agent: The Importance of State

The state can be viewed as the shared memory of the graph. It is the information that moves through the workflow, passing from one node to another. Each node has the ability to read this state, update it, and pass the updated version to the next node. In our example, the state contains a single piece of information called graph_state.

To define the state of the graph, we use the TypedDict class from Python's typing module, which provides type hints for the keys.

from typing_extensions import TypedDict

class State(TypedDict):
    graph_state: str

Nodes: The Workers of the Graph

A node is essentially a function that performs a task. When a node is activated, it receives the current state, performs an operation on it, and returns an updated state. One can compare a node to a worker in a factory who receives a package (the state), modifies it, and then passes it on.

The first positional argument of a node is the state, as defined earlier. Each node can access the graph_state key with state['graph_state'] and return a new value for this key. By default, the new value returned by each node replaces the previous value of the state.

def node_1(state):
    print("---Node 1---")
    return {"graph_state": state['graph_state'] + "I am"}

def node_2(state):
    print("---Node 2---")
    return {"graph_state": state['graph_state'] + "happy!"}

def node_3(state):
    print("---Node 3---")
    return {"graph_state": state['graph_state'] + "sad!"}

Edges: The Connections Between Nodes

An edge is simply a connection between nodes, indicating to the graph the next step after a node has completed its work. A normal edge follows a fixed path, but a conditional edge acts as a decision point, allowing the graph to choose the path based on the current state.

For example, after analyzing data, the graph might ask itself: "Do I have enough information?" If the answer is yes, it moves to the next step; otherwise, it returns to a previous step to gather more data.

Conditional edges are implemented as functions that determine the next node to visit based on certain logic.

import random
from typing import Literal

def decide_mood(state) -> Literal["node_2", "node_3"]:
    user_input = state['graph_state']  # Here, let's simply randomly choose between nodes 2 and 3
    if random.random() < 0.5:  # 50% of the time, we return Node 2
        return "node_2"
    return "node_3"  # 50% of the time, we return Node 3

Building and Executing the Graph

To build the graph, we use the StateGraph class. After initializing a StateGraph with the State class, we add our nodes and edges. The START Node is a special node that sends the user input to the graph, indicating where to start, while the END Node represents a terminal node. Finally, we compile our graph to perform some basic checks on its structure.

from IPython.display import Image, display
from langgraph.graph import StateGraph, START, END

# Build the Graph
builder = StateGraph(state)
builder.add_node("node_1", node_1)
builder.add_node("node_2", node_2)
builder.add_node("node_3", node_3)

# Logic
builder.add_edge(START, "node_1")
builder.add_conditional_edges("node_1", decide_mood)
builder.add_edge("node_2", END)
builder.add_edge("node_3", END)

# Add
graph = builder.compile()

# View
display(Image(graph.get_graph().draw_mermaid_png()))

The compiled graph implements the executable protocol, providing a standard way to execute the components of LangChain. The invoke method allows for the execution of the graph using an initial input dictionary.

Thus, LangGraph offers an innovative approach to orchestrating complex AI systems, making processes smarter and more adaptive.

Brief IA — L'actualité IA en français

L'essentiel de l'actualité de l'intelligence artificielle, décrypté et expliqué chaque jour.