DEV Community

Cover image for Learn How to Build AI Agents & Chatbots with LangGraph!
Pavan Belagatti
Pavan Belagatti

Posted on

Learn How to Build AI Agents & Chatbots with LangGraph!

As the world increasingly embraces artificial intelligence, the development of AI agents has become a focal point for many industries. LangGraph emerges as a powerful framework that simplifies the creation of these agents, enabling developers to build sophisticated multi-agent systems. This blog will serve as a step-by-step tutorial on how to leverage LangGraph to create your own AI applications, including a basic chatbot.

Understanding LangGraph

LangGraph

LangGraph is an open-source framework designed for creating and managing AI agents and multi-agent applications. It provides the tools necessary to handle the complexities associated with state management, agent interactions, and error handling. By using LangGraph, developers can build robust applications that utilize Large Language Models (LLMs) effectively.

langgraph rag flow

At its core, LangGraph allows you to create agentic applications by defining three key components:

  • Nodes: These represent individual computation steps or functions within your application.
  • States: This refers to the context or memory maintained as computations progress.
  • Edges: These connect nodes, defining the flow of computation from one step to another.

Setting Up Your Environment

Before diving into coding, ensure you have the necessary software installed. You'll need Python and the required libraries. We will be using SingleStore's notebook (just like Jupyter Notebooks and Google Colab) feature to run all our code. Signup to SingleStore and create a new notebook.

Simple Chatbot with LangGraph

We'll create a simple chatbot using LangGraph. This chatbot will respond directly to user messages. We will start by creating a StateGraph. A StateGraph object defines the structure of our chatbot as a state machine.

Begin by installing LangGraph and LangChain with the following commands:

!pip install langgraph langsmith
!pip install langchain langchain_groq langchain_community
Enter fullscreen mode Exit fullscreen mode

Additionally, set up your API keys for any LLM services you plan to use. This will allow you to access the models you need for your AI applications.

import os
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_API_KEY'] = 'Add your LangChain API Key'
os.environ['LANGCHAIN_PROJECT'] = 'LiveLanggraph'
Enter fullscreen mode Exit fullscreen mode

This will help you getting started with ChatGroq chat models.

from langchain_groq import ChatGroq

groq_api_key = "Add your Groq API Key"  # Replace with your actual API key
llm = ChatGroq(groq_api_key=groq_api_key, model_name='Gemma2-9b-It')
Enter fullscreen mode Exit fullscreen mode

Create the graph

from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
Enter fullscreen mode Exit fullscreen mode
class State(TypedDict):
  messages:Annotated[list, add_messages]

graph_builder = StateGraph(State)
Enter fullscreen mode Exit fullscreen mode
graph_builder
Enter fullscreen mode Exit fullscreen mode
def chatbot(state:State):
  return {"messages" : llm.invoke(state['messages'])}
Enter fullscreen mode Exit fullscreen mode
graph_builder.add_node("chatbot",chatbot)
Enter fullscreen mode Exit fullscreen mode
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
Enter fullscreen mode Exit fullscreen mode
graph = graph_builder.compile()
Enter fullscreen mode Exit fullscreen mode
from IPython.display import Image, display

try:
  display(Image(graph.get_graph().draw_mermaid_png()))
except Exception:
  pass
Enter fullscreen mode Exit fullscreen mode

chatbot

Use the graph

We can now use the created chatbot.

while True:
  user_input=input("User: ")
  if user_input.lower() in ["quit","q"]:
    print("Good Bye")
    break
  for event in graph.stream({'messages':("user",user_input)}):
    print(event.values())
    for value in event.values():
      print(value['messages'])
      print("Assistant:",value["messages"].content)
Enter fullscreen mode Exit fullscreen mode

Conclusion

LangGraph provides a powerful framework for building AI agents and chatbots. By understanding its core components - nodes, states, and edges - you can create sophisticated applications that leverage LLMs for various tasks. Whether you're building a simple chatbot or a complex multi-agent system, LangGraph offers the tools necessary to develop and manage your AI solutions effectively.

For more detailed examples and code, check out the LangGraph Chatbot Tutorial on GitHub.

Top comments (0)