plot-agent 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plot-agent
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: An AI-powered data visualization assistant using Plotly
5
5
  Author-email: andrewm4894 <andrewm4894@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/andrewm4894/plot-agent
@@ -7,88 +7,21 @@ from plotly.subplots import make_subplots
7
7
  from io import StringIO
8
8
  import traceback
9
9
  import sys
10
- from typing import Dict, List, Optional, Any
10
+ from typing import Dict, Optional, Any
11
11
 
12
12
  from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
13
13
  from langchain_core.messages import AIMessage, HumanMessage
14
14
  from langchain_core.tools import Tool, StructuredTool
15
15
  from langchain.agents import AgentExecutor, create_openai_tools_agent
16
16
  from langchain_openai import ChatOpenAI
17
+
17
18
  from plot_agent.prompt import DEFAULT_SYSTEM_PROMPT
18
19
  from plot_agent.models import (
19
20
  GeneratedCodeInput,
20
21
  DoesFigExistInput,
21
22
  ViewGeneratedCodeInput,
22
23
  )
23
-
24
-
25
- class PlotlyAgentExecutionEnvironment:
26
- """Environment to safely execute plotly code and capture the fig object."""
27
-
28
- def __init__(self, df: pd.DataFrame):
29
- self.df = df
30
- self.locals_dict = {
31
- "df": df,
32
- "px": px,
33
- "go": go,
34
- "pd": pd,
35
- "np": np,
36
- "plt": plt,
37
- "make_subplots": make_subplots,
38
- }
39
- self.output = None
40
- self.error = None
41
- self.fig = None
42
-
43
- def execute_code(self, generated_code: str) -> Dict[str, Any]:
44
- """
45
- Execute the provided code and capture the fig object if created.
46
-
47
- Args:
48
- generated_code (str): The code to execute.
49
-
50
- Returns:
51
- Dict[str, Any]: A dictionary containing the fig object, output, error, and success status.
52
- """
53
- self.output = None
54
- self.error = None
55
-
56
- # Capture stdout
57
- old_stdout = sys.stdout
58
- sys.stdout = mystdout = StringIO()
59
-
60
- try:
61
- # Execute the code
62
- exec(generated_code, globals(), self.locals_dict)
63
-
64
- # Check if a fig object was created
65
- if "fig" in self.locals_dict:
66
- self.fig = self.locals_dict["fig"]
67
- self.output = "Code executed successfully. 'fig' object was created."
68
- else:
69
- print(f"no fig object created: {generated_code}")
70
- self.error = "Code executed without errors, but no 'fig' object was created. Make sure your code creates a variable named 'fig'."
71
-
72
- except Exception as e:
73
- self.error = f"Error executing code: {str(e)}\n{traceback.format_exc()}"
74
-
75
- finally:
76
- # Restore stdout
77
- sys.stdout = old_stdout
78
- captured_output = mystdout.getvalue()
79
-
80
- if captured_output.strip():
81
- if self.output:
82
- self.output += f"\nOutput:\n{captured_output}"
83
- else:
84
- self.output = f"Output:\n{captured_output}"
85
-
86
- return {
87
- "fig": self.fig,
88
- "output": self.output,
89
- "error": self.error,
90
- "success": self.error is None and self.fig is not None,
91
- }
24
+ from plot_agent.execution import PlotlyAgentExecutionEnvironment
92
25
 
93
26
 
94
27
  class PlotlyAgent:
@@ -0,0 +1,79 @@
1
+ import sys
2
+ from io import StringIO
3
+ import traceback
4
+ import pandas as pd
5
+ import plotly.express as px
6
+ import plotly.graph_objects as go
7
+ import numpy as np
8
+ import matplotlib.pyplot as plt
9
+ from plotly.subplots import make_subplots
10
+ from typing import Dict, Any
11
+
12
+
13
+ class PlotlyAgentExecutionEnvironment:
14
+ """Environment to safely execute plotly code and capture the fig object."""
15
+
16
+ def __init__(self, df: pd.DataFrame):
17
+ self.df = df
18
+ self.locals_dict = {
19
+ "df": df,
20
+ "px": px,
21
+ "go": go,
22
+ "pd": pd,
23
+ "np": np,
24
+ "plt": plt,
25
+ "make_subplots": make_subplots,
26
+ }
27
+ self.output = None
28
+ self.error = None
29
+ self.fig = None
30
+
31
+ def execute_code(self, generated_code: str) -> Dict[str, Any]:
32
+ """
33
+ Execute the provided code and capture the fig object if created.
34
+
35
+ Args:
36
+ generated_code (str): The code to execute.
37
+
38
+ Returns:
39
+ Dict[str, Any]: A dictionary containing the fig object, output, error, and success status.
40
+ """
41
+ self.output = None
42
+ self.error = None
43
+
44
+ # Capture stdout
45
+ old_stdout = sys.stdout
46
+ sys.stdout = mystdout = StringIO()
47
+
48
+ try:
49
+ # Execute the code
50
+ exec(generated_code, globals(), self.locals_dict)
51
+
52
+ # Check if a fig object was created
53
+ if "fig" in self.locals_dict:
54
+ self.fig = self.locals_dict["fig"]
55
+ self.output = "Code executed successfully. 'fig' object was created."
56
+ else:
57
+ print(f"no fig object created: {generated_code}")
58
+ self.error = "Code executed without errors, but no 'fig' object was created. Make sure your code creates a variable named 'fig'."
59
+
60
+ except Exception as e:
61
+ self.error = f"Error executing code: {str(e)}\n{traceback.format_exc()}"
62
+
63
+ finally:
64
+ # Restore stdout
65
+ sys.stdout = old_stdout
66
+ captured_output = mystdout.getvalue()
67
+
68
+ if captured_output.strip():
69
+ if self.output:
70
+ self.output += f"\nOutput:\n{captured_output}"
71
+ else:
72
+ self.output = f"Output:\n{captured_output}"
73
+
74
+ return {
75
+ "fig": self.fig,
76
+ "output": self.output,
77
+ "error": self.error,
78
+ "success": self.error is None and self.fig is not None,
79
+ }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plot-agent
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: An AI-powered data visualization assistant using Plotly
5
5
  Author-email: andrewm4894 <andrewm4894@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/andrewm4894/plot-agent
@@ -3,6 +3,7 @@ README.md
3
3
  pyproject.toml
4
4
  plot_agent/__init__.py
5
5
  plot_agent/agent.py
6
+ plot_agent/execution.py
6
7
  plot_agent/models.py
7
8
  plot_agent/prompt.py
8
9
  plot_agent.egg-info/PKG-INFO
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "plot-agent"
7
- version = "0.2.0"
7
+ version = "0.2.1"
8
8
  authors = [
9
9
  { name="andrewm4894", email="andrewm4894@gmail.com" },
10
10
  ]
File without changes
File without changes
File without changes