Skip to content

Commit 9fcaab2

Browse files
committed
fix: check for common datascience packages, added them as optional deps, warn if not available
1 parent b02804b commit 9fcaab2

File tree

5 files changed

+721
-12
lines changed

5 files changed

+721
-12
lines changed

gptme/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
to do so, it needs to be able to store and query past conversations in a database.
2121
"""
2222
# The above may be used as a prompt for the agent.
23+
2324
import atexit
2425
import importlib.metadata
2526
import io
@@ -50,7 +51,12 @@
5051
)
5152
from .prompts import initial_prompt_single_message
5253
from .tabcomplete import register_tabcomplete
53-
from .tools import execute_msg, execute_python, execute_shell
54+
from .tools import (
55+
execute_msg,
56+
execute_python,
57+
execute_shell,
58+
init_tools,
59+
)
5460
from .tools.shell import get_shell
5561
from .tools.summarize import summarize
5662
from .tools.useredit import edit_text_with_editor
@@ -266,6 +272,7 @@ def main(
266272
load_dotenv()
267273
_load_readline_history()
268274
init_llm(llm) # set up API_KEY and API_BASE
275+
init_tools()
269276

270277
if "PYTEST_CURRENT_TEST" in os.environ:
271278
interactive = False

gptme/tools/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from typing import Generator
33

44
from ..message import Message
5-
from .python import execute_python
6-
from .shell import execute_shell
5+
from .python import execute_python, init_python
76
from .save import execute_save
7+
from .shell import execute_shell
88
from .summarize import summarize
99

1010
logger = logging.getLogger(__name__)
@@ -46,3 +46,8 @@ def execute_codeblock(codeblock: str, ask: bool) -> Generator[Message, None, Non
4646
logger.warning(
4747
f"Unknown codeblock type '{lang_or_fn}', neither supported language or filename."
4848
)
49+
50+
51+
def init_tools() -> None:
52+
"""Runs initialization logic for tools."""
53+
init_python()

gptme/tools/python.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
import ast
22
import io
33
from contextlib import redirect_stderr, redirect_stdout
4+
from logging import getLogger
45
from typing import Generator
56

67
from ..message import Message
78
from ..util import ask_execute, print_preview
89

10+
logger = getLogger(__name__)
911
locals_ = {} # type: ignore
1012

1113

14+
def init_python():
15+
check_available_packages()
16+
17+
1218
def execute_python(code: str, ask: bool) -> Generator[Message, None, None]:
1319
"""Executes a python codeblock and returns the output."""
1420
code = code.strip()
1521
if ask:
1622
print_preview(code, "python")
1723
confirm = ask_execute()
1824
print()
25+
if not confirm:
26+
# early return
27+
yield Message("system", "Aborted, user chose not to run command.")
28+
return
1929
else:
2030
print("Skipping confirmation")
2131

22-
if ask and not confirm:
23-
# early return
24-
yield Message("system", "Aborted, user chose not to run command.")
25-
return
26-
2732
# remove blank lines
2833
code = "\n".join([line for line in code.split("\n") if line.strip()])
2934

@@ -50,6 +55,21 @@ def execute_python(code: str, ask: bool) -> Generator[Message, None, None]:
5055
yield Message("system", "Executed code block.\n\n" + output)
5156

5257

58+
def check_available_packages():
59+
"""Checks that essentials like numpy, pandas, matplotlib are available."""
60+
expected = ["numpy", "pandas", "matplotlib"]
61+
missing = []
62+
for package in expected:
63+
try:
64+
__import__(package)
65+
except ImportError:
66+
missing.append(package)
67+
if missing:
68+
logger.warning(
69+
f"Missing packages: {', '.join(missing)}. Install them with `pip install gptme-python -E datascience`"
70+
)
71+
72+
5373
def test_execute_python():
5474
assert execute_python("1 + 1", ask=False) == ">>> 1 + 1\n2\n"
5575
assert execute_python("a = 2\na", ask=False) == ">>> a = 2\n>>> a\n2\n"

0 commit comments

Comments
 (0)