๐ฏ What You'll Learn Today
LangGraph Tutorial: Implementing Message Classification Nodes - Unit 1.3 Exercise 2
This tutorial is also available in Google Colab here or for download here
Joint Initiative: This tutorial is part of a collaboration between AI Product Engineer and the Nebius Academy.
This tutorial demonstrates how to implement a message classification system in LangGraph using dedicated classifier nodes. We'll create a node that can analyze message content and assign both classifications and confidence scores.
Key Concepts Covered
- Message Content Analysis
- Classification Logic
- Confidence Score Assignment
- State Preservation
from typing import Annotated, TypedDict
!pip install langchain-core
!pip install langgraph
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.graph.message import add_messages
Step 1: State Definition for Classification
We define a state structure that supports classification operations and confidence tracking.
class State(TypedDict):
"""State container optimized for message classification.
This implementation focuses on three key aspects:
1. Message storage with proper annotations
2. Classification category assignment
3. Confidence score tracking
Attributes:
messages: List of conversation messages with add_messages annotation
classification: Current message classification label
confidence: Classification confidence score (0.0 to 1.0)
Note:
The combination of classification and confidence enables
more sophisticated routing and decision-making logic.
"""
messages: Annotated[list[BaseMessage], add_messages]
classification: str
confidence: float
Why This Matters
Message classification is crucial because it enables
- Intent-based conversation routing
- Automated response selection
- Analytics and monitoring
- Quality control through confidence tracking
Step 2: Classifier Node Implementation
We implement the core classification logic with confidence scoring.
def classifier_node(state: State) -> State:
"""Analyze and classify conversation messages with confidence scoring.
This node implements several key concepts:
1. Content-based classification
2. Confidence score assignment
3. State preservation during updates
The classification process follows this flow:
1. Extract last message content
2. Analyze content patterns
3. Assign classification and confidence
4. Preserve existing state
Args:
state: Current conversation state with messages
Returns:
State: Updated state with classification and confidence
Example:
>>> state = {
... "messages": [HumanMessage(content="Hello!")],
... "classification": "",
... "confidence": 0.0
... }
>>> result = classifier_node(state)
>>> print(f"{result['classification']}: {result['confidence']}")
"greeting: 0.9"
"""
# Extract message content for analysis
message = state["messages"][-1].content
# Perform classification analysis
if "hello" in message.lower():
return {
"messages": state["messages"],
"classification": "greeting",
"confidence": 0.9,
}
else:
return {
"messages": state["messages"],
"classification": "unknown",
"confidence": 0.1,
}
Debug Tips
- Classification Issues:
- Log message content before classification
- Verify classification logic paths
- Monitor confidence score assignments
- State Management:
- Check message preservation
- Validate classification updates
- Verify confidence range
- Common Errors:
- IndexError: Empty message list
- AttributeError: Invalid message format
- KeyError: Missing state fields
Key Takeaways
- Classification Design:
- Use content-based analysis
- Assign meaningful confidence scores
- Preserve existing messages
- State Handling:
- Maintain message integrity
- Update classifications atomically
- Track confidence accurately
Common Pitfalls
- Not handling edge cases
- Inconsistent confidence scoring
- Message list mutation
- Missing state preservation
Next Steps
- Add pattern recognition
- Implement multi-label support
- Add confidence thresholds
- Enable classification history
Example Usage
initial_state = {
"messages": [HumanMessage(content="Hello!")],
"classification": "",
"confidence": 0.0,
}
result = classifier_node(initial_state)
print(f"Classification: {result['classification']}")
print(f"Confidence: {result['confidence']}")
Variations and Extensions
- Enhanced Classification:
- Regular expression patterns
- Keyword-based scoring Example use case: Complex intent detection
- Advanced Confidence:
- Multi-factor confidence scoring
- Context-aware confidence Scenario: High-precision classification
Expected Output
Classification: greeting
Confidence: 0.9
๐ฌ๐ง Chapter