File size: 9,283 Bytes
9858829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
315
316
317
318
319
320
# Visualization Agents

## Overview
This folder contains AI-powered agents that enhance the sentiment analysis dashboard with intelligent, context-aware insights and analysis capabilities.

## Architecture

### Base Agent Pattern
All agents inherit from `BaseVisualizationAgent` which provides:
- Common interface (`process()`, `validate_input()`)
- Error handling
- Logging functionality
- Consistent configuration

### LLM Helper
`utils/llm_helper.py` provides:
- OpenAI API integration
- Retry logic with exponential backoff
- JSON mode support
- Token usage tracking

## Available Agents

### 1. ContentSummaryAgent

**Purpose**: Analyze and summarize comments for content pieces

**Location**: `agents/content_summary_agent.py`

**Input**:
```python
{
    'content_sk': str,              # Content identifier
    'content_description': str,      # Content title/description
    'comments': DataFrame or list    # Comments data
}
```

**Output**:
```python
{
    'success': bool,
    'content_sk': str,
    'summary': {
        'executive_summary': str,              # 2-3 sentence overview
        'main_themes': [                       # Top themes discussed
            {
                'theme': str,
                'sentiment': str,  # positive/negative/mixed
                'description': str
            }
        ],
        'praise_points': [str],                # What users love
        'key_complaints': [str],               # Main concerns
        'frequently_asked_questions': [str],   # Common questions
        'unexpected_insights': [str],          # Surprising patterns
        'action_recommendations': [            # Suggested actions
            {
                'priority': str,   # high/medium/low
                'action': str
            }
        ]
    },
    'metadata': {
        'total_comments_analyzed': int,
        'model_used': str,
        'tokens_used': int
    }
}
```

**Configuration**:
- Model: `gpt-5-nano` (configurable)
- Temperature: 0.3 (lower for focused summaries)
- Sampling: All negative comments + up to 50 positive/neutral (if >100 total)

**Features**:
- **Smart sampling**: Prioritizes negative comments, samples others
- **Context preservation**: Includes sentiment and intent metadata
- **Token optimization**: Truncates long comments to 300 chars
- **Structured output**: JSON format with guaranteed fields
- **Error handling**: Graceful failures with retry capability

## UI Integration

### Poor Sentiment Contents Page

**Location**: `components/poor_sentiment_contents.py`

**User Flow**:
1. User views content cards on Poor Sentiment Contents page
2. Clicks "๐Ÿ” Generate AI Analysis" button
3. Agent processes comments (with spinner indicator)
4. Summary displays in expandable section
5. Result cached in session state

**Display Sections**:
- **Executive Summary**: High-level overview (info box)
- **Main Themes**: Key topics with sentiment indicators
- **Praise Points** โœ… & **Key Complaints** โš ๏ธ (side-by-side)
- **FAQs** โ“ & **Unexpected Insights** ๐Ÿ’ก (side-by-side)
- **Recommended Actions** ๐ŸŽฏ (priority-coded)
- **Analysis Metadata** โ„น๏ธ (expandable details)

**Session Caching**:
- Summaries stored in `st.session_state.content_summaries`
- Key: `content_sk`
- Persists during session, cleared on page reload
- Prevents redundant API calls

## Usage Example

```python
from agents.content_summary_agent import ContentSummaryAgent
import pandas as pd

# Initialize agent
agent = ContentSummaryAgent(model="gpt-5-nano", temperature=0.3)

# Prepare input
input_data = {
    'content_sk': '12345',
    'content_description': 'Advanced Drum Fills Tutorial',
    'comments': comments_df  # DataFrame with comments
}

# Generate summary
result = agent.process(input_data)

if result['success']:
    summary = result['summary']
    print(summary['executive_summary'])

    for theme in summary['main_themes']:
        print(f"Theme: {theme['theme']} ({theme['sentiment']})")
        print(f"  {theme['description']}")
else:
    print(f"Error: {result['error']}")
```

## Environment Setup

### Required Environment Variables
Add to `.env` file (parent directory):
```bash
OPENAI_API_KEY=your_openai_api_key_here
```

### Dependencies
All dependencies already in `visualization/requirements.txt`:
- `streamlit>=1.28.0`
- `pandas>=2.0.0`
- `python-dotenv>=1.0.0`
- OpenAI library (inherited from parent project)

## Error Handling

### Agent-Level Errors
- **Invalid input**: Returns `{'success': False, 'error': 'Invalid input data'}`
- **LLM API failure**: Retries up to 3 times with exponential backoff
- **JSON parsing error**: Returns error with raw content
- **Exception**: Catches all exceptions, logs, returns error dict

