Skip to content

Commit 3c33585

Browse files
committed
Refactor Terminal class in terminal.py to fix llama backtick error in code
1 parent 7c6180c commit 3c33585

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

interpreter/core/computer/terminal/terminal.py

+46-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,48 @@ def __init__(self):
2828
def get_language(self, language):
2929
for lang in self.languages:
3030
if language.lower() == lang.name.lower() or (
31-
hasattr(lang, "aliases") and language in lang.aliases
31+
hasattr(lang, "aliases") and language.lower() in (alias.lower() for alias in lang.aliases)
3232
):
3333
return lang
3434
return None
3535

36-
def run(self, language, code):
36+
def run(self, language, code, stream=False, display=False):
37+
# Llama 3 likes to hallucinate this tick new line thing at the start of code blocks
38+
if code.startswith("`\n"):
39+
code = code[2:].strip()
40+
41+
if code.startswith("```\n"):
42+
code = code[4:].strip()
43+
44+
if stream == False:
45+
# If stream == False, *pull* from _streaming_run.
46+
output_messages = []
47+
for chunk in self._streaming_run(language, code, display=display):
48+
if chunk.get("format") != "active_line":
49+
# Should we append this to the last message, or make a new one?
50+
if (
51+
output_messages != []
52+
and output_messages[-1].get("type") == chunk["type"]
53+
and output_messages[-1].get("format") == chunk["format"]
54+
):
55+
output_messages[-1]["content"] += chunk["content"]
56+
else:
57+
output_messages.append(chunk)
58+
return output_messages
59+
60+
elif stream == True:
61+
# If stream == True, replace this with _streaming_run.
62+
return self._streaming_run(language, code, display=display)
63+
64+
def _streaming_run(self, language, code, display=False):
3765
if language not in self._active_languages:
38-
self._active_languages[language] = self.get_language(language)()
66+
# Get the language. Pass in self.computer *if it takes a single argument*
67+
# but pass in nothing if not. This makes custom languages easier to add / understand.
68+
lang_class = self.get_language(language)
69+
if lang_class.__init__.__code__.co_argcount > 1:
70+
self._active_languages[language] = lang_class(self.computer)
71+
else:
72+
self._active_languages[language] = lang_class()
3973
try:
4074
for chunk in self._active_languages[language].run(code):
4175
# self.format_to_recipient can format some messages as having a certain recipient.
@@ -56,6 +90,14 @@ def run(self, language, code):
5690

5791
yield chunk
5892

93+
# Print it also if display = True
94+
if (
95+
display
96+
and chunk.get("format") != "active_line"
97+
and chunk.get("content")
98+
):
99+
print(chunk["content"], end="")
100+
59101
except GeneratorExit:
60102
self.stop()
61103

@@ -70,4 +112,4 @@ def terminate(self):
70112
language
71113
): # Not sure why this is None sometimes. We should look into this
72114
language.terminate()
73-
del self._active_languages[language_name]
115+
del self._active_languages[language_name]

0 commit comments

Comments
 (0)