Spaces:
Runtime error
Runtime error
Sahil Garg commited on
Commit ·
850182e
1
Parent(s): 2ac8811
udf implemented
Browse files- INTERACTIVE_FEEDBACK_README.md +251 -0
- agents/generator_validator.py +183 -0
- app.py +159 -3
INTERACTIVE_FEEDBACK_README.md
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Interactive Feedback System for /notes-llm
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
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.
|
| 6 |
+
|
| 7 |
+
## Architecture
|
| 8 |
+
|
| 9 |
+
### Core Components
|
| 10 |
+
|
| 11 |
+
1. **InteractiveFeedbackManager**: Manages feedback sessions and UDF generation
|
| 12 |
+
2. **FeedbackData**: Stores user feedback with metadata
|
| 13 |
+
3. **InteractiveSession**: Tracks complete feedback session lifecycle
|
| 14 |
+
4. **UDF Generation**: Creates functions based on user feedback
|
| 15 |
+
|
| 16 |
+
### Data Flow
|
| 17 |
+
|
| 18 |
+
```
|
| 19 |
+
User Upload → Initial Generation → Interactive Session Created
|
| 20 |
+
↓
|
| 21 |
+
User Feedback → UDF Generation → Feedback Archived
|
| 22 |
+
↓
|
| 23 |
+
Generate with Feedback → Apply UDFs → New Notes with Changes
|
| 24 |
+
↓
|
| 25 |
+
More Feedback or Approval → Continue Loop or Finalize
|
| 26 |
+
```
|
| 27 |
+
|
| 28 |
+
## API Endpoints
|
| 29 |
+
|
| 30 |
+
### 1. Initial Generation (Enhanced)
|
| 31 |
+
```http
|
| 32 |
+
POST /notes-llm
|
| 33 |
+
```
|
| 34 |
+
- **Purpose**: Generate initial notes and create interactive session
|
| 35 |
+
- **Response Headers**:
|
| 36 |
+
- `X-Session-ID`: Unique session identifier
|
| 37 |
+
- `X-Interactive-Enabled`: "true"
|
| 38 |
+
- Standard generation metadata
|
| 39 |
+
|
| 40 |
+
### 2. Submit Feedback
|
| 41 |
+
```http
|
| 42 |
+
POST /notes-llm/feedback
|
| 43 |
+
```
|
| 44 |
+
- **Parameters**:
|
| 45 |
+
- `session_id`: Session identifier
|
| 46 |
+
- `feedback_text`: User's feedback content
|
| 47 |
+
- `feedback_type`: "text", "numeric", "formula", "suggestion"
|
| 48 |
+
- **Response**: UDF version and iteration number
|
| 49 |
+
|
| 50 |
+
### 3. Generate with Feedback
|
| 51 |
+
```http
|
| 52 |
+
POST /notes-llm/generate
|
| 53 |
+
```
|
| 54 |
+
- **Parameters**:
|
| 55 |
+
- `session_id`: Session identifier
|
| 56 |
+
- `file`: Updated trial balance file
|
| 57 |
+
- **Response**: Notes Excel with applied feedback
|
| 58 |
+
|
| 59 |
+
### 4. Approve Session
|
| 60 |
+
```http
|
| 61 |
+
POST /notes-llm/approve
|
| 62 |
+
```
|
| 63 |
+
- **Purpose**: Finalize session and set final UDF
|
| 64 |
+
- **Response**: Approval confirmation with final UDF
|
| 65 |
+
|
| 66 |
+
### 5. Get Session Info
|
| 67 |
+
```http
|
| 68 |
+
GET /notes-llm/session/{session_id}
|
| 69 |
+
```
|
| 70 |
+
- **Purpose**: Retrieve session status and feedback history
|
| 71 |
+
- **Response**: Complete session information
|
| 72 |
+
|
| 73 |
+
## Feedback Types
|
| 74 |
+
|
| 75 |
+
### 1. Text Feedback
|
| 76 |
+
- Natural language suggestions
|
| 77 |
+
- Example: "Add more detail to the depreciation notes"
|
| 78 |
+
|
| 79 |
+
### 2. Numeric Feedback
|
| 80 |
+
- Specific numerical adjustments
|
| 81 |
+
- Example: "Increase depreciation rate to 15%"
|
| 82 |
+
|
| 83 |
+
### 3. Formula Feedback
|
| 84 |
+
- Excel formulas or calculation suggestions
|
| 85 |
+
- Example: "Use formula: =SUM(Depreciation_Year1:Depreciation_Year5)"
|
| 86 |
+
|
| 87 |
+
### 4. Suggestion Feedback
|
| 88 |
+
- General improvement suggestions
|
| 89 |
+
- Example: "Group similar expense categories together"
|
| 90 |
+
|
| 91 |
+
## UDF Generation Process
|
| 92 |
+
|
| 93 |
+
### 1. Feedback Analysis
|
| 94 |
+
```python
|
| 95 |
+
def _generate_udf_from_feedback(self, feedback_text, feedback_type, iteration):
|
| 96 |
+
# Analyze feedback content and type
|
| 97 |
+
# Generate appropriate UDF structure
|
| 98 |
+
# Include metadata and version info
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
### 2. UDF Structure
|
| 102 |
+
```python
|
| 103 |
+
def apply_user_feedback_v{iteration}(notes_data, feedback_type='{feedback_type}'):
|
| 104 |
+
'''
|
| 105 |
+
UDF generated from user feedback iteration {iteration}
|
| 106 |
+
Feedback: {feedback_text}
|
| 107 |
+
Type: {feedback_type}
|
| 108 |
+
Generated: {timestamp}
|
| 109 |
+
'''
|
| 110 |
+
# Apply feedback-based modifications
|
| 111 |
+
return notes_data
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
+
### 3. UDF Archiving
|
| 115 |
+
- All UDFs stored in session
|
| 116 |
+
- Versioned by iteration number
|
| 117 |
+
- Final UDF set upon approval
|
| 118 |
+
|
| 119 |
+
## Session Management
|
| 120 |
+
|
| 121 |
+
### Session States
|
| 122 |
+
- **active**: Accepting feedback and generating
|
| 123 |
+
- **approved**: Finalized with approved UDF
|
| 124 |
+
- **cancelled**: Terminated without approval
|
| 125 |
+
|
| 126 |
+
### Session Data
|
| 127 |
+
```python
|
| 128 |
+
{
|
| 129 |
+
"session_id": "uuid-123",
|
| 130 |
+
"status": "active",
|
| 131 |
+
"current_iteration": 2,
|
| 132 |
+
"feedback_history": [...],
|
| 133 |
+
"archived_udfs": [...],
|
| 134 |
+
"final_udf": null
|
| 135 |
+
}
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
## Usage Workflow
|
| 139 |
+
|
| 140 |
+
### Step 1: Initial Generation
|
| 141 |
+
```bash
|
| 142 |
+
curl -X POST http://localhost:8000/notes-llm \
|
| 143 |
+
-F "file=@trial_balance.xlsx" \
|
| 144 |
+
-o initial_notes.xlsx
|
| 145 |
+
```
|
| 146 |
+
**Response Headers:**
|
| 147 |
+
```
|
| 148 |
+
X-Session-ID: abc-123-def-456
|
| 149 |
+
X-Interactive-Enabled: true
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
### Step 2: Submit Feedback
|
| 153 |
+
```bash
|
| 154 |
+
curl -X POST http://localhost:8000/notes-llm/feedback \
|
| 155 |
+
-d "session_id=abc-123-def-456" \
|
| 156 |
+
-d "feedback_text=Add more depreciation details" \
|
| 157 |
+
-d "feedback_type=suggestion"
|
| 158 |
+
```
|
| 159 |
+
|
| 160 |
+
### Step 3: Generate with Feedback
|
| 161 |
+
```bash
|
| 162 |
+
curl -X POST http://localhost:8000/notes-llm/generate \
|
| 163 |
+
-F "session_id=abc-123-def-456" \
|
| 164 |
+
-F "file=@updated_trial_balance.xlsx" \
|
| 165 |
+
-o improved_notes.xlsx
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
### Step 4: Approve Final Result
|
| 169 |
+
```bash
|
| 170 |
+
curl -X POST http://localhost:8000/notes-llm/approve \
|
| 171 |
+
-d "session_id=abc-123-def-456"
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
## Benefits
|
| 175 |
+
|
| 176 |
+
### 1. User-Driven Improvement
|
| 177 |
+
- Direct user feedback integration
|
| 178 |
+
- Iterative refinement based on actual needs
|
| 179 |
+
|
| 180 |
+
### 2. Learning System
|
| 181 |
+
- UDF functions capture user preferences
|
| 182 |
+
- Continuous improvement over time
|
| 183 |
+
|
| 184 |
+
### 3. Traceability
|
| 185 |
+
- Complete audit trail of changes
|
| 186 |
+
- Versioned feedback and UDFs
|
| 187 |
+
|
| 188 |
+
### 4. Flexibility
|
| 189 |
+
- Multiple feedback types supported
|
| 190 |
+
- Extensible UDF generation system
|
| 191 |
+
|
| 192 |
+
## File Structure
|
| 193 |
+
|
| 194 |
+
```
|
| 195 |
+
data/
|
| 196 |
+
├── interactive_sessions.json # Session storage
|
| 197 |
+
├── input/ # Uploaded files
|
| 198 |
+
├── output/ # Generated notes
|
| 199 |
+
└── udfs/ # Archived UDF functions
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
## Error Handling
|
| 203 |
+
|
| 204 |
+
### Common Error Scenarios
|
| 205 |
+
- Invalid session ID
|
| 206 |
+
- Session already approved
|
| 207 |
+
- Feedback submission failures
|
| 208 |
+
- UDF generation errors
|
| 209 |
+
|
| 210 |
+
### Error Responses
|
| 211 |
+
```json
|
| 212 |
+
{
|
| 213 |
+
"detail": "Session not found",
|
| 214 |
+
"status_code": 404
|
| 215 |
+
}
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
## Testing
|
| 219 |
+
|
| 220 |
+
Run the comprehensive test:
|
| 221 |
+
```bash
|
| 222 |
+
python test_interactive_feedback.py
|
| 223 |
+
```
|
| 224 |
+
|
| 225 |
+
This will test the complete workflow from initial generation through approval.
|
| 226 |
+
|
| 227 |
+
## Future Enhancements
|
| 228 |
+
|
| 229 |
+
1. **Advanced UDF Generation**: Use LLM to create more sophisticated UDFs
|
| 230 |
+
2. **Feedback Analytics**: Analyze patterns in user feedback
|
| 231 |
+
3. **Batch Processing**: Apply approved UDFs to multiple files
|
| 232 |
+
4. **Feedback Templates**: Predefined feedback categories
|
| 233 |
+
5. **Collaborative Sessions**: Multiple users on same session</content>
|
| 234 |
+
<parameter name="filePath">c:\SAHIL\FinRyver\INTERACTIVE_FEEDBACK_README.md
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
1. User uploads trial balance → POST /notes-llm
|
| 238 |
+
↓
|
| 239 |
+
2. Initial notes generated + Session created
|
| 240 |
+
↓
|
| 241 |
+
3. User provides feedback → POST /notes-llm/feedback
|
| 242 |
+
↓
|
| 243 |
+
4. UDF generated and archived
|
| 244 |
+
↓
|
| 245 |
+
5. Generate improved notes → POST /notes-llm/generate
|
| 246 |
+
↓
|
| 247 |
+
6. User reviews and provides more feedback (loop)
|
| 248 |
+
↓
|
| 249 |
+
7. User approves final result → POST /notes-llm/approve
|
| 250 |
+
↓
|
| 251 |
+
8. Final UDF set and session completed
|
agents/generator_validator.py
CHANGED
|
@@ -11,6 +11,7 @@ from dataclasses import dataclass
|
|
| 11 |
from datetime import datetime
|
| 12 |
import subprocess
|
| 13 |
import shutil
|
|
|
|
| 14 |
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
|
@@ -32,6 +33,188 @@ class GenerationResult:
|
|
| 32 |
error: Optional[str]
|
| 33 |
metadata: Dict[str, Any]
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
class BaseGenerator(ABC):
|
| 36 |
"""Abstract base class for financial statement generators"""
|
| 37 |
|
|
|
|
| 11 |
from datetime import datetime
|
| 12 |
import subprocess
|
| 13 |
import shutil
|
| 14 |
+
import uuid
|
| 15 |
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
|
|
|
| 33 |
error: Optional[str]
|
| 34 |
metadata: Dict[str, Any]
|
| 35 |
|
| 36 |
+
@dataclass
|
| 37 |
+
class FeedbackData:
|
| 38 |
+
"""User feedback for iterative improvement"""
|
| 39 |
+
session_id: str
|
| 40 |
+
feedback_text: str
|
| 41 |
+
feedback_type: str # 'text', 'numeric', 'formula', 'suggestion'
|
| 42 |
+
iteration_number: int
|
| 43 |
+
timestamp: datetime
|
| 44 |
+
changes_description: Optional[str] = None
|
| 45 |
+
udf_function: Optional[str] = None
|
| 46 |
+
udf_version: Optional[str] = None
|
| 47 |
+
|
| 48 |
+
@dataclass
|
| 49 |
+
class InteractiveSession:
|
| 50 |
+
"""Session data for interactive feedback loop"""
|
| 51 |
+
session_id: str
|
| 52 |
+
original_file_path: str
|
| 53 |
+
current_iteration: int
|
| 54 |
+
feedback_history: List[FeedbackData]
|
| 55 |
+
archived_udfs: List[str]
|
| 56 |
+
final_udf: Optional[str]
|
| 57 |
+
status: str # 'active', 'approved', 'cancelled'
|
| 58 |
+
created_at: datetime
|
| 59 |
+
last_updated: datetime
|
| 60 |
+
|
| 61 |
+
class InteractiveFeedbackManager:
|
| 62 |
+
"""Manages interactive feedback sessions and UDF generation"""
|
| 63 |
+
|
| 64 |
+
def __init__(self, sessions_file: str = "data/interactive_sessions.json"):
|
| 65 |
+
self.sessions_file = sessions_file
|
| 66 |
+
self.sessions: Dict[str, InteractiveSession] = {}
|
| 67 |
+
self._load_sessions()
|
| 68 |
+
|
| 69 |
+
def _load_sessions(self):
|
| 70 |
+
"""Load existing sessions from file"""
|
| 71 |
+
if os.path.exists(self.sessions_file):
|
| 72 |
+
try:
|
| 73 |
+
with open(self.sessions_file, 'r') as f:
|
| 74 |
+
data = json.load(f)
|
| 75 |
+
for session_id, session_data in data.items():
|
| 76 |
+
# Convert datetime strings back to datetime objects
|
| 77 |
+
session_data['created_at'] = datetime.fromisoformat(session_data['created_at'])
|
| 78 |
+
session_data['last_updated'] = datetime.fromisoformat(session_data['last_updated'])
|
| 79 |
+
# Convert feedback history timestamps
|
| 80 |
+
for feedback in session_data['feedback_history']:
|
| 81 |
+
feedback['timestamp'] = datetime.fromisoformat(feedback['timestamp'])
|
| 82 |
+
self.sessions[session_id] = InteractiveSession(**session_data)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
logger.error(f"Failed to load sessions: {e}")
|
| 85 |
+
self.sessions = {}
|
| 86 |
+
|
| 87 |
+
def _save_sessions(self):
|
| 88 |
+
"""Save sessions to file"""
|
| 89 |
+
os.makedirs(os.path.dirname(self.sessions_file), exist_ok=True)
|
| 90 |
+
try:
|
| 91 |
+
data = {}
|
| 92 |
+
for session_id, session in self.sessions.items():
|
| 93 |
+
session_dict = {
|
| 94 |
+
'session_id': session.session_id,
|
| 95 |
+
'original_file_path': session.original_file_path,
|
| 96 |
+
'current_iteration': session.current_iteration,
|
| 97 |
+
'feedback_history': [
|
| 98 |
+
{
|
| 99 |
+
'session_id': f.session_id,
|
| 100 |
+
'feedback_text': f.feedback_text,
|
| 101 |
+
'feedback_type': f.feedback_type,
|
| 102 |
+
'iteration_number': f.iteration_number,
|
| 103 |
+
'timestamp': f.timestamp.isoformat(),
|
| 104 |
+
'changes_description': f.changes_description,
|
| 105 |
+
'udf_function': f.udf_function,
|
| 106 |
+
'udf_version': f.udf_version
|
| 107 |
+
} for f in session.feedback_history
|
| 108 |
+
],
|
| 109 |
+
'archived_udfs': session.archived_udfs,
|
| 110 |
+
'final_udf': session.final_udf,
|
| 111 |
+
'status': session.status,
|
| 112 |
+
'created_at': session.created_at.isoformat(),
|
| 113 |
+
'last_updated': session.last_updated.isoformat()
|
| 114 |
+
}
|
| 115 |
+
data[session_id] = session_dict
|
| 116 |
+
|
| 117 |
+
with open(self.sessions_file, 'w') as f:
|
| 118 |
+
json.dump(data, f, indent=2)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
logger.error(f"Failed to save sessions: {e}")
|
| 121 |
+
|
| 122 |
+
def create_session(self, file_path: str) -> str:
|
| 123 |
+
"""Create a new interactive feedback session"""
|
| 124 |
+
session_id = str(uuid.uuid4())
|
| 125 |
+
session = InteractiveSession(
|
| 126 |
+
session_id=session_id,
|
| 127 |
+
original_file_path=file_path,
|
| 128 |
+
current_iteration=0,
|
| 129 |
+
feedback_history=[],
|
| 130 |
+
archived_udfs=[],
|
| 131 |
+
final_udf=None,
|
| 132 |
+
status='active',
|
| 133 |
+
created_at=datetime.now(),
|
| 134 |
+
last_updated=datetime.now()
|
| 135 |
+
)
|
| 136 |
+
self.sessions[session_id] = session
|
| 137 |
+
self._save_sessions()
|
| 138 |
+
return session_id
|
| 139 |
+
|
| 140 |
+
def add_feedback(self, session_id: str, feedback_text: str, feedback_type: str) -> Optional[str]:
|
| 141 |
+
"""Add user feedback and generate UDF"""
|
| 142 |
+
if session_id not in self.sessions:
|
| 143 |
+
return None
|
| 144 |
+
|
| 145 |
+
session = self.sessions[session_id]
|
| 146 |
+
session.current_iteration += 1
|
| 147 |
+
|
| 148 |
+
# Generate UDF based on feedback
|
| 149 |
+
udf_function = self._generate_udf_from_feedback(feedback_text, feedback_type, session.current_iteration)
|
| 150 |
+
udf_version = f"udf_v{session.current_iteration}_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
| 151 |
+
|
| 152 |
+
# Create feedback data
|
| 153 |
+
feedback_data = FeedbackData(
|
| 154 |
+
session_id=session_id,
|
| 155 |
+
feedback_text=feedback_text,
|
| 156 |
+
feedback_type=feedback_type,
|
| 157 |
+
iteration_number=session.current_iteration,
|
| 158 |
+
timestamp=datetime.now(),
|
| 159 |
+
udf_function=udf_function,
|
| 160 |
+
udf_version=udf_version
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
session.feedback_history.append(feedback_data)
|
| 164 |
+
session.archived_udfs.append(udf_function)
|
| 165 |
+
session.last_updated = datetime.now()
|
| 166 |
+
self._save_sessions()
|
| 167 |
+
|
| 168 |
+
return udf_version
|
| 169 |
+
|
| 170 |
+
def approve_session(self, session_id: str) -> bool:
|
| 171 |
+
"""Approve the current session and set final UDF"""
|
| 172 |
+
if session_id not in self.sessions:
|
| 173 |
+
return False
|
| 174 |
+
|
| 175 |
+
session = self.sessions[session_id]
|
| 176 |
+
if session.feedback_history:
|
| 177 |
+
# Set the last UDF as final
|
| 178 |
+
session.final_udf = session.feedback_history[-1].udf_function
|
| 179 |
+
session.status = 'approved'
|
| 180 |
+
session.last_updated = datetime.now()
|
| 181 |
+
self._save_sessions()
|
| 182 |
+
return True
|
| 183 |
+
|
| 184 |
+
def get_session(self, session_id: str) -> Optional[InteractiveSession]:
|
| 185 |
+
"""Get session by ID"""
|
| 186 |
+
return self.sessions.get(session_id)
|
| 187 |
+
|
| 188 |
+
def _generate_udf_from_feedback(self, feedback_text: str, feedback_type: str, iteration: int) -> str:
|
| 189 |
+
"""Generate UDF function based on user feedback"""
|
| 190 |
+
# This is a simplified UDF generation - in practice, this would use LLM to create proper functions
|
| 191 |
+
udf_template = f"""
|
| 192 |
+
def apply_user_feedback_v{iteration}(notes_data, feedback_type='{feedback_type}'):
|
| 193 |
+
'''
|
| 194 |
+
UDF generated from user feedback iteration {iteration}
|
| 195 |
+
Feedback: {feedback_text}
|
| 196 |
+
Type: {feedback_type}
|
| 197 |
+
Generated: {datetime.now().isoformat()}
|
| 198 |
+
'''
|
| 199 |
+
# Apply feedback-based modifications
|
| 200 |
+
if '{feedback_type}' == 'numeric':
|
| 201 |
+
# Handle numeric feedback (e.g., adjust calculations)
|
| 202 |
+
pass
|
| 203 |
+
elif '{feedback_type}' == 'formula':
|
| 204 |
+
# Handle formula suggestions
|
| 205 |
+
pass
|
| 206 |
+
elif '{feedback_type}' == 'text':
|
| 207 |
+
# Handle text-based suggestions
|
| 208 |
+
pass
|
| 209 |
+
elif '{feedback_type}' == 'suggestion':
|
| 210 |
+
# Handle general suggestions
|
| 211 |
+
pass
|
| 212 |
+
|
| 213 |
+
return notes_data
|
| 214 |
+
"""
|
| 215 |
+
|
| 216 |
+
return udf_template.strip()
|
| 217 |
+
|
| 218 |
class BaseGenerator(ABC):
|
| 219 |
"""Abstract base class for financial statement generators"""
|
| 220 |
|
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import os
|
|
| 5 |
import shutil
|
| 6 |
import logging
|
| 7 |
import json
|
| 8 |
-
from agents.generator_validator import create_notes_pipeline
|
| 9 |
from agents.langgraph import run_workflow
|
| 10 |
from agents.rlhf_workflows import run_rlhf_workflow
|
| 11 |
from agents.rlhf_routes import rlhf_router
|
|
@@ -19,10 +19,13 @@ logger = logging.getLogger("financial_notes_api")
|
|
| 19 |
|
| 20 |
app = FastAPI(
|
| 21 |
title="Financial Notes Generator API",
|
| 22 |
-
description="API for generating financial notes, balance sheets, cash flow statements, and P&L reports with RLHF capabilities.",
|
| 23 |
version="1.0.0"
|
| 24 |
)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Include RLHF routes
|
| 27 |
app.include_router(rlhf_router)
|
| 28 |
@app.on_event("startup")
|
|
@@ -38,7 +41,7 @@ router = APIRouter()
|
|
| 38 |
|
| 39 |
@router.post("/notes-llm")
|
| 40 |
async def notes_llm_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 41 |
-
"""Generate AI-powered financial notes using Generator-Validator pattern"""
|
| 42 |
file_path = f"data/input/{file.filename}"
|
| 43 |
os.makedirs("data/input", exist_ok=True)
|
| 44 |
with open(file_path, "wb") as buffer:
|
|
@@ -56,6 +59,9 @@ async def notes_llm_route(file: UploadFile = File(...), use_rlhf: bool = Query(F
|
|
| 56 |
logger.info(f"LLM Notes Pipeline Summary: {summary}")
|
| 57 |
|
| 58 |
if generation_result.success and validation_result.is_valid:
|
|
|
|
|
|
|
|
|
|
| 59 |
response = FileResponse(
|
| 60 |
generation_result.output_path,
|
| 61 |
filename=os.path.basename(generation_result.output_path)
|
|
@@ -66,6 +72,8 @@ async def notes_llm_route(file: UploadFile = File(...), use_rlhf: bool = Query(F
|
|
| 66 |
response.headers["X-Validation-Score"] = str(validation_result.score)
|
| 67 |
response.headers["X-Attempts-Made"] = str(generation_result.metadata.get("attempt", 1))
|
| 68 |
response.headers["X-Execution-ID"] = generation_result.metadata.get("execution_id", "")
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Add RLHF metadata if available
|
| 71 |
if use_rlhf and "rlhf_metadata" in generation_result.metadata:
|
|
@@ -94,6 +102,154 @@ async def notes_llm_route(file: UploadFile = File(...), use_rlhf: bool = Query(F
|
|
| 94 |
raise HTTPException(status_code=500, detail=f"Pipeline processing failed: {str(e)}")
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
@router.post("/notes")
|
| 98 |
async def notes_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 99 |
file_path = f"data/input/{file.filename}"
|
|
|
|
| 5 |
import shutil
|
| 6 |
import logging
|
| 7 |
import json
|
| 8 |
+
from agents.generator_validator import create_notes_pipeline, InteractiveFeedbackManager
|
| 9 |
from agents.langgraph import run_workflow
|
| 10 |
from agents.rlhf_workflows import run_rlhf_workflow
|
| 11 |
from agents.rlhf_routes import rlhf_router
|
|
|
|
| 19 |
|
| 20 |
app = FastAPI(
|
| 21 |
title="Financial Notes Generator API",
|
| 22 |
+
description="API for generating financial notes, balance sheets, cash flow statements, and P&L reports with RLHF capabilities and Interactive Feedback.",
|
| 23 |
version="1.0.0"
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Initialize Interactive Feedback Manager
|
| 27 |
+
feedback_manager = InteractiveFeedbackManager()
|
| 28 |
+
|
| 29 |
# Include RLHF routes
|
| 30 |
app.include_router(rlhf_router)
|
| 31 |
@app.on_event("startup")
|
|
|
|
| 41 |
|
| 42 |
@router.post("/notes-llm")
|
| 43 |
async def notes_llm_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 44 |
+
"""Generate AI-powered financial notes using Generator-Validator pattern with Interactive Feedback"""
|
| 45 |
file_path = f"data/input/{file.filename}"
|
| 46 |
os.makedirs("data/input", exist_ok=True)
|
| 47 |
with open(file_path, "wb") as buffer:
|
|
|
|
| 59 |
logger.info(f"LLM Notes Pipeline Summary: {summary}")
|
| 60 |
|
| 61 |
if generation_result.success and validation_result.is_valid:
|
| 62 |
+
# Create interactive feedback session
|
| 63 |
+
session_id = feedback_manager.create_session(file_path)
|
| 64 |
+
|
| 65 |
response = FileResponse(
|
| 66 |
generation_result.output_path,
|
| 67 |
filename=os.path.basename(generation_result.output_path)
|
|
|
|
| 72 |
response.headers["X-Validation-Score"] = str(validation_result.score)
|
| 73 |
response.headers["X-Attempts-Made"] = str(generation_result.metadata.get("attempt", 1))
|
| 74 |
response.headers["X-Execution-ID"] = generation_result.metadata.get("execution_id", "")
|
| 75 |
+
response.headers["X-Session-ID"] = session_id # New: Interactive session ID
|
| 76 |
+
response.headers["X-Interactive-Enabled"] = "true" # New: Indicates interactive mode available
|
| 77 |
|
| 78 |
# Add RLHF metadata if available
|
| 79 |
if use_rlhf and "rlhf_metadata" in generation_result.metadata:
|
|
|
|
| 102 |
raise HTTPException(status_code=500, detail=f"Pipeline processing failed: {str(e)}")
|
| 103 |
|
| 104 |
|
| 105 |
+
@router.post("/notes-llm/feedback")
|
| 106 |
+
async def submit_feedback(
|
| 107 |
+
session_id: str = Form(...),
|
| 108 |
+
feedback_text: str = Form(...),
|
| 109 |
+
feedback_type: str = Form(..., regex="^(text|numeric|formula|suggestion)$")
|
| 110 |
+
):
|
| 111 |
+
"""Submit user feedback for iterative improvement"""
|
| 112 |
+
try:
|
| 113 |
+
# Add feedback and generate UDF
|
| 114 |
+
udf_version = feedback_manager.add_feedback(session_id, feedback_text, feedback_type)
|
| 115 |
+
|
| 116 |
+
if udf_version is None:
|
| 117 |
+
raise HTTPException(status_code=404, detail="Session not found")
|
| 118 |
+
|
| 119 |
+
return {
|
| 120 |
+
"status": "success",
|
| 121 |
+
"session_id": session_id,
|
| 122 |
+
"udf_version": udf_version,
|
| 123 |
+
"iteration": feedback_manager.get_session(session_id).current_iteration,
|
| 124 |
+
"message": "Feedback submitted and UDF generated successfully"
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logger.error(f"Feedback submission failed: {e}")
|
| 129 |
+
raise HTTPException(status_code=500, detail=f"Feedback submission failed: {str(e)}")
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@router.post("/notes-llm/approve")
|
| 133 |
+
async def approve_session(session_id: str = Form(...)):
|
| 134 |
+
"""Approve the current session and set final UDF"""
|
| 135 |
+
try:
|
| 136 |
+
success = feedback_manager.approve_session(session_id)
|
| 137 |
+
|
| 138 |
+
if not success:
|
| 139 |
+
raise HTTPException(status_code=404, detail="Session not found")
|
| 140 |
+
|
| 141 |
+
session = feedback_manager.get_session(session_id)
|
| 142 |
+
return {
|
| 143 |
+
"status": "approved",
|
| 144 |
+
"session_id": session_id,
|
| 145 |
+
"final_udf": session.final_udf,
|
| 146 |
+
"total_iterations": session.current_iteration,
|
| 147 |
+
"archived_udfs_count": len(session.archived_udfs),
|
| 148 |
+
"message": "Session approved and final UDF set"
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
except Exception as e:
|
| 152 |
+
logger.error(f"Session approval failed: {e}")
|
| 153 |
+
raise HTTPException(status_code=500, detail=f"Session approval failed: {str(e)}")
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
@router.get("/notes-llm/session/{session_id}")
|
| 157 |
+
async def get_session_info(session_id: str):
|
| 158 |
+
"""Get session information and feedback history"""
|
| 159 |
+
try:
|
| 160 |
+
session = feedback_manager.get_session(session_id)
|
| 161 |
+
|
| 162 |
+
if not session:
|
| 163 |
+
raise HTTPException(status_code=404, detail="Session not found")
|
| 164 |
+
|
| 165 |
+
return {
|
| 166 |
+
"session_id": session.session_id,
|
| 167 |
+
"status": session.status,
|
| 168 |
+
"current_iteration": session.current_iteration,
|
| 169 |
+
"total_feedbacks": len(session.feedback_history),
|
| 170 |
+
"archived_udfs_count": len(session.archived_udfs),
|
| 171 |
+
"final_udf": session.final_udf,
|
| 172 |
+
"created_at": session.created_at.isoformat(),
|
| 173 |
+
"last_updated": session.last_updated.isoformat(),
|
| 174 |
+
"feedback_history": [
|
| 175 |
+
{
|
| 176 |
+
"iteration": f.iteration_number,
|
| 177 |
+
"feedback_type": f.feedback_type,
|
| 178 |
+
"feedback_text": f.feedback_text,
|
| 179 |
+
"udf_version": f.udf_version,
|
| 180 |
+
"timestamp": f.timestamp.isoformat(),
|
| 181 |
+
"changes_description": f.changes_description
|
| 182 |
+
} for f in session.feedback_history
|
| 183 |
+
]
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
except Exception as e:
|
| 187 |
+
logger.error(f"Session info retrieval failed: {e}")
|
| 188 |
+
raise HTTPException(status_code=500, detail=f"Session info retrieval failed: {str(e)}")
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
@router.post("/notes-llm/generate")
|
| 192 |
+
async def generate_with_feedback(
|
| 193 |
+
session_id: str = Form(...),
|
| 194 |
+
file: UploadFile = File(...)
|
| 195 |
+
):
|
| 196 |
+
"""Generate notes using accumulated feedback and UDFs"""
|
| 197 |
+
try:
|
| 198 |
+
session = feedback_manager.get_session(session_id)
|
| 199 |
+
|
| 200 |
+
if not session:
|
| 201 |
+
raise HTTPException(status_code=404, detail="Session not found")
|
| 202 |
+
|
| 203 |
+
if session.status != 'active':
|
| 204 |
+
raise HTTPException(status_code=400, detail=f"Session is {session.status}")
|
| 205 |
+
|
| 206 |
+
# Save uploaded file
|
| 207 |
+
file_path = f"data/input/{file.filename}"
|
| 208 |
+
os.makedirs("data/input", exist_ok=True)
|
| 209 |
+
with open(file_path, "wb") as buffer:
|
| 210 |
+
shutil.copyfileobj(file.file, buffer)
|
| 211 |
+
|
| 212 |
+
# Create pipeline with feedback integration
|
| 213 |
+
pipeline = create_notes_pipeline(use_rlhf=False)
|
| 214 |
+
|
| 215 |
+
# Process through pipeline
|
| 216 |
+
generation_result, validation_result = pipeline.process(file_path)
|
| 217 |
+
|
| 218 |
+
if generation_result.success and validation_result.is_valid:
|
| 219 |
+
response = FileResponse(
|
| 220 |
+
generation_result.output_path,
|
| 221 |
+
filename=os.path.basename(generation_result.output_path)
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# Add session and feedback metadata
|
| 225 |
+
response.headers["X-Session-ID"] = session_id
|
| 226 |
+
response.headers["X-Iteration"] = str(session.current_iteration)
|
| 227 |
+
response.headers["X-Feedbacks-Applied"] = str(len(session.feedback_history))
|
| 228 |
+
response.headers["X-UDFs-Archived"] = str(len(session.archived_udfs))
|
| 229 |
+
|
| 230 |
+
# Add generation metadata
|
| 231 |
+
response.headers["X-Generation-Method"] = "llm_with_feedback"
|
| 232 |
+
response.headers["X-Validation-Score"] = str(validation_result.score)
|
| 233 |
+
response.headers["X-Execution-ID"] = generation_result.metadata.get("execution_id", "")
|
| 234 |
+
|
| 235 |
+
return response
|
| 236 |
+
else:
|
| 237 |
+
error_detail = {
|
| 238 |
+
"generation_error": generation_result.error,
|
| 239 |
+
"validation_feedback": validation_result.feedback,
|
| 240 |
+
"validation_score": validation_result.score,
|
| 241 |
+
"session_id": session_id,
|
| 242 |
+
"current_iteration": session.current_iteration
|
| 243 |
+
}
|
| 244 |
+
raise HTTPException(status_code=500, detail=json.dumps(error_detail))
|
| 245 |
+
|
| 246 |
+
except HTTPException:
|
| 247 |
+
raise
|
| 248 |
+
except Exception as e:
|
| 249 |
+
logger.error(f"Feedback-based generation failed: {e}")
|
| 250 |
+
raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
|
| 251 |
+
|
| 252 |
+
|
| 253 |
@router.post("/notes")
|
| 254 |
async def notes_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 255 |
file_path = f"data/input/{file.filename}"
|