From 7363f5bcbf6e47a777ea119bb3a44965f2a658fb Mon Sep 17 00:00:00 2001 From: Ty Fiero Date: Mon, 12 Feb 2024 22:11:03 -0800 Subject: [PATCH] Add doc strings to all computer API functions for the LLM to use in a skill library --- interpreter/core/computer/browser/browser.py | 3 +++ .../core/computer/clipboard/clipboard.py | 9 +++++++ interpreter/core/computer/computer.py | 2 +- interpreter/core/computer/display/display.py | 22 +++++++++++++--- interpreter/core/computer/files/files.py | 8 +++++- .../core/computer/keyboard/keyboard.py | 24 +++--------------- interpreter/core/computer/mouse/mouse.py | 25 +++++++++++++++++++ interpreter/core/computer/os/os.py | 6 +++++ 8 files changed, 73 insertions(+), 26 deletions(-) diff --git a/interpreter/core/computer/browser/browser.py b/interpreter/core/computer/browser/browser.py index 6b6cf80ba9..4410bf6ba4 100644 --- a/interpreter/core/computer/browser/browser.py +++ b/interpreter/core/computer/browser/browser.py @@ -6,6 +6,9 @@ def __init__(self, computer): self.computer = computer def search(self, query): + """ + Searches the web for the specified query and returns the results. + """ response = requests.get( f'{self.computer.api_base.strip("/")}/browser/search', params={"q": query} ) diff --git a/interpreter/core/computer/clipboard/clipboard.py b/interpreter/core/computer/clipboard/clipboard.py index bd4996b876..b18e808edb 100644 --- a/interpreter/core/computer/clipboard/clipboard.py +++ b/interpreter/core/computer/clipboard/clipboard.py @@ -17,13 +17,22 @@ def __init__(self, computer): self.modifier_key = "command" def view(self): + """ + Returns the current content of on the clipboard. + """ return pyperclip.paste() def copy(self, text=None): + """ + Copies the given text to the clipboard. + """ if text is not None: pyperclip.copy(text) else: self.computer.keyboard.hotkey(self.modifier_key, "c") def paste(self): + """ + Pastes the current content of the clipboard. + """ self.computer.keyboard.hotkey(self.modifier_key, "v") diff --git a/interpreter/core/computer/computer.py b/interpreter/core/computer/computer.py index 3062a1af81..7fa8085129 100644 --- a/interpreter/core/computer/computer.py +++ b/interpreter/core/computer/computer.py @@ -49,8 +49,8 @@ def run(self, *args, **kwargs): def exec(self, code): """ - It has hallucinated this. Shortcut for computer.terminal.run("shell", code) + It has hallucinated this. """ return self.terminal.run("shell", code) diff --git a/interpreter/core/computer/display/display.py b/interpreter/core/computer/display/display.py index 169c06f8ba..be23aaec3c 100644 --- a/interpreter/core/computer/display/display.py +++ b/interpreter/core/computer/display/display.py @@ -33,9 +33,15 @@ def __init__(self, computer): pass def size(self): + """ + Returns the current screen size as a tuple (width, height). + """ return pyautogui.size() def center(self): + """ + Calculates and returns the center point of the screen as a tuple (x, y). + """ return self.width // 2, self.height // 2 def view(self, show=True, quadrant=None): @@ -48,6 +54,9 @@ def view(self, show=True, quadrant=None): # return get_active_window() def screenshot(self, show=True, quadrant=None, active_app_only=False): + """ + Shows you what's on the screen by taking a screenshot of the entire screen or a specified quadrant. Returns a `pil_image` `in case you need it (rarely). **You almost always want to do this first!** + """ time.sleep(2) if not self.computer.emit_images: text = self.get_text_as_list_of_lists() @@ -110,7 +119,9 @@ def screenshot(self, show=True, quadrant=None, active_app_only=False): return screenshot def find_text(self, text, screenshot=None): - # Take a screenshot + """ + Searches for specified text within a screenshot or the current screen if no screenshot is provided. + """ if screenshot == None: screenshot = self.screenshot(show=False) @@ -140,7 +151,9 @@ def find_text(self, text, screenshot=None): ] # Have it deliver the text properly soon. def get_text_as_list_of_lists(self, screenshot=None): - # Take a screenshot + """ + Extracts and returns text from a screenshot or the current screen as a list of lists, each representing a line of text. + """ if screenshot == None: screenshot = self.screenshot(show=False) @@ -172,6 +185,9 @@ def get_text_as_list_of_lists(self, screenshot=None): # locate text should be moved here as well! def find_icon(self, query, screenshot=None): + """ + Locates an icon on the screen and returns its coordinates. + """ message = format_to_recipient( "Locating this icon will take ~30 seconds. We're working on speeding this up.", recipient="user", @@ -201,5 +217,5 @@ def find_icon(self, query, screenshot=None): except Exception as e: raise Exception( str(e) - + "\n\nIcon locating API not avaliable, or we were unable to find the icon. Please try another method to find this icon." + + "\n\nIcon locating API not available, or we were unable to find the icon. Please try another method to find this icon." ) diff --git a/interpreter/core/computer/files/files.py b/interpreter/core/computer/files/files.py index ab0a53b635..377b56f696 100644 --- a/interpreter/core/computer/files/files.py +++ b/interpreter/core/computer/files/files.py @@ -9,11 +9,14 @@ def __init__(self, computer): def search(self, *args, **kwargs): """ - AI Filesystem Search + Search the filesystem for the given query. """ return search(*args, **kwargs) def edit(self, path, original_text, replacement_text): + """ + Edits a file on the filesystem, replacing the original text with the replacement text. + """ with open(path, "r") as file: filedata = file.read() @@ -32,6 +35,9 @@ def edit(self, path, original_text, replacement_text): def get_close_matches_in_text(original_text, filedata, n=3): + """ + Returns the closest matches to the original text in the content of the file. + """ words = filedata.split() original_words = original_text.split() len_original = len(original_words) diff --git a/interpreter/core/computer/keyboard/keyboard.py b/interpreter/core/computer/keyboard/keyboard.py index 9cfb8d1f32..c75b7007df 100644 --- a/interpreter/core/computer/keyboard/keyboard.py +++ b/interpreter/core/computer/keyboard/keyboard.py @@ -17,11 +17,7 @@ def __init__(self, computer): def write(self, text, interval=None, **kwargs): """ - Type out a string of characters. - - Args: - text (str): The string to be typed out. - interval (int or float, optional): The delay between pressing each character key. Defaults to 0.1. + Type out a string of characters. """ time.sleep(0.15) @@ -68,11 +64,6 @@ def press(self, *args, presses=1, interval=0.1): If keys is a string, it is treated as a single key and is pressed the number of times specified by presses. If keys is a list, each key in the list is pressed once. - - Args: - keys (str or list): The key(s) to be pressed. - presses (int, optional): The number of times to press the key. Defaults to 1. - interval (float, optional): The delay between each key press. Defaults to 0.1. """ time.sleep(0.15) pyautogui.press(keys, presses=presses, interval=interval) @@ -81,9 +72,6 @@ def press(self, *args, presses=1, interval=0.1): def hotkey(self, *args, interval=0.1): """ Press a sequence of keys in the order they are provided, and then release them in reverse order. - - Args: - *args: The keys to be pressed. """ time.sleep(0.15) modifiers = ["command", "option", "alt", "ctrl", "shift"] @@ -121,10 +109,7 @@ def hotkey(self, *args, interval=0.1): def down(self, key): """ - Simulate the pressing down of a key. - - Args: - key (str): The key to be pressed down. + Press down a key. """ time.sleep(0.15) pyautogui.keyDown(key) @@ -132,10 +117,7 @@ def down(self, key): def up(self, key): """ - Simulate the releasing of a key. - - Args: - key (str): The key to be released. + Release a key. """ time.sleep(0.15) pyautogui.keyUp(key) diff --git a/interpreter/core/computer/mouse/mouse.py b/interpreter/core/computer/mouse/mouse.py index 23af576453..de7b2b36a5 100644 --- a/interpreter/core/computer/mouse/mouse.py +++ b/interpreter/core/computer/mouse/mouse.py @@ -19,6 +19,9 @@ def __init__(self, computer): self.computer = computer def scroll(self, clicks): + """ + Scrolls the mouse wheel up or down the specified number of clicks. + """ pyautogui.scroll(clicks) def position(self): @@ -36,6 +39,10 @@ def position(self): ) def move(self, *args, x=None, y=None, icon=None, text=None, screenshot=None): + """ + Moves the mouse to specified coordinates, an icon, or text. + """ + screenshot = None if len(args) > 1: raise ValueError( "Too many positional arguments provided. To move/click specific coordinates, use kwargs (x=x, y=y).\n\nPlease take a screenshot with computer.display.view() to find text/icons to click, then use computer.mouse.click(text) or computer.mouse.click(icon=description_of_icon) if at all possible. This is **significantly** more accurate than using coordinates. Specifying (x=x, y=y) is highly likely to fail. Specifying ('text to click') is highly likely to succeed." @@ -216,29 +223,47 @@ def move(self, *args, x=None, y=None, icon=None, text=None, screenshot=None): smooth_move_to(x, y) def click(self, *args, button="left", clicks=1, interval=0.1, **kwargs): + """ + Clicks the mouse at the specified coordinates, icon, or text. + """ if args or kwargs: self.move(*args, **kwargs) pyautogui.click(button=button, clicks=clicks, interval=interval) def double_click(self, *args, button="left", interval=0.1, **kwargs): + """ + Double-clicks the mouse at the specified coordinates, icon, or text. + """ if args or kwargs: self.move(*args, **kwargs) pyautogui.doubleClick(button=button, interval=interval) def triple_click(self, *args, button="left", interval=0.1, **kwargs): + """ + Triple-clicks the mouse at the specified coordinates, icon, or text. + """ if args or kwargs: self.move(*args, **kwargs) pyautogui.tripleClick(button=button, interval=interval) def right_click(self, *args, **kwargs): + """ + Right-clicks the mouse at the specified coordinates, icon, or text. + """ if args or kwargs: self.move(*args, **kwargs) pyautogui.rightClick() def down(self): + """ + Presses the mouse button down. + """ pyautogui.mouseDown() def up(self): + """ + Releases the mouse button. + """ pyautogui.mouseUp() diff --git a/interpreter/core/computer/os/os.py b/interpreter/core/computer/os/os.py index d700b0d5f9..e4508dbac0 100644 --- a/interpreter/core/computer/os/os.py +++ b/interpreter/core/computer/os/os.py @@ -7,6 +7,9 @@ def __init__(self, computer): self.computer = computer def get_selected_text(self): + """ + Returns the currently selected text. + """ # Store the current clipboard content current_clipboard = self.computer.clipboard.view() # Copy the selected text to clipboard @@ -18,6 +21,9 @@ def get_selected_text(self): return selected_text def notify(self, text): + """ + Displays a notification on the computer. + """ try: title = "Open Interpreter"