Objectives
- Define the State schema for storing message history.
- Initialize ChatOpenAI and invoke it from a graph node.
- Build a stateless agent graph with a single processor node.
- Compile and run the agent.
Agent I: Simple Agent Bot
Goal
Build a simple stateless agent using LangGraph that takes user messages and passes them directly to an LLM for processing.Sample Input
Sample Output
Plan
- Define the state schema (
AgentState) containingmessages(a list of messages). - Define the node function
processthat invokes the LLM with the messages. - Build the graph by adding the node and connecting START to it and it to END.
- Compile and invoke the agent.
Code Implementation
1. Define the State Schema
We define a schemaAgentState with a list of messages:
2. Define the Node Function
The node function receives the state, invokes the model, and prints the response:3. Build and Compile the Graph
We setup the StateGraph, register the node, and link entry and finish points:4. Invoke the Agent
We invoke the compiled agent:Exercise: Pirate Speak Translator Agent 🏴☠️
Goal
Build a custom system prompt agent that translates whatever user inputs into dramatic Pirate Speak.Sample Input
Sample Output
Plan
- Reuse or create an
AgentStatecontaining the message list. - Write a node function
pirate_nodethat inserts aSystemMessagespecifying the pirate persona ahead of the user messages, invokes the LLM, and prints the pirate response. - Initialize the
StateGraph, register the pirate node, hook it to START/END, and compile. - Test the compiled agent with a human message and verify the pirate speak output.
Solution
Solution
Practice & Exercises
To reinforce what you’ve learned in this section, practice with the interactive notebook:Practice & Exercises
Practice defining simple agent structures, customizing system instructions, and compiling basic workflows.💻 VS Code | 🚀 Colab | 📥 Download