From 0e5e3985a539e07614f5dcff207a88344e27eb3e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 30 Nov 2024 08:49:56 +0100 Subject: [PATCH] pre-commit autoupdate && pre-commit run --all-files --- .pre-commit-config.yaml | 4 +-- interpreter/computer_use/tools/computer.py | 4 ++- interpreter/computer_use/tools/run.py | 4 ++- .../core/computer/calendar/calendar.py | 2 +- .../core/computer/clipboard/clipboard.py | 4 ++- interpreter/core/computer/computer.py | 8 +++--- interpreter/core/computer/docs/docs.py | 3 ++- interpreter/core/computer/files/files.py | 3 ++- .../core/computer/terminal/languages/java.py | 17 +++++++----- .../core/computer/terminal/languages/ruby.py | 11 ++++---- .../core/computer/terminal/terminal.py | 26 ++++++++++++++----- interpreter/core/computer/vision/vision.py | 7 ++--- interpreter/core/core.py | 1 + interpreter/core/llm/run_text_llm.py | 4 +-- .../llm/utils/convert_to_openai_messages.py | 12 ++++----- interpreter/core/utils/lazy_import.py | 1 + .../profiles/defaults/cerebras.py | 3 ++- .../profiles/defaults/codestral-few-shot.py | 3 ++- .../profiles/defaults/llama31-database.py | 5 ++-- .../profiles/defaults/obsidian.py | 3 ++- .../profiles/defaults/screenpipe.py | 3 ++- .../profiles/defaults/template_profile.py | 6 ++--- .../render_past_conversation.py | 1 - .../utils/export_to_markdown.py | 2 +- tests/core/computer/test_computer.py | 9 +++++-- 25 files changed, 91 insertions(+), 55 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0bcc5c333d..9bd7562fa4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ repos: # Using this mirror lets us use mypyc-compiled black, which is 2x faster - repo: https://github.com/psf/black-pre-commit-mirror - rev: 23.10.1 + rev: 24.10.0 hooks: - id: black # It is recommended to specify the latest version of Python @@ -10,6 +10,6 @@ repos: # https://pre-commit.com/#top_level-default_language_version language_version: python3.11 - repo: https://github.com/PyCQA/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort diff --git a/interpreter/computer_use/tools/computer.py b/interpreter/computer_use/tools/computer.py index 62faf62e7e..7fbe422b28 100644 --- a/interpreter/computer_use/tools/computer.py +++ b/interpreter/computer_use/tools/computer.py @@ -101,7 +101,9 @@ class ComputerTool(BaseAnthropicTool): api_type: Literal["computer_20241022"] = "computer_20241022" width: int height: int - display_num: None # Simplified to always be None since we're only using primary display + display_num: ( + None # Simplified to always be None since we're only using primary display + ) _screenshot_delay = 2.0 _scaling_enabled = True diff --git a/interpreter/computer_use/tools/run.py b/interpreter/computer_use/tools/run.py index 89db980ac8..157957b107 100644 --- a/interpreter/computer_use/tools/run.py +++ b/interpreter/computer_use/tools/run.py @@ -2,7 +2,9 @@ import asyncio -TRUNCATED_MESSAGE: str = "To save on context only part of this file has been shown to you. You should retry this tool after you have searched inside the file with `grep -n` in order to find the line numbers of what you are looking for." +TRUNCATED_MESSAGE: str = ( + "To save on context only part of this file has been shown to you. You should retry this tool after you have searched inside the file with `grep -n` in order to find the line numbers of what you are looking for." +) MAX_RESPONSE_LEN: int = 16000 diff --git a/interpreter/core/computer/calendar/calendar.py b/interpreter/core/computer/calendar/calendar.py index 781f895b5e..4b992b400c 100644 --- a/interpreter/core/computer/calendar/calendar.py +++ b/interpreter/core/computer/calendar/calendar.py @@ -4,7 +4,6 @@ from ..utils.run_applescript import run_applescript, run_applescript_capture - makeDateFunction = """ on makeDate(yr, mon, day, hour, min, sec) set theDate to current date @@ -20,6 +19,7 @@ end makeDate """ + class Calendar: def __init__(self, computer): self.computer = computer diff --git a/interpreter/core/computer/clipboard/clipboard.py b/interpreter/core/computer/clipboard/clipboard.py index 72d5234641..4665d73175 100644 --- a/interpreter/core/computer/clipboard/clipboard.py +++ b/interpreter/core/computer/clipboard/clipboard.py @@ -1,8 +1,10 @@ import platform + from ...utils.lazy_import import lazy_import # Lazy import of optional packages -pyperclip = lazy_import('pyperclip') +pyperclip = lazy_import("pyperclip") + class Clipboard: def __init__(self, computer): diff --git a/interpreter/core/computer/computer.py b/interpreter/core/computer/computer.py index 82a8821f47..5d949bdc98 100644 --- a/interpreter/core/computer/computer.py +++ b/interpreter/core/computer/computer.py @@ -164,9 +164,11 @@ def _extract_tool_info(self, tool): method_signature = inspect.signature(method) # Construct the signature string without *args and **kwargs param_str = ", ".join( - f"{param.name}" - if param.default == param.empty - else f"{param.name}={param.default!r}" + ( + f"{param.name}" + if param.default == param.empty + else f"{param.name}={param.default!r}" + ) for param in method_signature.parameters.values() if param.kind not in (param.VAR_POSITIONAL, param.VAR_KEYWORD) ) diff --git a/interpreter/core/computer/docs/docs.py b/interpreter/core/computer/docs/docs.py index c83585097e..7265ec7b4d 100644 --- a/interpreter/core/computer/docs/docs.py +++ b/interpreter/core/computer/docs/docs.py @@ -4,7 +4,8 @@ from ...utils.lazy_import import lazy_import # Lazy import of aifs, imported when needed to speed up start time -aifs = lazy_import('aifs') +aifs = lazy_import("aifs") + class Docs: def __init__(self, computer): diff --git a/interpreter/core/computer/files/files.py b/interpreter/core/computer/files/files.py index 4c559b3ab1..74a825b611 100644 --- a/interpreter/core/computer/files/files.py +++ b/interpreter/core/computer/files/files.py @@ -3,7 +3,8 @@ from ...utils.lazy_import import lazy_import # Lazy import of aifs, imported when needed -aifs = lazy_import('aifs') +aifs = lazy_import("aifs") + class Files: def __init__(self, computer): diff --git a/interpreter/core/computer/terminal/languages/java.py b/interpreter/core/computer/terminal/languages/java.py index c3ac31f39e..c52e62780b 100644 --- a/interpreter/core/computer/terminal/languages/java.py +++ b/interpreter/core/computer/terminal/languages/java.py @@ -5,8 +5,10 @@ import threading import time import traceback + from .subprocess_language import SubprocessLanguage + class Java(SubprocessLanguage): file_extension = "java" name = "Java" @@ -33,12 +35,12 @@ def detect_end_of_execution(self, line): def run(self, code): try: # Extract the class name from the code - match = re.search(r'class\s+(\w+)', code) + match = re.search(r"class\s+(\w+)", code) if not match: yield { "type": "console", "format": "output", - "content": "Error: No class definition found in the provided code." + "content": "Error: No class definition found in the provided code.", } return @@ -46,7 +48,7 @@ def run(self, code): file_name = f"{class_name}.java" # Write the Java code to a file, preserving newlines - with open(file_name, "w", newline='\n') as file: + with open(file_name, "w", newline="\n") as file: file.write(code) # Compile the Java code @@ -54,7 +56,7 @@ def run(self, code): ["javac", file_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, - text=True + text=True, ) stdout, stderr = compile_process.communicate() @@ -63,7 +65,7 @@ def run(self, code): yield { "type": "console", "format": "output", - "content": f"Compilation Error:\n{stderr}" + "content": f"Compilation Error:\n{stderr}", } return @@ -72,7 +74,7 @@ def run(self, code): ["java", class_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, - text=True + text=True, ) stdout_thread = threading.Thread( @@ -115,7 +117,7 @@ def run(self, code): yield { "type": "console", "format": "output", - "content": f"{traceback.format_exc()}" + "content": f"{traceback.format_exc()}", } finally: # Clean up the generated Java files @@ -125,6 +127,7 @@ def run(self, code): if os.path.exists(class_file): os.remove(class_file) + def preprocess_java(code): """ Add active line markers diff --git a/interpreter/core/computer/terminal/languages/ruby.py b/interpreter/core/computer/terminal/languages/ruby.py index 296a0abf50..b84e529abd 100644 --- a/interpreter/core/computer/terminal/languages/ruby.py +++ b/interpreter/core/computer/terminal/languages/ruby.py @@ -1,5 +1,6 @@ import re from pathlib import Path + from .subprocess_language import SubprocessLanguage @@ -9,12 +10,12 @@ class Ruby(SubprocessLanguage): def __init__(self): super().__init__() - self.start_cmd = ["irb"] + self.start_cmd = ["irb"] def preprocess_code(self, code): """ Add active line markers - Wrap in a tryCatch for better error handling + Wrap in a tryCatch for better error handling Add end of execution marker """ @@ -39,7 +40,7 @@ def preprocess_code(self, code): end """ self.code_line_count = len(processed_code.split("\n")) - #print(processed_code) + # print(processed_code) return processed_code def line_postprocessor(self, line): @@ -48,7 +49,7 @@ def line_postprocessor(self, line): self.code_line_count -= 1 return None if "nil" in line: - return None + return None return line def detect_active_line(self, line): @@ -57,4 +58,4 @@ def detect_active_line(self, line): return None def detect_end_of_execution(self, line): - return "##end_of_execution##" in line or "##execution_error##" in line \ No newline at end of file + return "##end_of_execution##" in line or "##execution_error##" in line diff --git a/interpreter/core/computer/terminal/terminal.py b/interpreter/core/computer/terminal/terminal.py index b9f92582f6..ba261a4d4d 100644 --- a/interpreter/core/computer/terminal/terminal.py +++ b/interpreter/core/computer/terminal/terminal.py @@ -1,8 +1,8 @@ +import getpass import json import os -import time import subprocess -import getpass +import time from ..utils.recipient_utils import parse_for_recipient from .languages.applescript import AppleScript @@ -50,7 +50,7 @@ def __init__(self, computer): def sudo_install(self, package): try: # First, try to install without sudo - subprocess.run(['apt', 'install', '-y', package], check=True) + subprocess.run(["apt", "install", "-y", package], check=True) except subprocess.CalledProcessError: # If it fails, try with sudo print(f"Installation of {package} requires sudo privileges.") @@ -59,9 +59,9 @@ def sudo_install(self, package): try: # Use sudo with password subprocess.run( - ['sudo', '-S', 'apt', 'install', '-y', package], + ["sudo", "-S", "apt", "install", "-y", package], input=sudo_password.encode(), - check=True + check=True, ) print(f"Successfully installed {package}") except subprocess.CalledProcessError as e: @@ -84,9 +84,21 @@ def run(self, language, code, stream=False, display=False): if language == "shell" and code.strip().startswith("apt install"): package = code.split()[-1] if self.sudo_install(package): - return [{"type": "console", "format": "output", "content": f"Package {package} installed successfully."}] + return [ + { + "type": "console", + "format": "output", + "content": f"Package {package} installed successfully.", + } + ] else: - return [{"type": "console", "format": "output", "content": f"Failed to install package {package}."}] + return [ + { + "type": "console", + "format": "output", + "content": f"Failed to install package {package}.", + } + ] if language == "python": if ( diff --git a/interpreter/core/computer/vision/vision.py b/interpreter/core/computer/vision/vision.py index 54647f22ac..3ab31e98cc 100644 --- a/interpreter/core/computer/vision/vision.py +++ b/interpreter/core/computer/vision/vision.py @@ -22,9 +22,10 @@ def __init__(self, computer): def load(self, load_moondream=True, load_easyocr=True): # print("Loading vision models (Moondream, EasyOCR)...\n") - with contextlib.redirect_stdout( - open(os.devnull, "w") - ), contextlib.redirect_stderr(open(os.devnull, "w")): + with ( + contextlib.redirect_stdout(open(os.devnull, "w")), + contextlib.redirect_stderr(open(os.devnull, "w")), + ): if self.easyocr == None and load_easyocr: import easyocr diff --git a/interpreter/core/core.py b/interpreter/core/core.py index b964b745f9..96c9480c16 100644 --- a/interpreter/core/core.py +++ b/interpreter/core/core.py @@ -2,6 +2,7 @@ This file defines the Interpreter class. It's the main file. `from interpreter import interpreter` will import an instance of this class. """ + import json import os import threading diff --git a/interpreter/core/llm/run_text_llm.py b/interpreter/core/llm/run_text_llm.py index 49abcb8403..9aa8c4d1b3 100644 --- a/interpreter/core/llm/run_text_llm.py +++ b/interpreter/core/llm/run_text_llm.py @@ -4,9 +4,7 @@ def run_text_llm(llm, params): if llm.execution_instructions: try: # Add the system message - params["messages"][0][ - "content" - ] += "\n" + llm.execution_instructions + params["messages"][0]["content"] += "\n" + llm.execution_instructions except: print('params["messages"][0]', params["messages"][0]) raise diff --git a/interpreter/core/llm/utils/convert_to_openai_messages.py b/interpreter/core/llm/utils/convert_to_openai_messages.py index aabe2790f8..5aeb49a27a 100644 --- a/interpreter/core/llm/utils/convert_to_openai_messages.py +++ b/interpreter/core/llm/utils/convert_to_openai_messages.py @@ -73,9 +73,9 @@ def convert_to_openai_messages( # especially for the OpenAI service hosted on Azure new_message["content"] = "" else: - new_message[ - "content" - ] = f"""```{message["format"]}\n{message["content"]}\n```""" + new_message["content"] = ( + f"""```{message["format"]}\n{message["content"]}\n```""" + ) elif message["type"] == "console" and message["format"] == "output": if function_calling: @@ -88,9 +88,9 @@ def convert_to_openai_messages( print("\n\n\nStrange chunk found:", message, "\n\n\n") message["content"] = str(message["content"]) if message["content"].strip() == "": - new_message[ - "content" - ] = "No output" # I think it's best to be explicit, but we should test this. + new_message["content"] = ( + "No output" # I think it's best to be explicit, but we should test this. + ) else: new_message["content"] = message["content"] diff --git a/interpreter/core/utils/lazy_import.py b/interpreter/core/utils/lazy_import.py index 4d42046633..b43020db22 100644 --- a/interpreter/core/utils/lazy_import.py +++ b/interpreter/core/utils/lazy_import.py @@ -1,6 +1,7 @@ import importlib.util import sys + def lazy_import(name, optional=True): """Lazily import a module, specified by the name. Useful for optional packages, to speed up startup times.""" # Check if module is already imported diff --git a/interpreter/terminal_interface/profiles/defaults/cerebras.py b/interpreter/terminal_interface/profiles/defaults/cerebras.py index 74cd8c5c62..8632a252de 100644 --- a/interpreter/terminal_interface/profiles/defaults/cerebras.py +++ b/interpreter/terminal_interface/profiles/defaults/cerebras.py @@ -6,9 +6,10 @@ See https://inference-docs.cerebras.ai/introduction for more information. """ -from interpreter import interpreter import os +from interpreter import interpreter + # LLM settings interpreter.llm.api_base = "https://api.cerebras.ai/v1" interpreter.llm.model = "openai/llama3.1-70b" # or "openai/Llama-3.1-8B" diff --git a/interpreter/terminal_interface/profiles/defaults/codestral-few-shot.py b/interpreter/terminal_interface/profiles/defaults/codestral-few-shot.py index 9ad54a3b7c..b8a524dffe 100644 --- a/interpreter/terminal_interface/profiles/defaults/codestral-few-shot.py +++ b/interpreter/terminal_interface/profiles/defaults/codestral-few-shot.py @@ -234,7 +234,8 @@ # Set offline for all local models interpreter.offline = True -import os, platform +import os +import platform # Get the current user's login name username = os.getlogin() diff --git a/interpreter/terminal_interface/profiles/defaults/llama31-database.py b/interpreter/terminal_interface/profiles/defaults/llama31-database.py index f3facbe51f..1510fb2c96 100644 --- a/interpreter/terminal_interface/profiles/defaults/llama31-database.py +++ b/interpreter/terminal_interface/profiles/defaults/llama31-database.py @@ -2,9 +2,10 @@ This is an Open Interpreter profile to chat with a database. """ -from interpreter import interpreter -from datetime import date import os +from datetime import date + +from interpreter import interpreter # Use environment variables for database connection or update defaults with your credentials db_user = os.environ.get("DB_USER", "user") diff --git a/interpreter/terminal_interface/profiles/defaults/obsidian.py b/interpreter/terminal_interface/profiles/defaults/obsidian.py index edfe4df22b..6f371459c5 100644 --- a/interpreter/terminal_interface/profiles/defaults/obsidian.py +++ b/interpreter/terminal_interface/profiles/defaults/obsidian.py @@ -2,9 +2,10 @@ This is an Open Interpreter profile to control an Obsidian vault. """ -from interpreter import interpreter import os +from interpreter import interpreter + # You can hardcode the path to the Obsidian vault or use the environment variable obsidian_directory = os.environ.get("OBSIDIAN_VAULT_PATH") diff --git a/interpreter/terminal_interface/profiles/defaults/screenpipe.py b/interpreter/terminal_interface/profiles/defaults/screenpipe.py index 84f729653c..e408344ffc 100644 --- a/interpreter/terminal_interface/profiles/defaults/screenpipe.py +++ b/interpreter/terminal_interface/profiles/defaults/screenpipe.py @@ -3,9 +3,10 @@ It leverages Llama 3.1 70b served by Groq and requires the environment variable GROQ_API_KEYH to be set. """ +from datetime import datetime, timezone + # Configure Open Interpreter from interpreter import interpreter -from datetime import datetime, timezone interpreter.llm.model = "groq/llama-3.1-70b-versatile" interpreter.computer.import_computer_api = False diff --git a/interpreter/terminal_interface/profiles/defaults/template_profile.py b/interpreter/terminal_interface/profiles/defaults/template_profile.py index 36627836d3..716844790b 100644 --- a/interpreter/terminal_interface/profiles/defaults/template_profile.py +++ b/interpreter/terminal_interface/profiles/defaults/template_profile.py @@ -7,12 +7,12 @@ """ -# Import the interpreter -from interpreter import interpreter - # You can import other libraries too from datetime import date +# Import the interpreter +from interpreter import interpreter + # You can set variables today = date.today() diff --git a/interpreter/terminal_interface/render_past_conversation.py b/interpreter/terminal_interface/render_past_conversation.py index b603df0c33..3f4c5c1666 100644 --- a/interpreter/terminal_interface/render_past_conversation.py +++ b/interpreter/terminal_interface/render_past_conversation.py @@ -2,7 +2,6 @@ This is all messed up.... Uses the old streaming structure. """ - from .components.code_block import CodeBlock from .components.message_block import MessageBlock from .utils.display_markdown_message import display_markdown_message diff --git a/interpreter/terminal_interface/utils/export_to_markdown.py b/interpreter/terminal_interface/utils/export_to_markdown.py index 638e39db95..2560727ca1 100644 --- a/interpreter/terminal_interface/utils/export_to_markdown.py +++ b/interpreter/terminal_interface/utils/export_to_markdown.py @@ -1,6 +1,6 @@ def export_to_markdown(messages: list[dict], export_path: str): markdown = messages_to_markdown(messages) - with open(export_path, 'w') as f: + with open(export_path, "w") as f: f.write(markdown) print(f"Exported current conversation to {export_path}") diff --git a/tests/core/computer/test_computer.py b/tests/core/computer/test_computer.py index 3741cc97d2..406eafef33 100644 --- a/tests/core/computer/test_computer.py +++ b/tests/core/computer/test_computer.py @@ -1,7 +1,9 @@ import unittest from unittest import mock + from interpreter.core.computer.computer import Computer + class TestComputer(unittest.TestCase): def setUp(self): self.computer = Computer(mock.Mock()) @@ -15,12 +17,15 @@ def test_get_all_computer_tools_list(self): def test_get_all_computer_tools_signature_and_description(self): # Act - tools_description = self.computer._get_all_computer_tools_signature_and_description() + tools_description = ( + self.computer._get_all_computer_tools_signature_and_description() + ) # Assert self.assertGreater(len(tools_description), 64) + if __name__ == "__main__": testing = TestComputer() testing.setUp() - testing.test_get_all_computer_tools_signature_and_description() \ No newline at end of file + testing.test_get_all_computer_tools_signature_and_description()