File size: 4,519 Bytes
141cc25 | 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 | # Code Routes Documentation
This document describes the API endpoints available for code execution, editing, fixing, and cleaning operations in the Auto-Analyst backend.
## Base URL
All code-related endpoints are prefixed with `/code`.
## Endpoints
### Execute Code
Executes Python code against the current session's dataframe.
**Endpoint:** `POST /code/execute`
**Request Body:**
```json
{
"code": "string", // Python code to execute
"session_id": "string", // Optional session ID
"message_id": 123 // Optional message ID for tracking
}
```
**Response:**
```json
{
"output": "string", // Execution output
"plotly_outputs": [ // Optional array of plotly outputs
"string"
]
}
```
**Error Responses:**
- `400 Bad Request`: No dataset loaded or no code provided
- `500 Internal Server Error`: Execution error
### Edit Code
Uses AI to edit code based on user instructions.
**Endpoint:** `POST /code/edit`
**Request Body:**
```json
{
"original_code": "string", // Code to be edited
"user_prompt": "string" // Instructions for editing
}
```
**Response:**
```json
{
"edited_code": "string" // The edited code
}
```
**Error Responses:**
- `400 Bad Request`: Missing original code or editing instructions
- `500 Internal Server Error`: Editing error
### Fix Code
Uses AI to fix code with errors, employing a block-by-block approach with DSPy refinement.
**Endpoint:** `POST /code/fix`
**Request Body:**
```json
{
"code": "string", // Code containing errors
"error": "string" // Error message to fix
}
```
**Response:**
```json
{
"fixed_code": "string" // The fixed code
}
```
**Error Responses:**
- `400 Bad Request`: Missing code or error message
- `500 Internal Server Error`: Fixing error
### Clean Code
Cleans and formats code by organizing imports and ensuring proper code block formatting.
**Endpoint:** `POST /code/clean-code`
**Request Body:**
```json
{
"code": "string" // Code to clean
}
```
**Response:**
```json
{
"cleaned_code": "string" // The cleaned code
}
```
**Error Responses:**
- `400 Bad Request`: No code provided
- `500 Internal Server Error`: Cleaning error
### Get Latest Code
Retrieves the latest code from a specific message.
**Endpoint:** `POST /code/get-latest-code`
**Request Body:**
```json
{
"message_id": 123 // Message ID to retrieve code from
}
```
**Response:**
```json
{
"code": "string" // The retrieved code
}
```
**Error Responses:**
- `400 Bad Request`: Missing message ID
- `404 Not Found`: Message not found
- `500 Internal Server Error`: Retrieval error
## Code Processing Features
### Import Organization
The code processing system automatically:
- Moves all import statements to the top of the file
- Deduplicates imports
- Sorts imports alphabetically
### Code Block Management
The system supports code blocks marked with special comments:
- Start marker: `# agent_name code start`
- End marker: `# agent_name code end`
### Error Handling with DSPy Refinement
When fixing code, the system uses DSPy's refinement mechanism:
- Identifies specific code blocks with errors
- Processes error messages to extract relevant information
- Uses a scoring function to validate fixes
- Employs iterative refinement with up to 3 attempts
- Fixes each block individually while maintaining the overall structure
- Preserves code block markers and relationships
### Dataset Context
When editing or fixing code, the system provides context about the current dataset including:
- Number of rows and columns
- Column names and data types
- Null value counts
- Sample values for each column
### Code Execution Safety
The execution system includes safety measures:
- Removes blocking calls like `plt.show()`
- Handles `__main__` block extraction
- Cleans up print statements with unwanted newlines
- Executes code in isolated namespaces
## Session Management
All endpoints require a valid session ID, which is used to:
- Access the current dataset
- Maintain state between requests
- Track code execution history
- Store execution results for analysis
## Error Handling
The system provides detailed error messages while maintaining security by:
- Logging errors for debugging
- Returning user-friendly error messages
- Preserving original code in case of processing failures
- Using code scoring to validate fixes before returning results
|