Get startedGet started for free

Graph and agent states

You've been commissioned to create a basic chatbot that can answer questions within a high school education app. The school would like you to use a version of ChatGPT from OpenAI as its LLM. You've decided you can efficiently manage this task using LangGraph to build a chatbot agent using nodes. First, you'll define an agent State() to store the agent's data, and set up a StateGraph() object to manage the agent's workflow.

The required modules have already been imported for this exercise and those that will follow:

from langchain_openai import ChatOpenAI
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages

This exercise is part of the course

Designing Agentic Systems with LangChain

View Course

Exercise instructions

  • Set up the llm using ChatOpenAI() and the model "gpt-4o-mini".
  • Define the State class using TypedDict to manage the chatbot's data.
  • Specify messages as an Annotated list using add_messages.
  • Initialize a StateGraph instance with State to structure the chatbot's workflow.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define the llm
llm = ____(model="____", api_key="OPENAI_API_KEY")

# Define the State
class State(____):
    
    # Define messages with metadata
    messages: ____[____, ____]

# Initialize StateGraph
graph_builder = ____(____)
Edit and Run Code