AI Agents Using Langraph: A Comprehensive Guide

ยท

5 min read

AI Agents Using Langraph: A Comprehensive Guide

Introduction:

Creating AI agents is a complex yet fascinating task that involves multiple components and intricate workflows. Langraph is a powerful tool designed to simplify this process, enabling developers to build, manage, and enhance AI agents efficiently. This blog will walk you through the essential components of Langraph, explain how to create a simple Langraph, and delve into advanced concepts such as persistence, streaming, human-in-the-loop, and state memory. To illustrate the entire process, we will conclude with a case study on creating an essay writer AI agent.

Components of a Graph

Langraph leverages a graph-based approach to design AI agents. A typical graph consists of three main components:

  1. Nodes: Represent agents or functions.

  2. Edges: Connect nodes to define the flow.

  3. Conditional Edges: Enable decision-making within the graph.

The basic workflow of a langraph can be shown as:

Data/State

The agent's state in Langraph is accessible throughout the graph and can be stored in a persistence layer. There are two types of states: simple and complex.

Simple State

A simple state contains a single parameter called message, which is annotated, meaning each new message is appended to the existing messages.

Complex State

A complex state includes four parameters: input, chat_history, agent_outcome, and intermediate_steps. Among these, only intermediate_steps is annotated, allowing for appending, while the other features are updated with each new entry.

Creating a Simple Langraph

Creating a simple Langraph involves several steps:

  1. Import Necessary Libraries and Define Tools:

  2. Define AgentState to Store Messages Across the Application:

  3. Define the Agent Class:

    • Align the system message through the input.

    • Integrate AgentState into the graph.

    • Add nodes and conditional edges by specifying the node's name and function.

    • Set the entry point of the application.

    • Create an instance of Langchain runnable using the compile method.

    • Store a list of tool names and attach tools to the model.

    • Pass the system prompt, list of tools, and model to the agent instance.

    • Visualize the graph.

    • Pass the query inside the invoke method of the agent.

Persistence

Persistence allows maintaining an agent's state at a specific point in time, enabling you to revert to that state and resume operations from there. To add persistence, include a checkpointer while compiling the graph.

Streaming

Streaming lets you emit a list of signals to show real-time operations. For streaming messages, add the threading configuration and use the stream method along with the thread configuration.

For streaming tokens, use the astream method asynchronously, employing AsyncSqliteSaver as a checkpoint instead of SqliteSaver.

Human in the Loop

Human-in-the-loop involves integrating human oversight into the AI workflow. This can be achieved by:

  1. Creating a custom function to update the user message in the state.

  2. Adding an interrupt point on the node where human intervention is required while compiling the graph.

  3. Creating the agent with the prompt, model, and tool.

  4. Using threading and streaming to call the agent.

State Memory

As the graph executes, a snapshot of each memory state is stored. This snapshot includes agentstate and useful attributes like thread and a unique identifier called thread_ts. Thread_ts is crucial for accessing and modifying specific states if needed.

Case Study: Creating an Essay Writer:

To illustrate the process, let's create an essay writer AI agent:

  1. Import Essentials and Setup Memory:

  2. Define AgentState with attributes: task (topic of essay), plan (generated by the planning agent), draft (essay content), critique (populated by the critique agent), content (documents retrieved by the search tool), revision_number (track of revisions), and max_revision (maximum number of revisions).

  3. Define the Model:

  4. Write Prompts for Each Agent:

  5. Import Tavily as a Searching Tool and define the structure of queries.

  6. Declare and Define Each Agent Individually:

    • Start with the planning agent to generate a plan using AgentState and the planning prompt.

    • Create a research agent to generate a query based on the task and retrieve documents using Tavily, storing the output in content.

    • Develop a generation node to create the draft of the essay, utilizing the topic, plan, and documents.

    • Implement a reflection node to review the rough draft and store feedback in critique.

    • Create a research critique node to refine the critique, passing the response through Tavily search and appending the results.

    • Add a function to check if the revision number exceeds the maximum and determine whether to continue or stop revisions.

  7. Initialize the Graph with AgentState and add the nodes separately.

  8. Set the Entry Point and Add Conditional Edges.

  9. Complete the Graph by Compiling the Memory.

  10. Test with an Input Subject:

Summary

This blog provided an in-depth look at creating AI agents using Langraph, covering the core components, data/state types, the step-by-step process of building a simple Langraph, and advanced features like persistence, streaming, human-in-the-loop, and state memory. The case study on creating an essay writer AI agent demonstrated how these concepts come together to build a functional and sophisticated AI solution. By following this guide, you can harness the power of Langraph to develop robust AI agents tailored to your specific needs.

ย