Skip to content

Commit

Permalink
feat: include context about included programs and packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 30, 2023
1 parent cd959d6 commit 9ccb0a3
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion gptme/prompts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import logging
import os
import shutil
Expand Down Expand Up @@ -92,10 +93,16 @@ def initial_prompt(short: bool = False) -> Generator[Message, None, None]:
hide=True,
)

python_libraries = get_installed_python_libraries()
python_libraries_str = "\n".join(f"- {lib}" for lib in python_libraries)

shell_programs = get_installed_programs()
shell_programs_str = "\n".join(f"- {prog}" for prog in shell_programs)

if include_tools:
yield Message(
"system",
"""
f"""
You are gptme, an AI assistant CLI tool powered powered by large language models that helps the user.
You can run code and execute terminal commands on their local machine.
The assistant shows the user to write code, interact with the system, and access the internet. The user will then choose to execute the suggested commands.
Expand All @@ -111,10 +118,16 @@ def initial_prompt(short: bool = False) -> Generator[Message, None, None]:
When you send a message containing Python code (and is not a file block), it will be executed in a stateful environment. Python will respond with the output of the execution.
The following libraries are available:
{python_libraries_str}
## bash
When you send a message containing bash code, it will be executed in a stateful bash shell. The shell will respond with the output of the execution.
These programs are available, among others:
{shell_programs_str}
## saving files
When you send a message containing a code block, if the first line contains a filename, like "```hello.py" (a "file block"), the code block will be saved to that file.
Expand Down Expand Up @@ -208,3 +221,36 @@ def initial_prompt(short: bool = False) -> Generator[Message, None, None]:
hide=True,
pinned=True,
)


@functools.lru_cache
def get_installed_python_libraries() -> set[str]:
"""Check if a select list of Python libraries are installed."""
candidates = [
"numpy",
"pandas",
"matplotlib",
"seaborn",
"scipy",
"scikit-learn",
"statsmodels",
"pillow",
]
installed = set()
for candidate in candidates:
try:
__import__(candidate)
installed.add(candidate)
except ImportError:
pass
return installed


@functools.lru_cache
def get_installed_programs() -> set[str]:
candidates = ["ffmpeg", "convert", "pandoc"]
installed = set()
for candidate in candidates:
if shutil.which(candidate) is not None:
installed.add(candidate)
return installed

0 comments on commit 9ccb0a3

Please sign in to comment.