Building graphs for chatbots

Progettare sistemi agentici con LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Chatbots with LangGraph

Head of chatbot

 

  • Custom agents with LangGraph

     
    • Workflow management
      • Graph states
      • Agent states
    • Chatbot construction
      • Nodes
      • Edges
Progettare sistemi agentici con LangChain

Graphs and agent states

LangGraph icon

   

Graph State

  • Organizes different tasks
    • Tool use
    • LLM calls
  • Order of tasks = workflow

Agent State

  • Tracks agent's progress
  • Logs task completion
Progettare sistemi agentici con LangChain

Building an agent with LangGraph

People reading, sat on large books.

Magnifying glass representing information.

Progettare sistemi agentici con LangChain

Nodes and edges

Chatbot diagram.

   

Nodes

  • Functions or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes
Progettare sistemi agentici con LangChain

Nodes and edges

Chatbot diagram with first edge highlighted.

   

Nodes

  • Tasks or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes
Progettare sistemi agentici con LangChain

Nodes and edges

Chatbot diagram with second edge highlighted.

   

Nodes

  • Tasks or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes
Progettare sistemi agentici con LangChain

Nodes and edges

Full chatbot diagram highlighted.

   

Nodes

  • Tasks or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes

Pre-built Nodes

START and END nodes from LangGraph

Progettare sistemi agentici con LangChain

Building graph and agent states

# Modules for structuring text
from typing import Annotated
from typing_extensions import TypedDict


# LangGraph modules for defining graphs from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages
# Module for setting up OpenAI from langchain_openai import ChatOpenAI
Progettare sistemi agentici con LangChain

Building graph and agent states

# Define the llm
llm = ChatOpenAI(model="gpt-4o-mini", api_key="OPENAI_API_KEY")


# Define the State class State(TypedDict):
# Define messages with metadata messages: Annotated[list, add_messages]
# Initialize StateGraph graph_builder = StateGraph(State)
Progettare sistemi agentici con LangChain

Adding nodes and edges

# Define chatbot function to respond
# with the model
def chatbot(state: State):
    return {"messages": 
    [llm.invoke(state["messages"])]}


# Add chatbot node to the graph graph_builder.add_node("chatbot", chatbot)

Chatbot node.

Progettare sistemi agentici con LangChain

Adding nodes and edges

# Define the start and end of the 
# conversation flow
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)


# Compile the graph to prepare for # execution graph = graph_builder.compile()

Chatbot diagram.

Progettare sistemi agentici con LangChain

Let's practice!

Progettare sistemi agentici con LangChain

Preparing Video For Download...