added together ai agent
Browse files
python_code_executor_service.py
CHANGED
|
@@ -3,27 +3,37 @@ import matplotlib.pyplot as plt
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Dict, Any, List, Optional
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
import json
|
| 7 |
import io
|
| 8 |
import contextlib
|
| 9 |
import traceback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from pydantic import BaseModel
|
| 11 |
|
|
|
|
| 12 |
class CodeResponse(BaseModel):
|
| 13 |
"""Container for code-related responses"""
|
| 14 |
language: str = "python"
|
| 15 |
code: str
|
| 16 |
|
|
|
|
| 17 |
class ChartSpecification(BaseModel):
|
| 18 |
"""Details about requested charts"""
|
| 19 |
image_description: str
|
| 20 |
code: Optional[str] = None
|
| 21 |
|
|
|
|
| 22 |
class AnalysisOperation(BaseModel):
|
| 23 |
"""Container for a single analysis operation with its code and result"""
|
| 24 |
code: CodeResponse
|
| 25 |
description: str
|
| 26 |
|
|
|
|
| 27 |
class CsvChatResult(BaseModel):
|
| 28 |
"""Structured response for CSV-related AI interactions"""
|
| 29 |
response_type: str # Literal["casual", "data_analysis", "visualization", "mixed"]
|
|
@@ -31,8 +41,9 @@ class CsvChatResult(BaseModel):
|
|
| 31 |
analysis_operations: List[AnalysisOperation]
|
| 32 |
charts: Optional[List[ChartSpecification]] = None
|
| 33 |
|
|
|
|
| 34 |
class PythonExecutor:
|
| 35 |
-
"""Handles execution of Python code
|
| 36 |
|
| 37 |
def __init__(self, df: pd.DataFrame, charts_folder: str = "generated_charts"):
|
| 38 |
"""
|
|
@@ -48,7 +59,7 @@ class PythonExecutor:
|
|
| 48 |
|
| 49 |
def execute_code(self, code: str) -> Dict[str, Any]:
|
| 50 |
"""
|
| 51 |
-
Execute Python code
|
| 52 |
|
| 53 |
Args:
|
| 54 |
code (str): Python code to execute
|
|
@@ -78,12 +89,28 @@ class PythonExecutor:
|
|
| 78 |
plt.close('all')
|
| 79 |
|
| 80 |
try:
|
| 81 |
-
# Create execution context with
|
| 82 |
exec_globals = {
|
|
|
|
| 83 |
'pd': pd,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
'plt': plt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
'json': json,
|
| 86 |
-
'df': self.df, # Include the DataFrame in the execution context
|
| 87 |
'__builtins__': __builtins__,
|
| 88 |
}
|
| 89 |
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import Dict, Any, List, Optional
|
| 5 |
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
import json
|
| 8 |
import io
|
| 9 |
import contextlib
|
| 10 |
import traceback
|
| 11 |
+
import time
|
| 12 |
+
from datetime import datetime, timedelta
|
| 13 |
+
import seaborn as sns
|
| 14 |
+
import statsmodels.api as sm
|
| 15 |
+
import scipy.stats as stats
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
+
|
| 19 |
class CodeResponse(BaseModel):
|
| 20 |
"""Container for code-related responses"""
|
| 21 |
language: str = "python"
|
| 22 |
code: str
|
| 23 |
|
| 24 |
+
|
| 25 |
class ChartSpecification(BaseModel):
|
| 26 |
"""Details about requested charts"""
|
| 27 |
image_description: str
|
| 28 |
code: Optional[str] = None
|
| 29 |
|
| 30 |
+
|
| 31 |
class AnalysisOperation(BaseModel):
|
| 32 |
"""Container for a single analysis operation with its code and result"""
|
| 33 |
code: CodeResponse
|
| 34 |
description: str
|
| 35 |
|
| 36 |
+
|
| 37 |
class CsvChatResult(BaseModel):
|
| 38 |
"""Structured response for CSV-related AI interactions"""
|
| 39 |
response_type: str # Literal["casual", "data_analysis", "visualization", "mixed"]
|
|
|
|
| 41 |
analysis_operations: List[AnalysisOperation]
|
| 42 |
charts: Optional[List[ChartSpecification]] = None
|
| 43 |
|
| 44 |
+
|
| 45 |
class PythonExecutor:
|
| 46 |
+
"""Handles execution of Python code with comprehensive data analysis libraries"""
|
| 47 |
|
| 48 |
def __init__(self, df: pd.DataFrame, charts_folder: str = "generated_charts"):
|
| 49 |
"""
|
|
|
|
| 59 |
|
| 60 |
def execute_code(self, code: str) -> Dict[str, Any]:
|
| 61 |
"""
|
| 62 |
+
Execute Python code with full data analysis context and return results
|
| 63 |
|
| 64 |
Args:
|
| 65 |
code (str): Python code to execute
|
|
|
|
| 89 |
plt.close('all')
|
| 90 |
|
| 91 |
try:
|
| 92 |
+
# Create comprehensive execution context with data analysis libraries
|
| 93 |
exec_globals = {
|
| 94 |
+
# Core data analysis
|
| 95 |
'pd': pd,
|
| 96 |
+
'np': np,
|
| 97 |
+
'df': self.df,
|
| 98 |
+
|
| 99 |
+
# Visualization
|
| 100 |
'plt': plt,
|
| 101 |
+
'sns': sns,
|
| 102 |
+
|
| 103 |
+
# Statistics
|
| 104 |
+
'sm': sm,
|
| 105 |
+
'stats': stats,
|
| 106 |
+
|
| 107 |
+
# Date/time
|
| 108 |
+
'datetime': datetime,
|
| 109 |
+
'timedelta': timedelta,
|
| 110 |
+
'time': time,
|
| 111 |
+
|
| 112 |
+
# Utilities
|
| 113 |
'json': json,
|
|
|
|
| 114 |
'__builtins__': __builtins__,
|
| 115 |
}
|
| 116 |
|