Designing Agentic Systems with LangChain
Dilini K. Sumanapala, PhD
Founder & AI Engineer, Genverv, Ltd.
START and END nodes from LangGraph
# 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
# 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)
# 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)
# 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()
Designing Agentic Systems with LangChain