Skip to content

senthilece01/chatbots_with_langGraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Chatbots with LangGraph

Open In Colab Python LangGraph License: MIT

A hands-on tutorial for building stateful AI chatbots using LangGraph — a powerful framework from LangChain for orchestrating LLM workflows as graphs. This notebook demonstrates how to construct a conversational AI system with proper state management using Groq's ultra-fast inference with the Gemma2-9b model.


What is LangGraph?

LangGraph is a library built on top of LangChain that lets you model your AI agent logic as a stateful graph. Unlike simple LLM chains, LangGraph allows:

  • Cyclical flows — agents can loop, retry, and branch based on conditions
  • Persistent state — conversation history and context are maintained across turns
  • Multi-agent coordination — multiple specialized agents can collaborate within a single graph
  • Human-in-the-loop — you can pause execution and inject human feedback at any node

In this notebook, you'll learn the foundational building blocks: StateGraph, nodes, edges, and how to wire them together to create a working chatbot.


Architecture Overview

User Input
    │
    ▼
┌─────────────────────────────────┐
│         StateGraph              │
│                                 │
│   START ──► chatbot ──► END     │
│                                 │
│   State: { messages: [...] }    │
└─────────────────────────────────┘
    │
    ▼
LLM Response (via Groq)

The graph consists of:

  • State — a TypedDict holding the list of messages, updated via add_messages (append-only)
  • chatbot node — calls the Groq LLM with the current message history and returns the response
  • EdgesSTART → chatbot → END defining the flow

Tech Stack

Component Library Purpose
Graph Orchestration langgraph Stateful graph-based agent framework
LLM Provider langchain-groq Fast inference via Groq API
LLM Model Gemma2-9b-It Google's open-weight instruction-tuned model
Observability langsmith Tracing and monitoring LLM calls
Environment Google Colab / Jupyter Interactive notebook execution

Getting Started

Option 1: Run in Google Colab (Recommended)

Click the badge at the top → runs entirely in your browser, no local setup needed.

You'll need to add these secrets in Colab (🔑 Secrets panel in the left sidebar):

Option 2: Run Locally

1. Clone the repository

git clone https://github.com/senthilece01/chatbots_with_langGraph.git
cd chatbots_with_langGraph

2. Install dependencies

pip install langgraph langsmith langchain langchain-groq langchain-community

3. Set environment variables

Create a .env file in the project root:

GROQ_API_KEY=your_groq_api_key_here
LANGSMITH_API_KEY=your_langsmith_api_key_here

4. Launch the notebook

jupyter notebook chatbot_with_langGraph.ipynb

Notebook Walkthrough

1. Install Required Libraries

Installs langgraph, langsmith, langchain, langchain-groq, and langchain-community.

2. Configure API Keys

Loads secrets securely from Colab's secret manager (or .env for local use).

3. Initialize the LLM

Sets up ChatGroq with the Gemma2-9b-It model — one of the fastest open-weight models available via Groq's LPU inference engine.

4. Define the Graph State

class State(TypedDict):
    messages: Annotated[list, add_messages]

The add_messages annotation ensures messages are appended (not overwritten) on each graph invocation — this is what gives the chatbot memory.

5. Build the Graph

graph_builder = StateGraph(State)
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()

6. Visualize the Graph

Renders a Mermaid diagram showing the graph structure — useful for debugging complex multi-node graphs.

7. Run the Chatbot

Interactive loop that streams responses from the graph and prints them in real time.


Key Concepts Learned

  • StateGraph — the core LangGraph class for building stateful graphs
  • TypedDict State — how to define and annotate graph state
  • add_messages — LangGraph's built-in reducer for message lists
  • Nodes and Edges — how to compose graph logic
  • graph.stream() — streaming execution of the graph for real-time output
  • LangSmith Tracing — automatic observability for every LLM call

Prerequisites


Project Structure

chatbots_with_langGraph/
│
└── chatbot_with_langGraph.ipynb   # Main notebook with full implementation

What's Next?

This notebook covers the foundational single-node chatbot. Future notebooks in this series will cover:

  • Chatbot with memory (persistent conversation history using MemorySaver)
  • Tool-calling agents (web search, calculator, code execution)
  • Multi-agent systems (supervisor + worker pattern)
  • Human-in-the-loop flows with interrupt/resume

License

This project is licensed under the MIT License.

About

Chatbots built with LangGraph - examples including stateful chatbots using Groq LLM

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors