Skip to content

Commit

Permalink
Fully Working. All functionality tested, but still havent done idiot …
Browse files Browse the repository at this point in the history
…testing.
  • Loading branch information
unaidedelf8777 committed Oct 5, 2023
1 parent 54b2451 commit 03fb194
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 177 deletions.
40 changes: 33 additions & 7 deletions interpreter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
from .core.core import Interpreter
import sys
from .core.core import Interpreter
from .cli.cli import cli


# This is done so when users `import interpreter`,
# they get an instance of interpreter:

sys.modules["interpreter"] = Interpreter()
def create_interpreter(**kwargs):
"""
Factory function to create an instance of Interpreter with the provided keyword arguments.
Parameters:
**kwargs: Keyword arguments to be set as attributes in the Interpreter instance.
Returns:
An instance of Interpreter initialized with the provided arguments.
"""
# Create a new interpreter instance
new_interpreter = Interpreter()

# Iterate through the provided keyword arguments
for key, value in kwargs.items():
# Check if the attribute exists in the interpreter
if hasattr(new_interpreter, key):
# Check if the provided value is of the correct type
if isinstance(value, type(getattr(new_interpreter, key))):
setattr(new_interpreter, key, value)
else:
print(
f"Type mismatch: '{key}' should be of type {type(getattr(new_interpreter, key))}. Using the default value instead.")

else:
print(
f"Unknown attribute: '{key}'. Ignoring.")


return new_interpreter

# **This is a controversial thing to do,**
# because perhaps modules ought to behave like modules.

# But I think it saves a step, removes friction, and looks good.

# ____ ____ __ __
# / __ \____ ___ ____ / _/___ / /____ _________ ________ / /____ _____
Expand Down
5 changes: 4 additions & 1 deletion interpreter/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import appdirs
from ..utils.display_markdown_message import display_markdown_message
from ..terminal_interface.conversation_navigator import conversation_navigator
from ..core.core import Interpreter

arguments = [
{
Expand Down Expand Up @@ -90,7 +91,7 @@
}
]

def cli(interpreter):
def cli():

parser = argparse.ArgumentParser(description="Open Interpreter")

Expand All @@ -112,6 +113,8 @@ def cli(interpreter):

args = parser.parse_args()

interpreter = Interpreter()

# This should be pushed into an open_config.py util
# If --config is used, open the config.yaml file in the Open Interpreter folder of the user's config dir
if args.config:
Expand Down
2 changes: 0 additions & 2 deletions interpreter/code_interpreters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from .dockerfiles.docker_manager import DockerManager as ContainerConfig

__all__ = ["ContainerConfig"]
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def build_docker_images(
images = client.images.list(name=image_name, all=True)
if not images:
Print("Downloading default image from Docker Hub, please wait...")

subprocess.run(["docker", "pull", "unaidedelf/openinterpreter-runtime-container:latest"])
subprocess.run(["docker", "tag", "unaidedelf/openinterpreter-runtime-container:latest", image_name ],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
Expand Down Expand Up @@ -306,7 +307,7 @@ def init_exec_instance(self, command):
# since docker sets up the socketio wierd and tries to make it hard to mess with and write to.
# We make the socket "Cooperative"
self.exec_socket = self.client.exec_start(
self.exec_id, socket=True, tty=False, demux=False)._sock
self.exec_id, socket=True, tty=False, demux=False)._sock # type: ignore


def wait_for_container_start(self, container_id, timeout=30):
Expand Down
30 changes: 13 additions & 17 deletions interpreter/code_interpreters/dockerfiles/docker_manager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os

class DockerManager:
def __init__(self, requirements_file='requirements.txt', docker_file='Dockerfile'):
self.requirements_file = requirements_file
self.docker_file = docker_file

here = os.path.abspath(__file__)
requirements_file = os.path.normpath(os.path.join(here, "..", "requirements.txt"))
docker_file = os.path.normpath(os.path.join(here,"..", "Dockerfile"))

def add_dependency(self, language, dependency):
def add_dependency(language, dependency):
lines = []
language_section_found = False
dependency_name = dependency.split('==')[0]

with open(self.requirements_file, 'r') as f:
with open(DockerManager.requirements_file, 'r') as f:
lines = f.readlines()

for i, line in enumerate(lines):
Expand All @@ -28,14 +31,14 @@ def add_dependency(self, language, dependency):

lines.insert(i, f"{dependency}\n")

with open(self.requirements_file, 'w') as f:
with open(DockerManager.requirements_file, 'w') as f:
f.writelines(lines)

def remove_dependency(self, language, dependency_name):
def remove_dependency(language, dependency_name):
lines = []
language_section_found = False

with open(self.requirements_file, 'r') as f:
with open(DockerManager.requirements_file, 'r') as f:
lines = f.readlines()

for i, line in enumerate(lines):
Expand All @@ -49,15 +52,8 @@ def remove_dependency(self, language, dependency_name):
del lines[i]
break
else:
raise ValueError(f"Error: Language section [{language}] or dependency {dependency_name} not found. please add the language using the '.add_language' method")
raise ValueError(f"Error: Language section [{language}] or dependency {dependency_name} not found.")

with open(self.requirements_file, 'w') as f:
with open(DockerManager.requirements_file, 'w') as f:
f.writelines(lines)


def add_language(self, language, install_command):
with open(self.docker_file, 'a') as f:
f.write(f'\n# Install {language}\nRUN {install_command}\n')

with open(self.requirements_file, 'a') as f:
f.write(f"\n[{language}]\n")
12 changes: 5 additions & 7 deletions interpreter/core/core.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""
This file defines the Interpreter class.
It's the main file. `import interpreter` will import an instance of this class.
running ```import interpreter``` followed by ```interpreter.create_interpreter(**kwargs)``` will create an instance of this class.
"""
from interpreter.utils import display_markdown_message
from ..cli.cli import cli
from ..utils.get_config import get_config
from .respond import respond
from ..llm.setup_llm import setup_llm
Expand All @@ -18,8 +16,6 @@
from ..code_interpreters.container_utils.container_utils import build_docker_images

class Interpreter:
def cli(self):
cli(self)

def __init__(self):
# State
Expand Down Expand Up @@ -49,12 +45,14 @@ def __init__(self):
self.max_budget = None
self._llm = None

# Container options
self.use_containers = False

# Load config defaults
config = get_config()
self.__dict__.update(config)

# Container options
self.use_containers = False



# Check for update
Expand Down
Loading

0 comments on commit 03fb194

Please sign in to comment.