-
-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
365 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.0.1" | ||
__version__ = "1.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import subprocess | ||
|
||
from instructor import OpenAISchema | ||
from pydantic import Field | ||
|
||
|
||
class Function(OpenAISchema): | ||
""" | ||
Executes a shell command and returns the output (result). | ||
""" | ||
|
||
shell_command: str = Field( | ||
..., | ||
example="ls -la", | ||
descriptions="Shell command to execute.", | ||
) | ||
|
||
class Config: | ||
title = "execute_shell_command" | ||
|
||
@classmethod | ||
def execute(cls, shell_command: str) -> str: | ||
process = subprocess.Popen( | ||
shell_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT | ||
) | ||
output, _ = process.communicate() | ||
exit_code = process.returncode | ||
return f"Exit code: {exit_code}, Output:\n{output.decode()}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import os | ||
import platform | ||
import shutil | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from ..config import cfg | ||
from ..utils import option_callback | ||
|
||
FUNCTIONS_FOLDER = Path(cfg.get("OPENAI_FUNCTIONS_PATH")) | ||
|
||
|
||
@option_callback | ||
def install_functions(*_args: Any) -> None: | ||
current_folder = os.path.dirname(os.path.abspath(__file__)) | ||
common_folder = Path(current_folder + "/common") | ||
common_files = [Path(path) for path in common_folder.glob("*.py")] | ||
print("Installing default functions...") | ||
|
||
for file in common_files: | ||
print(f"Installed {FUNCTIONS_FOLDER}/{file.name}") | ||
shutil.copy(file, FUNCTIONS_FOLDER, follow_symlinks=True) | ||
|
||
current_platform = platform.system() | ||
if current_platform == "Linux": | ||
print("Installing Linux functions...") | ||
if current_platform == "Windows": | ||
print("Installing Windows functions...") | ||
if current_platform == "Darwin": | ||
print("Installing Mac functions...") | ||
mac_folder = Path(current_folder + "/mac") | ||
mac_files = [Path(path) for path in mac_folder.glob("*.py")] | ||
for file in mac_files: | ||
print(f"Installed {FUNCTIONS_FOLDER}/{file.name}") | ||
shutil.copy(file, FUNCTIONS_FOLDER, follow_symlinks=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import subprocess | ||
|
||
from instructor import OpenAISchema | ||
from pydantic import Field | ||
|
||
|
||
class Function(OpenAISchema): | ||
""" | ||
Executes Apple Script on macOS and returns the output (result). | ||
Can be used for actions like: draft (prepare) an email, show calendar events, create a note. | ||
""" | ||
|
||
apple_script: str = Field( | ||
..., | ||
example='tell application "Finder" to get the name of every disk', | ||
descriptions="Apple Script to execute.", | ||
) | ||
|
||
class Config: | ||
title = "execute_apple_script" | ||
|
||
@classmethod | ||
def execute(cls, apple_script): | ||
script_command = ["osascript", "-e", apple_script] | ||
try: | ||
process = subprocess.Popen( | ||
script_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE | ||
) | ||
output, _ = process.communicate() | ||
output = output.decode("utf-8").strip() | ||
return f"Output: {output}" | ||
except Exception as e: | ||
return f"Error: {e}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import importlib.util | ||
import sys | ||
from abc import ABCMeta | ||
from pathlib import Path | ||
from typing import Any, Callable | ||
|
||
from .config import cfg | ||
|
||
|
||
class Function: | ||
def __init__(self, path: str): | ||
module = self._read(path) | ||
self._function = module.Function.execute | ||
self._openai_schema = module.Function.openai_schema | ||
self._name = self._openai_schema["name"] | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._name | ||
|
||
@property | ||
def openai_schema(self) -> dict[str, Any]: | ||
return self._openai_schema | ||
|
||
@property | ||
def execute(self) -> Callable[..., str]: | ||
return self._function | ||
|
||
@classmethod | ||
def _read(cls, path: str) -> Any: | ||
module_name = path.replace("/", ".").rstrip(".py") | ||
spec = importlib.util.spec_from_file_location(module_name, path) | ||
module = importlib.util.module_from_spec(spec) | ||
sys.modules[module_name] = module | ||
spec.loader.exec_module(module) | ||
|
||
if not isinstance(module.Function, ABCMeta): | ||
raise TypeError( | ||
f"Function {module_name} must be a subclass of pydantic.BaseModel" | ||
) | ||
if not hasattr(module.Function, "execute"): | ||
raise TypeError( | ||
f"Function {module_name} must have a 'execute' static method" | ||
) | ||
|
||
return module | ||
|
||
|
||
functions_folder = Path(cfg.get("OPENAI_FUNCTIONS_PATH")) | ||
functions_folder.mkdir(parents=True, exist_ok=True) | ||
functions = [Function(str(path)) for path in functions_folder.glob("*.py")] | ||
|
||
|
||
def get_function(name: str) -> Callable[..., Any]: | ||
for function in functions: | ||
if function.name == name: | ||
return function.execute | ||
raise ValueError(f"Function {name} not found") | ||
|
||
|
||
def get_openai_schemas() -> [dict[str, Any]]: | ||
return [function.openai_schema for function in functions] |
Oops, something went wrong.