Spaces:
Sleeping
Sleeping
File size: 5,894 Bytes
087ac11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
"""
LangGraph State Schema
Defines the state that flows through the graph
"""
from typing import TypedDict, Optional, Dict, Any, List
from datetime import datetime
class ReviewState(TypedDict):
"""
State schema for review processing graph
All stages add to this state as it flows through the graph
"""
# Input data
review: Dict[str, Any]
review_id: str
review_text: str
rating: int
# Stage 1: Classification outputs
llm1_result: Optional[Dict[str, Any]]
llm2_result: Optional[Dict[str, Any]]
manager_result: Optional[Dict[str, Any]]
# Stage 1: Extracted fields for easy access
classification_type: Optional[str]
department: Optional[str]
priority: Optional[str]
user_type: Optional[str]
emotion: Optional[str]
# Stage 2: Sentiment outputs
best_sentiment_result: Optional[Dict[str, Any]]
alt_sentiment_result: Optional[Dict[str, Any]]
sentiment_layer_result: Optional[Dict[str, Any]]
# Stage 2: Extracted fields
sentiment: Optional[str] # POSITIVE, NEGATIVE, NEUTRAL
sentiment_confidence: Optional[float]
sentiment_agreement: Optional[bool]
# Stage 3: Finalization outputs
final_result: Optional[Dict[str, Any]]
# Stage 3: Extracted fields
final_sentiment: Optional[str]
final_confidence: Optional[float]
reasoning: Optional[str]
action_recommendation: Optional[str]
conflicts_found: Optional[str]
validation_notes: Optional[str]
# Routing decisions
needs_human_review: bool
route_to: Optional[str] # 'human_review', 'complete', 'batch_analysis'
# Processing metadata
stage1_completed: bool
stage2_completed: bool
stage3_completed: bool
processing_started_at: Optional[str]
processing_completed_at: Optional[str]
# Timing information
stage1_time: Optional[float]
stage2_time: Optional[float]
stage3_time: Optional[float]
total_time: Optional[float]
# Error handling
errors: List[str]
retry_count: int
# Database sync status
db_stage1_saved: bool
db_stage2_saved: bool
db_stage3_saved: bool
class BatchState(TypedDict):
"""
State for batch analysis (Stage 4)
Aggregates results from multiple reviews
"""
# Input
all_reviews: List[ReviewState]
total_count: int
# Aggregated metrics
sentiment_distribution: Optional[Dict[str, int]]
priority_distribution: Optional[Dict[str, int]]
department_distribution: Optional[Dict[str, int]]
emotion_distribution: Optional[Dict[str, int]]
# Analysis outputs
critical_issues: Optional[List[Dict[str, Any]]]
quick_wins: Optional[List[Dict[str, Any]]]
churn_risk: Optional[float]
model_agreement_rate: Optional[float]
# Recommendations
recommendations: Optional[List[str]]
# Processing metadata
batch_started_at: Optional[str]
batch_completed_at: Optional[str]
batch_processing_time: Optional[float]
def create_initial_state(review: Dict[str, Any]) -> ReviewState:
"""
Create initial state for a review
"""
return ReviewState(
# Input
review=review,
review_id=review.get('review_id', 'unknown'),
review_text=review.get('review_text', ''),
rating=review.get('rating', 3),
# Stage 1
llm1_result=None,
llm2_result=None,
manager_result=None,
classification_type=None,
department=None,
priority=None,
user_type=None,
emotion=None,
# Stage 2
best_sentiment_result=None,
alt_sentiment_result=None,
sentiment_layer_result=None,
sentiment=None,
sentiment_confidence=None,
sentiment_agreement=None,
# Stage 3
final_result=None,
final_sentiment=None,
final_confidence=None,
reasoning=None,
action_recommendation=None,
conflicts_found=None,
validation_notes=None,
# Routing
needs_human_review=False,
route_to=None,
# Processing metadata
stage1_completed=False,
stage2_completed=False,
stage3_completed=False,
processing_started_at=datetime.now().isoformat(),
processing_completed_at=None,
# Timing
stage1_time=None,
stage2_time=None,
stage3_time=None,
total_time=None,
# Errors
errors=[],
retry_count=0,
# Database
db_stage1_saved=False,
db_stage2_saved=False,
db_stage3_saved=False
)
def create_batch_state(reviews: List[ReviewState]) -> BatchState:
"""
Create batch state from processed reviews
"""
return BatchState(
all_reviews=reviews,
total_count=len(reviews),
sentiment_distribution=None,
priority_distribution=None,
department_distribution=None,
emotion_distribution=None,
critical_issues=None,
quick_wins=None,
churn_risk=None,
model_agreement_rate=None,
recommendations=None,
batch_started_at=datetime.now().isoformat(),
batch_completed_at=None,
batch_processing_time=None
)
if __name__ == "__main__":
# Test state creation
print("\n" + "="*60)
print("🧪 TESTING LANGGRAPH STATE")
print("="*60)
test_review = {
'review_id': 'test_001',
'review_text': 'App crashes!',
'rating': 1
}
state = create_initial_state(test_review)
print(f"\n✅ Initial state created for: {state['review_id']}")
print(f" Review text: {state['review_text']}")
print(f" Stage 1 completed: {state['stage1_completed']}")
print("\n✅ State schema test complete!")
|