File size: 10,913 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""
LangGraph Graph Definition
Defines the review processing workflow with conditional routing
"""

from langgraph.graph import StateGraph, END
from langgraph.checkpoint.memory import MemorySaver
from typing import Literal

from langgraph_state import ReviewState, BatchState, create_initial_state
from langgraph_nodes import (
    stage1_classification_node,
    stage2_sentiment_node,
    stage3_finalization_node
)
from stage4_batch_analysis import Stage4BatchAnalysis
from database_enhanced import EnhancedDatabase


# ============================================================================
# DATABASE SYNC NODES
# ============================================================================

def save_stage1_to_db_node(state: ReviewState) -> dict:
    """Save Stage 1 results to database"""
    db = EnhancedDatabase()
    db.connect()
    
    try:
        stage1_data = {
            'llm1_type': state['llm1_result'].get('type'),
            'llm1_department': state['llm1_result'].get('department'),
            'llm1_priority': state['llm1_result'].get('priority'),
            'llm1_confidence': state['llm1_result'].get('confidence'),
            'llm1_reasoning': state['llm1_result'].get('reasoning'),
            
            'llm2_user_type': state['llm2_result'].get('user_type'),
            'llm2_emotion': state['llm2_result'].get('emotion'),
            'llm2_context': state['llm2_result'].get('context'),
            'llm2_confidence': state['llm2_result'].get('confidence'),
            'llm2_reasoning': state['llm2_result'].get('reasoning'),
            
            'manager_classification': str(state['manager_result']),
            'manager_reasoning': state['manager_result'].get('reasoning'),
        }
        
        db.update_stage1(state['review_id'], stage1_data)
        db.close()
        
        return {"db_stage1_saved": True}
    except Exception as e:
        db.close()
        errors = state.get('errors', [])
        errors.append(f"DB Stage 1 save error: {str(e)}")
        return {"errors": errors}


def save_stage2_to_db_node(state: ReviewState) -> dict:
    """Save Stage 2 results to database"""
    db = EnhancedDatabase()
    db.connect()
    
    try:
        stage2_data = {
            'best_sentiment': state['best_sentiment_result']['sentiment'],
            'best_confidence': state['best_sentiment_result']['confidence'],
            'best_prob_positive': state['best_sentiment_result']['prob_positive'],
            'best_prob_neutral': state['best_sentiment_result']['prob_neutral'],
            'best_prob_negative': state['best_sentiment_result']['prob_negative'],
            
            'alt_sentiment': state['alt_sentiment_result']['sentiment'],
            'alt_confidence': state['alt_sentiment_result']['confidence'],
            'alt_prob_positive': state['alt_sentiment_result']['prob_positive'],
            'alt_prob_neutral': state['alt_sentiment_result']['prob_neutral'],
            'alt_prob_negative': state['alt_sentiment_result']['prob_negative'],
            
            'agreement': state['sentiment_agreement'],
            'layer_sentiment': state['sentiment'],
        }
        
        db.update_stage2(state['review_id'], stage2_data)
        db.close()
        
        return {"db_stage2_saved": True}
    except Exception as e:
        db.close()
        errors = state.get('errors', [])
        errors.append(f"DB Stage 2 save error: {str(e)}")
        return {"errors": errors}


def save_stage3_to_db_node(state: ReviewState) -> dict:
    """Save Stage 3 results to database"""
    db = EnhancedDatabase()
    db.connect()
    
    try:
        stage3_data = {
            'final_sentiment': state['final_sentiment'],
            'confidence': state['final_confidence'],
            'reasoning': state['reasoning'],
            'validation_notes': state['validation_notes'],
            'conflicts_found': state['conflicts_found'],
            'action_recommendation': state['action_recommendation'],
            'needs_human_review': state['needs_human_review'],
        }
        
        db.update_stage3(state['review_id'], stage3_data)
        db.close()
        
        return {"db_stage3_saved": True}
    except Exception as e:
        db.close()
        errors = state.get('errors', [])
        errors.append(f"DB Stage 3 save error: {str(e)}")
        return {"errors": errors}


# ============================================================================
# STAGE 4: BATCH ANALYSIS NODE
# ============================================================================

def stage4_batch_analysis_node(state: BatchState) -> dict:
    """
    Stage 4 Node: Batch analysis
    Runs after all reviews are processed
    """
    print(f"\n{'='*70}")
    print(f"📊 STAGE 4: BATCH ANALYSIS")
    print(f"{'='*70}")
    
    stage4 = Stage4BatchAnalysis()
    
    # Convert ReviewState list to dict format for Stage4
    reviews_for_analysis = []
    for review_state in state['all_reviews']:
        review_dict = {
            'review_id': review_state['review_id'],
            'review_text': review_state['review_text'],
            'rating': review_state['rating'],
            'stage1_llm1_type': review_state.get('classification_type'),
            'stage1_llm1_department': review_state.get('department'),
            'stage1_llm1_priority': review_state.get('priority'),
            'stage1_llm2_user_type': review_state.get('user_type'),
            'stage1_llm2_emotion': review_state.get('emotion'),
            'stage2_agreement': review_state.get('sentiment_agreement'),
            'stage3_final_sentiment': review_state.get('final_sentiment'),
            'stage3_needs_human_review': review_state.get('needs_human_review'),
            'stage3_reasoning': review_state.get('reasoning'),
            'stage3_action_recommendation': review_state.get('action_recommendation'),
        }
        reviews_for_analysis.append(review_dict)
    
    # Analyze batch
    insights = stage4.analyze_batch(reviews_for_analysis)
    
    # Save to database
    db = EnhancedDatabase()
    db.connect()
    db.save_batch_insights(insights)
    db.close()
    
    return {
        'sentiment_distribution': insights.get('sentiment_distribution'),
        'priority_distribution': insights.get('priority_distribution'),
        'department_distribution': insights.get('department_distribution'),
        'emotion_distribution': insights.get('emotion_distribution'),
        'critical_issues': insights.get('critical_issues'),
        'quick_wins': insights.get('quick_wins'),
        'churn_risk': insights.get('churn_risk'),
        'model_agreement_rate': insights.get('model_agreement_rate'),
        'recommendations': insights.get('recommendations'),
        'batch_completed_at': insights.get('batch_completed_at')
    }


# ============================================================================
# ROUTING FUNCTIONS
# ============================================================================

def route_after_stage3(state: ReviewState) -> Literal["human_review", "complete"]:
    """
    Conditional routing after Stage 3
    Decides if human review is needed
    """
    # Check if human review needed
    if state.get('needs_human_review', False):
        return "human_review"
    
    # Check confidence threshold
    if state.get('final_confidence', 1.0) < 0.5:
        return "human_review"
    
    # Check for conflicts
    if state.get('conflicts_found', 'none') != 'none':
        return "human_review"
    
    # Check priority
    if state.get('priority') == 'critical':
        return "human_review"
    
    return "complete"


def human_review_queue_node(state: ReviewState) -> dict:
    """
    Node for reviews flagged for human review
    Just marks them in the database
    """
    print(f"         🚨 FLAGGED for human review")
    
    # Could integrate with ticketing system, email alerts, etc.
    # For now, just mark in state
    return {
        "route_to": "human_review"
    }


# ============================================================================
# BUILD REVIEW PROCESSING GRAPH
# ============================================================================

def build_review_graph():
    """
    Build the complete review processing graph
    """
    
    # Create graph
    workflow = StateGraph(ReviewState)
    
    # Add all nodes
    workflow.add_node("stage1_classify", stage1_classification_node)
    workflow.add_node("save_stage1", save_stage1_to_db_node)
    
    workflow.add_node("stage2_sentiment", stage2_sentiment_node)
    workflow.add_node("save_stage2", save_stage2_to_db_node)
    
    workflow.add_node("stage3_finalize", stage3_finalization_node)
    workflow.add_node("save_stage3", save_stage3_to_db_node)
    
    workflow.add_node("human_review_queue", human_review_queue_node)
    
    # Add edges (sequential flow through stages)
    workflow.add_edge("stage1_classify", "save_stage1")
    workflow.add_edge("save_stage1", "stage2_sentiment")
    workflow.add_edge("stage2_sentiment", "save_stage2")
    workflow.add_edge("save_stage2", "stage3_finalize")
    workflow.add_edge("stage3_finalize", "save_stage3")
    
    # Add conditional routing after Stage 3
    workflow.add_conditional_edges(
        "save_stage3",
        route_after_stage3,
        {
            "human_review": "human_review_queue",
            "complete": END
        }
    )
    
    # Human review goes to END
    workflow.add_edge("human_review_queue", END)
    
    # Set entry point
    workflow.set_entry_point("stage1_classify")
    
    # Compile with checkpointing
    memory = MemorySaver()
    graph = workflow.compile(checkpointer=memory)
    
    return graph


# ============================================================================
# BUILD BATCH ANALYSIS GRAPH (Stage 4)
# ============================================================================

def build_batch_graph():
    """
    Build the batch analysis graph (Stage 4)
    This runs after all reviews are processed
    """
    
    workflow = StateGraph(BatchState)
    
    # Add batch analysis node
    workflow.add_node("stage4_batch", stage4_batch_analysis_node)
    
    # Simple linear flow
    workflow.set_entry_point("stage4_batch")
    workflow.add_edge("stage4_batch", END)
    
    # Compile
    graph = workflow.compile()
    
    return graph


if __name__ == "__main__":
    print("\n" + "="*60)
    print("🧪 TESTING LANGGRAPH GRAPH BUILDER")
    print("="*60)
    
    # Build review graph
    print("\n📊 Building review processing graph...")
    review_graph = build_review_graph()
    print("   ✅ Review graph built!")
    
    # Build batch graph
    print("\n📊 Building batch analysis graph...")
    batch_graph = build_batch_graph()
    print("   ✅ Batch graph built!")
    
    print("\n✅ Graph builder test complete!")