Building graphs for chatbots

Designing Agentic Systems with 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
Designing Agentic Systems with 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
Designing Agentic Systems with LangChain

Building an agent with LangGraph

People reading, sat on large books.

Magnifying glass representing information.

Designing Agentic Systems with LangChain

Nodes and edges

Chatbot diagram.

   

Nodes

  • Functions or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes
Designing Agentic Systems with LangChain

Nodes and edges

Chatbot diagram with first edge highlighted.

   

Nodes

  • Tasks or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes
Designing Agentic Systems with LangChain

Nodes and edges

Chatbot diagram with second edge highlighted.

   

Nodes

  • Tasks or actions
    • Response
    • Tool call

Edges

  • Rules connecting nodes
Designing Agentic Systems with 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

Designing Agentic Systems with 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
Designing Agentic Systems with 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)
Designing Agentic Systems with 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.

Designing Agentic Systems with 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.

Designing Agentic Systems with LangChain

Let's practice!

Designing Agentic Systems with LangChain

Preparing Video For Download...