File size: 6,389 Bytes
850182e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Interactive Feedback System for /notes-llm

## Overview

The Interactive Feedback System extends the existing Generator-Validator pattern for `/notes-llm` with user-driven iterative improvements. Users can provide feedback on generated notes, which creates UDF (User Defined Functions) for continuous improvement.

## Architecture

### Core Components

1. **InteractiveFeedbackManager**: Manages feedback sessions and UDF generation
2. **FeedbackData**: Stores user feedback with metadata
3. **InteractiveSession**: Tracks complete feedback session lifecycle
4. **UDF Generation**: Creates functions based on user feedback

### Data Flow

```
User Upload β†’ Initial Generation β†’ Interactive Session Created
       ↓
User Feedback β†’ UDF Generation β†’ Feedback Archived
       ↓
Generate with Feedback β†’ Apply UDFs β†’ New Notes with Changes
       ↓
More Feedback or Approval β†’ Continue Loop or Finalize
```

## API Endpoints

### 1. Initial Generation (Enhanced)
```http
POST /notes-llm
```
- **Purpose**: Generate initial notes and create interactive session
- **Response Headers**:
  - `X-Session-ID`: Unique session identifier
  - `X-Interactive-Enabled`: "true"
  - Standard generation metadata

### 2. Submit Feedback
```http
POST /notes-llm/feedback
```
- **Parameters**:
  - `session_id`: Session identifier
  - `feedback_text`: User's feedback content
  - `feedback_type`: "text", "numeric", "formula", "suggestion"
- **Response**: UDF version and iteration number

### 3. Generate with Feedback
```http
POST /notes-llm/generate
```
- **Parameters**:
  - `session_id`: Session identifier
  - `file`: Updated trial balance file
- **Response**: Notes Excel with applied feedback

### 4. Approve Session
```http
POST /notes-llm/approve
```
- **Purpose**: Finalize session and set final UDF
- **Response**: Approval confirmation with final UDF

### 5. Get Session Info
```http
GET /notes-llm/session/{session_id}
```
- **Purpose**: Retrieve session status and feedback history
- **Response**: Complete session information

## Feedback Types

### 1. Text Feedback
- Natural language suggestions
- Example: "Add more detail to the depreciation notes"

### 2. Numeric Feedback
- Specific numerical adjustments
- Example: "Increase depreciation rate to 15%"

### 3. Formula Feedback
- Excel formulas or calculation suggestions
- Example: "Use formula: =SUM(Depreciation_Year1:Depreciation_Year5)"

### 4. Suggestion Feedback
- General improvement suggestions
- Example: "Group similar expense categories together"

## UDF Generation Process

### 1. Feedback Analysis
```python
def _generate_udf_from_feedback(self, feedback_text, feedback_type, iteration):
    # Analyze feedback content and type
    # Generate appropriate UDF structure
    # Include metadata and version info
```

### 2. UDF Structure
```python
def apply_user_feedback_v{iteration}(notes_data, feedback_type='{feedback_type}'):
    '''
    UDF generated from user feedback iteration {iteration}
    Feedback: {feedback_text}
    Type: {feedback_type}
    Generated: {timestamp}
    '''
    # Apply feedback-based modifications
    return notes_data
```

### 3. UDF Archiving
- All UDFs stored in session
- Versioned by iteration number
- Final UDF set upon approval

## Session Management

### Session States
- **active**: Accepting feedback and generating
- **approved**: Finalized with approved UDF
- **cancelled**: Terminated without approval

### Session Data
```python
{
    "session_id": "uuid-123",
    "status": "active",
    "current_iteration": 2,
    "feedback_history": [...],
    "archived_udfs": [...],
    "final_udf": null
}
```

## Usage Workflow

### Step 1: Initial Generation
```bash
curl -X POST http://localhost:8000/notes-llm \
  -F "file=@trial_balance.xlsx" \
  -o initial_notes.xlsx
```
**Response Headers:**
```
X-Session-ID: abc-123-def-456
X-Interactive-Enabled: true
```

### Step 2: Submit Feedback
```bash
curl -X POST http://localhost:8000/notes-llm/feedback \
  -d "session_id=abc-123-def-456" \
  -d "feedback_text=Add more depreciation details" \
  -d "feedback_type=suggestion"
```

### Step 3: Generate with Feedback
```bash
curl -X POST http://localhost:8000/notes-llm/generate \
  -F "session_id=abc-123-def-456" \
  -F "file=@updated_trial_balance.xlsx" \
  -o improved_notes.xlsx
```

### Step 4: Approve Final Result
```bash
curl -X POST http://localhost:8000/notes-llm/approve \
  -d "session_id=abc-123-def-456"
```

## Benefits

### 1. User-Driven Improvement
- Direct user feedback integration
- Iterative refinement based on actual needs

### 2. Learning System
- UDF functions capture user preferences
- Continuous improvement over time

### 3. Traceability
- Complete audit trail of changes
- Versioned feedback and UDFs

### 4. Flexibility
- Multiple feedback types supported
- Extensible UDF generation system

## File Structure

```
data/
β”œβ”€β”€ interactive_sessions.json    # Session storage
β”œβ”€β”€ input/                       # Uploaded files
β”œβ”€β”€ output/                      # Generated notes
└── udfs/                        # Archived UDF functions
```

## Error Handling

### Common Error Scenarios
- Invalid session ID
- Session already approved
- Feedback submission failures
- UDF generation errors

### Error Responses
```json
{
  "detail": "Session not found",
  "status_code": 404
}
```

## Testing

Run the comprehensive test:
```bash
python test_interactive_feedback.py
```

This will test the complete workflow from initial generation through approval.

## Future Enhancements

1. **Advanced UDF Generation**: Use LLM to create more sophisticated UDFs
2. **Feedback Analytics**: Analyze patterns in user feedback
3. **Batch Processing**: Apply approved UDFs to multiple files
4. **Feedback Templates**: Predefined feedback categories
5. **Collaborative Sessions**: Multiple users on same session</content>
<parameter name="filePath">c:\SAHIL\FinRyver\INTERACTIVE_FEEDBACK_README.md


1. User uploads trial balance β†’ POST /notes-llm
   ↓
2. Initial notes generated + Session created
   ↓
3. User provides feedback β†’ POST /notes-llm/feedback
   ↓
4. UDF generated and archived
   ↓
5. Generate improved notes β†’ POST /notes-llm/generate
   ↓
6. User reviews and provides more feedback (loop)
   ↓
7. User approves final result β†’ POST /notes-llm/approve
   ↓
8. Final UDF set and session completed