Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Feb 19, 2024
2 parents 38e0358 + 52fecdd commit 483401e
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 384 deletions.
3 changes: 3 additions & 0 deletions interpreter/core/computer/computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __init__(self):
self.save_skills = True
# self.api_base = "http://0.0.0.0/v0"

self.import_computer_api = True
self._has_imported_computer_api = False # Because we only want to do this once

# Shortcut for computer.terminal.languages
@property
def languages(self):
Expand Down
21 changes: 6 additions & 15 deletions interpreter/core/computer/skills/skills.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import glob
import inspect
import os
from pathlib import Path

from ....terminal_interface.utils.oi_dir import oi_dir
from ...utils.lazy_import import lazy_import

# Lazy import of aifs, imported when needed to speed up start time
Expand All @@ -11,25 +13,14 @@
class Skills:
def __init__(self, computer):
self.computer = computer
self.skills_dir = None
self.path = str(Path(oi_dir) / "skills")

def search(self, query, module=None, paths=None):
if paths:
return aifs.search(query, file_paths=paths, python_docstrings_only=True)

if module is None:
module = self.computer

# Get the path of the module
module_path = os.path.dirname(inspect.getfile(module.__class__))

# Use aifs to search over the files in the module path
results = aifs.search(query, path=module_path, python_docstrings_only=True)
return results
def search(self, query):
return aifs.search(query, self.path, python_docstrings_only=True)

def import_skills(self):
self.computer.save_skills = False
for file in glob.glob(os.path.join(self.skills_dir, "*.py")):
for file in glob.glob(os.path.join(self.path, "*.py")):
with open(file, "r") as f:
self.computer.run("python", f.read())
self.computer.save_skills = True
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def run(self, code):
functions = {}

if self.computer.save_skills and functions:
skill_library_path = self.computer.skills.skills_dir
skill_library_path = self.computer.skills.path

if not os.path.exists(skill_library_path):
os.makedirs(skill_library_path)
Expand Down
14 changes: 14 additions & 0 deletions interpreter/core/computer/terminal/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ def get_language(self, language):
return None

def run(self, language, code, stream=False, display=False):
if (
language == "python"
and self.computer.import_computer_api
and "computer" in code
):
if not self.computer._has_imported_computer_api:
self.computer._has_imported_computer_api = True
# Give it access to the computer via Python
self.computer.run(
language="python",
code="import time\nfrom interpreter import interpreter\ncomputer = interpreter.computer", # We ask it to use time, so
display=self.computer.verbose,
)

if stream == False:
# If stream == False, *pull* from _streaming_run.
output_messages = []
Expand Down
20 changes: 12 additions & 8 deletions interpreter/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import threading
import time
from datetime import datetime
from pathlib import Path

from ..terminal_interface.terminal_interface import terminal_interface
from ..terminal_interface.utils.display_markdown_message import display_markdown_message
Expand Down Expand Up @@ -61,7 +60,9 @@ def __init__(
system_message=default_system_message,
custom_instructions="",
computer=None,
skills_dir=None,
sync_computer=True,
import_computer_api=True,
skills_path=None,
import_skills=True,
):
# State
Expand Down Expand Up @@ -98,12 +99,15 @@ def __init__(

# Computer
self.computer = Computer() if computer is None else computer
self.sync_computer = (
True # Sync the interpreter's computer with the user's interpreter.computer
)
self.computer.skills.skills_dir = (
skills_dir if skills_dir else str(Path(oi_dir) / "skills")
)

self.sync_computer = sync_computer
self.computer.import_computer_api = import_computer_api

# Skills
if skills_path:
self.computer.skills.path = skills_path

self.import_skills = import_skills
if import_skills:
self.computer.skills.import_skills()

Expand Down
7 changes: 6 additions & 1 deletion interpreter/core/render_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def render_message(interpreter, message):
outputs = []
for line in output:
if line.get("format") == "output":
outputs.append(line["content"])
if "IGNORE_ALL_ABOVE_THIS_LINE" in line["content"]:
outputs.append(
line["content"].split("IGNORE_ALL_ABOVE_THIS_LINE")[1]
)
else:
outputs.append(line["content"])
output = "\n".join(outputs)

# Replace the part with the output
Expand Down
16 changes: 10 additions & 6 deletions interpreter/core/respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def respond(interpreter):
system_message += "\n\n" + interpreter.custom_instructions

# Storing the messages so they're accessible in the interpreter's computer
output = interpreter.computer.run("python", f"messages={interpreter.messages}")
if interpreter.sync_computer:
output = interpreter.computer.run(
"python", f"messages={interpreter.messages}"
)

## Rendering ↓
rendered_system_message = render_message(interpreter, system_message)
Expand Down Expand Up @@ -133,7 +136,7 @@ def respond(interpreter):
language = interpreter.messages[-1]["format"].lower().strip()
code = interpreter.messages[-1]["content"]

if interpreter.os and language == "text":
if language == "text":
# It does this sometimes just to take notes. Let it, it's useful.
# In the future we should probably not detect this behavior as code at all.
continue
Expand Down Expand Up @@ -173,8 +176,8 @@ def respond(interpreter):
# We need to tell python what we (the generator) should do if they exit
break

# don't let it import computer on os mode — we handle that!
if interpreter.sync_computer and language == "python":
# don't let it import computer — we handle that!
if interpreter.computer.import_computer_api and language == "python":
code = code.replace("import computer\n", "pass\n")
code = re.sub(
r"import computer\.(\w+) as (\w+)", r"\2 = computer.\1", code
Expand Down Expand Up @@ -224,7 +227,7 @@ def respond(interpreter):

# sync up your computer with the interpreter's computer
try:
if interpreter.os and language == "python":
if interpreter.sync_computer and language == "python":
# sync up the interpreter's computer with your computer
result = interpreter.computer.run(
"python",
Expand Down Expand Up @@ -259,7 +262,7 @@ def respond(interpreter):
## FORCE TASK COMLETION
# This makes it utter specific phrases if it doesn't want to be told to "Proceed."

force_task_completion_message = """Proceed. You CAN run code on my machine. If you want to run code, start your message with "```"! If the entire task I asked for is done, say exactly 'The task is done.' If it's impossible, say 'The task is impossible.' (If I haven't provided a task, say exactly 'Let me know what you'd like to do next.') Otherwise keep going."""
force_task_completion_message = """Proceed. You CAN run code on my machine. If you want to run code, start your message with "```"! If the entire task I asked for is done, say exactly 'The task is done.' If you need some specific information (like username or password) say EXACTLY 'Please provide more information.' If it's impossible, say 'The task is impossible.' (If I haven't provided a task, say exactly 'Let me know what you'd like to do next.') Otherwise keep going."""
if interpreter.os:
force_task_completion_message.replace(
"If the entire task I asked for is done,",
Expand All @@ -269,6 +272,7 @@ def respond(interpreter):
"the task is done.",
"the task is impossible.",
"let me know what you'd like to do next.",
"please provide more information.",
]

if (
Expand Down
6 changes: 0 additions & 6 deletions interpreter/terminal_interface/profiles/defaults/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@
# if chunk.get("format") != "active_line":
# print(chunk.get("content"))

# Give it access to the computer via Python
interpreter.computer.run(
language="python",
code="import time\nfrom interpreter import interpreter\ncomputer = interpreter.computer", # We ask it to use time, so
display=interpreter.verbose,
)

if not interpreter.auto_run:
interpreter.display_message(
Expand Down
Loading

0 comments on commit 483401e

Please sign in to comment.