From 4b1e0a13627c62f4dff33e009c453c6d8b4f60d9 Mon Sep 17 00:00:00 2001 From: cyai <83634399+cyai@users.noreply.github.com> Date: Tue, 23 Jul 2024 22:36:05 +0530 Subject: [PATCH] refactor: change format based on black and isort --- interpreter/core/computer/calendar/calendar.py | 2 +- .../core/computer/clipboard/clipboard.py | 4 +++- 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 ++++++----- interpreter/core/computer/terminal/terminal.py | 2 +- interpreter/core/llm/run_text_llm.py | 4 +--- interpreter/core/render_message.py | 7 ++++++- interpreter/core/utils/lazy_import.py | 1 + .../profiles/defaults/codestral-few-shot.py | 3 ++- 11 files changed, 35 insertions(+), 22 deletions(-) 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/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 d3bd397930..6fff2bef0f 100644 --- a/interpreter/core/computer/terminal/terminal.py +++ b/interpreter/core/computer/terminal/terminal.py @@ -4,6 +4,7 @@ from ..utils.recipient_utils import parse_for_recipient from .languages.applescript import AppleScript from .languages.html import HTML +from .languages.java import Java from .languages.javascript import JavaScript from .languages.powershell import PowerShell from .languages.python import Python @@ -11,7 +12,6 @@ from .languages.react import React from .languages.ruby import Ruby from .languages.shell import Shell -from .languages.java import Java # Should this be renamed to OS or System? 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/render_message.py b/interpreter/core/render_message.py index 874709c4fa..7aaac2c70a 100644 --- a/interpreter/core/render_message.py +++ b/interpreter/core/render_message.py @@ -21,7 +21,12 @@ def render_message(interpreter, message): ) # Extract the output content - outputs = (line["content"] for line in output if line.get("format") == "output" and "IGNORE_ALL_ABOVE_THIS_LINE" not in line["content"]) + outputs = ( + line["content"] + for line in output + if line.get("format") == "output" + and "IGNORE_ALL_ABOVE_THIS_LINE" not in line["content"] + ) # Replace the part with the output parts[i] = "\n".join(outputs) 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/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()