### UI-Level Errors
- Displays error message in red box
- Provides "๐Ÿ”„ Retry Analysis" button
- Clears cache and regenerates on retry
- Logs errors to agent logger

## Performance Considerations

### API Costs
- Model: `gpt-5-nano` (cost-effective)
- Sampling strategy: Reduces tokens by up to 50% for large comment sets
- Comment truncation: Max 300 chars per comment
- Session caching: Eliminates duplicate API calls

### Response Time
- Average: 5-10 seconds for 50-100 comments
- Depends on: Comment count, OpenAI API latency
- User feedback: Spinner shows "Analyzing comments with AI..."

### Scalability
- Handles up to 100 comments per analysis (after sampling)
- Parallel requests: Each content analyzed independently
- Session state: Memory usage scales with number of analyzed contents

## Extending Agents

### Adding New Agents

1. **Create agent file**:
```python
# agents/new_agent.py
from agents.base_agent import BaseVisualizationAgent
from utils.llm_helper import LLMHelper

class NewAgent(BaseVisualizationAgent):
    def __init__(self, model="gpt-5-nano", temperature=0.7):
        super().__init__(name="NewAgent", model=model, temperature=temperature)
        self.llm_helper = LLMHelper(model=model, temperature=temperature)

    def validate_input(self, input_data):
        # Validation logic
        return True

    def process(self, input_data):
        # Processing logic
        pass
```

2. **Update `__init__.py`**:
```python
from .new_agent import NewAgent

__all__ = ['ContentSummaryAgent', 'NewAgent']
```

3. **Integrate in UI**:
- Import agent in component file
- Add UI controls (buttons, inputs)
- Display results
- Handle caching if needed

### Best Practices

1. **Input Validation**: Always validate required fields
2. **Error Handling**: Use `handle_error()` method
3. **Logging**: Use `log_processing()` for debugging
4. **Structured Output**: Return consistent dict format
5. **Caching**: Use session state for expensive operations
6. **Token Optimization**: Sample/truncate data for large inputs
7. **User Feedback**: Show spinners for async operations
8. **Graceful Degradation**: Provide fallbacks for failures

## Testing

### Manual Testing
1. Start dashboard: `streamlit run app.py`
2. Navigate to "โš ๏ธ Poor Sentiment Contents" page
3. Click "๐Ÿ” Generate AI Analysis" for any content
4. Verify summary displays correctly
5. Check session caching (click button again)
6. Test error handling (disconnect network)

### Unit Testing
```python
# tests/test_content_summary_agent.py
import pytest
from agents.content_summary_agent import ContentSummaryAgent

def test_validate_input():
    agent = ContentSummaryAgent()

    # Valid input
    valid_input = {
        'content_sk': '123',
        'content_description': 'Test',
        'comments': []
    }
    assert agent.validate_input(valid_input) == True

    # Missing field
    invalid_input = {'content_sk': '123'}
    assert agent.validate_input(invalid_input) == False
```

## Future Enhancements

### Planned Features
1. **Batch Analysis**: Analyze multiple contents at once
2. **Trend Detection**: Compare with historical summaries
3. **Export Summaries**: Download as PDF/CSV
4. **Custom Prompts**: User-defined analysis focus
5. **Multi-language Support**: Summaries in user's language

### Additional Agents (Roadmap)
- **InsightsSummaryAgent**: Overall dataset insights
- **InteractiveChatbotAgent**: Conversational analysis
- **ComparativeContentAgent**: Content comparison
- **ReplySuggestionAgent**: Generate reply suggestions
- **TrendForecastingAgent**: Predict sentiment trends

## Troubleshooting

### Common Issues

**Issue**: `OPENAI_API_KEY not found`
- **Solution**: Add key to `.env` file in parent directory

**Issue**: Import error for `agents` module
- **Solution**: Ensure `__init__.py` exists in `visualization/agents/`

**Issue**: LLM timeout errors
- **Solution**: Reduce comment count or increase retry limit

**Issue**: JSON parsing errors
- **Solution**: Check LLM prompt format, ensure JSON mode enabled

**Issue**: Cached summaries not showing
- **Solution**: Check `st.session_state.content_summaries` initialization

## Support

For issues or questions:
1. Check this README
2. Review agent logs in console
3. Inspect session state in Streamlit
4. Verify environment variables
5. Check OpenAI API status

## Version History

### v1.0.0 (Current)
- Initial release
- ContentSummaryAgent implementation
- Poor Sentiment Contents page integration
- Session-based caching
- Error handling and retry logic
- Comprehensive UI display