output for result_var name is not needed
Browse files- cerebras_csv_agent.py +4 -2
- python_code_executor_service.py +100 -46
cerebras_csv_agent.py
CHANGED
@@ -38,10 +38,12 @@ class CsvChatResult(BaseModel):
|
|
38 |
casual_response: str
|
39 |
|
40 |
# Data analysis components
|
41 |
-
analysis_operations: List[AnalysisOperation]
|
|
|
42 |
|
43 |
# Visualization components
|
44 |
-
charts: Optional[List[ChartSpecification]] = None
|
|
|
45 |
|
46 |
|
47 |
def get_csv_info(df: pd.DataFrame) -> dict:
|
|
|
38 |
casual_response: str
|
39 |
|
40 |
# Data analysis components
|
41 |
+
# analysis_operations: List[AnalysisOperation]
|
42 |
+
analysis_operations: AnalysisOperation
|
43 |
|
44 |
# Visualization components
|
45 |
+
# charts: Optional[List[ChartSpecification]] = None
|
46 |
+
charts: Optional[ChartSpecification] = None
|
47 |
|
48 |
|
49 |
def get_csv_info(df: pd.DataFrame) -> dict:
|
python_code_executor_service.py
CHANGED
@@ -43,8 +43,10 @@ class AnalysisOperation(BaseModel):
|
|
43 |
class CsvChatResult(BaseModel):
|
44 |
"""Structured response for CSV-related AI interactions"""
|
45 |
casual_response: str
|
46 |
-
analysis_operations: List[AnalysisOperation]
|
47 |
-
charts: Optional[List[ChartSpecification]] = None
|
|
|
|
|
48 |
|
49 |
|
50 |
class PythonExecutor:
|
@@ -194,53 +196,105 @@ class PythonExecutor:
|
|
194 |
return str(result)
|
195 |
|
196 |
async def process_response(self, response: CsvChatResult, chat_id: str) -> str:
|
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 |
-
if response.charts:
|
225 |
-
output_parts.append("\n📊 Visualizations:")
|
226 |
-
for chart in response.charts:
|
227 |
-
if chart.code:
|
228 |
-
chart_result = self.execute_code(chart.code)
|
229 |
-
if chart_result['plots']:
|
230 |
-
for plot_data in chart_result['plots']:
|
231 |
-
try:
|
232 |
-
public_url = await self.save_plot_to_supabase(
|
233 |
-
plot_data=plot_data,
|
234 |
-
description=chart.image_description,
|
235 |
-
chat_id=chat_id
|
236 |
-
)
|
237 |
-
output_parts.append(f"\n🖼️ {chart.image_description}")
|
238 |
-
output_parts.append(f"")
|
239 |
-
except Exception as e:
|
240 |
-
output_parts.append(f"\n⚠️ Values are missing - Error uploading chart: {str(e)}")
|
241 |
-
elif chart_result['error']:
|
242 |
-
output_parts.append("```python\n" + f"Error generating {chart.image_description}: {chart_result['error']['message']}" + "\n```")
|
243 |
-
else:
|
244 |
-
output_parts.append(f"\n⚠️ Values are missing - No chart generated for '{chart.image_description}'")
|
245 |
|
246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
class CsvChatResult(BaseModel):
|
44 |
"""Structured response for CSV-related AI interactions"""
|
45 |
casual_response: str
|
46 |
+
# analysis_operations: List[AnalysisOperation]
|
47 |
+
# charts: Optional[List[ChartSpecification]] = None
|
48 |
+
analysis_operations: AnalysisOperation
|
49 |
+
charts: ChartSpecification = None
|
50 |
|
51 |
|
52 |
class PythonExecutor:
|
|
|
196 |
return str(result)
|
197 |
|
198 |
async def process_response(self, response: CsvChatResult, chat_id: str) -> str:
|
199 |
+
"""Process the response with proper variable handling"""
|
200 |
+
output_parts = [response.casual_response]
|
201 |
|
202 |
+
# Process single analysis operation
|
203 |
+
if response.analysis_operations:
|
204 |
+
operation = response.analysis_operations
|
205 |
+
execution_result = self.execute_code(operation.code.code)
|
206 |
|
207 |
+
# Get the result from locals
|
208 |
+
result = self.exec_locals.get(operation.result_var)
|
209 |
|
210 |
+
if execution_result['error']:
|
211 |
+
output_parts.append(f"\n❌ Error in operation '{operation.result_var}':")
|
212 |
+
output_parts.append("```python\n" + execution_result['error']['message'] + "\n```")
|
213 |
+
elif result is not None:
|
214 |
+
# Handle empty/None results
|
215 |
+
if result is None or (hasattr(result, '__len__') and len(result) == 0):
|
216 |
+
output_parts.append(f"\n⚠️ Values are missing - Operation '{operation.result_var}' returned no data")
|
217 |
+
else:
|
218 |
+
output_parts.append(f"\n🔹 Result for '{operation.result_var}':")
|
219 |
+
output_parts.append("```python\n" + self._format_result(result) + "\n```")
|
220 |
+
else:
|
221 |
+
output_str = execution_result['output'].strip()
|
222 |
+
if output_str:
|
223 |
+
output_parts.append("```\n" + output_str + "\n```")
|
224 |
|
225 |
+
# Process single chart
|
226 |
+
if response.charts:
|
227 |
+
output_parts.append("\n📊 Visualizations:")
|
228 |
+
chart = response.charts
|
229 |
+
if chart.code:
|
230 |
+
chart_result = self.execute_code(chart.code)
|
231 |
+
if chart_result['plots']:
|
232 |
+
for plot_data in chart_result['plots']:
|
233 |
+
try:
|
234 |
+
public_url = await self.save_plot_to_supabase(
|
235 |
+
plot_data=plot_data,
|
236 |
+
description=chart.image_description,
|
237 |
+
chat_id=chat_id
|
238 |
+
)
|
239 |
+
output_parts.append(f"\n🖼️ {chart.image_description}")
|
240 |
+
output_parts.append(f"")
|
241 |
+
except Exception as e:
|
242 |
+
output_parts.append(f"\n⚠️ Values are missing - Error uploading chart: {str(e)}")
|
243 |
+
elif chart_result['error']:
|
244 |
+
output_parts.append("```python\n" + f"Error generating {chart.image_description}: {chart_result['error']['message']}" + "\n```")
|
245 |
+
else:
|
246 |
+
output_parts.append(f"\n⚠️ Values are missing - No chart generated for '{chart.image_description}'")
|
247 |
|
248 |
+
return "\n".join(output_parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
249 |
|
250 |
+
# async def process_response(self, response: CsvChatResult, chat_id: str) -> str:
|
251 |
+
# """Process the response with proper variable handling"""
|
252 |
+
# output_parts = [response.casual_response]
|
253 |
+
|
254 |
+
# # Process analysis operations first
|
255 |
+
# for operation in response.analysis_operations:
|
256 |
+
# execution_result = self.execute_code(operation.code.code)
|
257 |
+
|
258 |
+
# # Get the result from locals
|
259 |
+
# result = self.exec_locals.get(operation.result_var)
|
260 |
+
|
261 |
+
# if execution_result['error']:
|
262 |
+
# output_parts.append(f"\n❌ Error in operation '{operation.result_var}':")
|
263 |
+
# output_parts.append("```python\n" + execution_result['error']['message'] + "\n```")
|
264 |
+
# elif result is not None:
|
265 |
+
# # Handle empty/None results
|
266 |
+
# if result is None or (hasattr(result, '__len__') and len(result) == 0):
|
267 |
+
# output_parts.append(f"\n⚠️ Values are missing - Operation '{operation.result_var}' returned no data")
|
268 |
+
# else:
|
269 |
+
# output_parts.append(f"\n🔹 Result for '{operation.result_var}':")
|
270 |
+
# output_parts.append("```python\n" + self._format_result(result) + "\n```")
|
271 |
+
# else:
|
272 |
+
# output_str = execution_result['output'].strip()
|
273 |
+
# if output_str:
|
274 |
+
# output_parts.append("```\n" + output_str + "\n```")
|
275 |
+
|
276 |
+
|
277 |
+
# # Process charts after all operations
|
278 |
+
# if response.charts:
|
279 |
+
# output_parts.append("\n📊 Visualizations:")
|
280 |
+
# for chart in response.charts:
|
281 |
+
# if chart.code:
|
282 |
+
# chart_result = self.execute_code(chart.code)
|
283 |
+
# if chart_result['plots']:
|
284 |
+
# for plot_data in chart_result['plots']:
|
285 |
+
# try:
|
286 |
+
# public_url = await self.save_plot_to_supabase(
|
287 |
+
# plot_data=plot_data,
|
288 |
+
# description=chart.image_description,
|
289 |
+
# chat_id=chat_id
|
290 |
+
# )
|
291 |
+
# output_parts.append(f"\n🖼️ {chart.image_description}")
|
292 |
+
# output_parts.append(f"")
|
293 |
+
# except Exception as e:
|
294 |
+
# output_parts.append(f"\n⚠️ Values are missing - Error uploading chart: {str(e)}")
|
295 |
+
# elif chart_result['error']:
|
296 |
+
# output_parts.append("```python\n" + f"Error generating {chart.image_description}: {chart_result['error']['message']}" + "\n```")
|
297 |
+
# else:
|
298 |
+
# output_parts.append(f"\n⚠️ Values are missing - No chart generated for '{chart.image_description}'")
|
299 |
+
|
300 |
+
# return "\n".join(output_parts)
|