added together ai agent
Browse files- python_code_executor_service.py +92 -39
python_code_executor_service.py
CHANGED
@@ -160,51 +160,104 @@ class PythonExecutor:
|
|
160 |
# Return a dummy URL
|
161 |
return f"https://example.com/charts/{filename}"
|
162 |
|
163 |
-
def process_response(self, response: CsvChatResult) -> str:
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
# Return a dummy URL
|
161 |
return f"https://example.com/charts/{filename}"
|
162 |
|
163 |
+
# def process_response(self, response: CsvChatResult) -> str:
|
164 |
+
# """
|
165 |
+
# Process the CsvChatResult response and generate formatted output
|
166 |
|
167 |
+
# Args:
|
168 |
+
# response (CsvChatResult): Response from CSV analysis
|
169 |
|
170 |
+
# Returns:
|
171 |
+
# str: Formatted output with results and dummy image URLs
|
172 |
+
# """
|
173 |
+
# output_parts = []
|
174 |
|
175 |
+
# # Add casual response
|
176 |
+
# output_parts.append(response.casual_response)
|
177 |
|
178 |
+
# # Process analysis operations
|
179 |
+
# for operation in response.analysis_operations:
|
180 |
+
# # Execute the code
|
181 |
+
# result = self.execute_code(operation.code.code)
|
182 |
|
183 |
+
# # Add operation description
|
184 |
+
# output_parts.append(f"\n{operation.description}:")
|
185 |
|
186 |
+
# # Add output or error
|
187 |
+
# if result['error']:
|
188 |
+
# output_parts.append(f"Error: {result['error']['message']}")
|
189 |
+
# else:
|
190 |
+
# output_parts.append(result['output'].strip())
|
191 |
|
192 |
+
# # Process charts if they exist
|
193 |
+
# if response.charts:
|
194 |
+
# output_parts.append("\nVisualizations:")
|
195 |
|
196 |
+
# for chart in response.charts:
|
197 |
+
# if chart.code:
|
198 |
+
# # Execute the chart code
|
199 |
+
# result = self.execute_code(chart.code)
|
200 |
|
201 |
+
# if result['plots']:
|
202 |
+
# # Save each generated plot and get dummy URL
|
203 |
+
# for plot_data in result['plots']:
|
204 |
+
# dummy_url = self.save_plot_dummy(plot_data, chart.image_description)
|
205 |
+
# output_parts.append(f"\n{chart.image_description}")
|
206 |
+
# output_parts.append(f"")
|
207 |
+
# elif result['error']:
|
208 |
+
# output_parts.append(f"\nError generating {chart.image_description}: {result['error']['message']}")
|
209 |
+
|
210 |
+
# return "\n".join(output_parts)
|
211 |
+
|
212 |
+
def process_response(self, response: CsvChatResult) -> str:
|
213 |
+
"""
|
214 |
+
Process the CsvChatResult response and generate formatted output
|
215 |
+
with markdown code blocks for structured data.
|
216 |
+
"""
|
217 |
+
output_parts = []
|
218 |
+
|
219 |
+
# Add casual response
|
220 |
+
output_parts.append(response.casual_response)
|
221 |
+
|
222 |
+
# Process analysis operations
|
223 |
+
for operation in response.analysis_operations:
|
224 |
+
# Execute the code
|
225 |
+
result = self.execute_code(operation.code.code)
|
226 |
+
|
227 |
+
# Add operation description
|
228 |
+
output_parts.append(f"\n{operation.description}:")
|
229 |
+
|
230 |
+
# Add output or error with markdown wrapping
|
231 |
+
if result['error']:
|
232 |
+
output_parts.append("```python\n" + f"Error: {result['error']['message']}" + "\n```")
|
233 |
+
else:
|
234 |
+
output = result['output'].strip()
|
235 |
+
if self._looks_like_structured_data(output): # New helper method
|
236 |
+
output_parts.append("```python\n" + output + "\n```")
|
237 |
+
else:
|
238 |
+
output_parts.append(output)
|
239 |
+
|
240 |
+
# Process charts remains the same
|
241 |
+
if response.charts:
|
242 |
+
output_parts.append("\nVisualizations:")
|
243 |
+
for chart in response.charts:
|
244 |
+
if chart.code:
|
245 |
+
result = self.execute_code(chart.code)
|
246 |
+
if result['plots']:
|
247 |
+
for plot_data in result['plots']:
|
248 |
+
dummy_url = self.save_plot_dummy(plot_data, chart.image_description)
|
249 |
+
output_parts.append(f"\n{chart.image_description}")
|
250 |
+
output_parts.append(f"")
|
251 |
+
elif result['error']:
|
252 |
+
output_parts.append("```python\n" + f"Error generating {chart.image_description}: {result['error']['message']}" + "\n```")
|
253 |
+
|
254 |
+
return "\n".join(output_parts)
|
255 |
+
|
256 |
+
def _looks_like_structured_data(self, output: str) -> bool:
|
257 |
+
"""Helper to detect JSON-like or array-like output"""
|
258 |
+
output = output.strip()
|
259 |
+
return (
|
260 |
+
output.startswith('{') and output.endswith('}') or # JSON object
|
261 |
+
output.startswith('[') and output.endswith(']') or # Array
|
262 |
+
'\n' in output and '=' in output # Python console output
|
263 |
+
)
|