Skip to content

Commit b26054c

Browse files
committed
Common hallucination fix
1 parent d0a59e2 commit b26054c

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

interpreter/core/respond.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,34 @@ def respond(interpreter):
150150

151151
# A common hallucination
152152
if code.startswith("functions.execute("):
153-
code = code.replace("functions.execute(", "").rstrip(")")
154-
code_dict = json.loads(code)
155-
language = code_dict.get("language", language)
156-
code = code_dict.get("code", code)
157-
interpreter.messages[-1]["content"] = code # So the LLM can see it.
158-
interpreter.messages[-1][
159-
"format"
160-
] = language # So the LLM can see it.
153+
edited_code = code.replace("functions.execute(", "").rstrip(")")
154+
try:
155+
code_dict = json.loads(edited_code)
156+
language = code_dict.get("language", language)
157+
code = code_dict.get("code", code)
158+
interpreter.messages[-1][
159+
"content"
160+
] = code # So the LLM can see it.
161+
interpreter.messages[-1][
162+
"format"
163+
] = language # So the LLM can see it.
164+
except:
165+
pass
166+
167+
if code.replace("\n", "").replace(" ", "").startswith('{"language":'):
168+
try:
169+
code_dict = json.loads(code)
170+
if set(code_dict.keys()) == {"language", "code"}:
171+
language = code_dict["language"]
172+
code = code_dict["code"]
173+
interpreter.messages[-1][
174+
"content"
175+
] = code # So the LLM can see it.
176+
interpreter.messages[-1][
177+
"format"
178+
] = language # So the LLM can see it.
179+
except:
180+
pass
161181

162182
if language == "text" or language == "markdown":
163183
# It does this sometimes just to take notes. Let it, it's useful.

tests/test_interpreter.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@
2222
from websocket import create_connection
2323

2424

25+
def test_hallucinations():
26+
# We should be resiliant to common hallucinations.
27+
28+
code = """{
29+
"language": "python",
30+
"code": "10+12"
31+
}"""
32+
33+
interpreter.messages = [
34+
{"role": "assistant", "type": "code", "format": "python", "content": code}
35+
]
36+
for chunk in interpreter._respond_and_store():
37+
if chunk.get("format") == "output":
38+
assert chunk.get("content") == "22"
39+
break
40+
41+
code = """functions.execute({
42+
"language": "python",
43+
"code": "10+12"
44+
})"""
45+
46+
interpreter.messages = [
47+
{"role": "assistant", "type": "code", "format": "python", "content": code}
48+
]
49+
for chunk in interpreter._respond_and_store():
50+
if chunk.get("format") == "output":
51+
assert chunk.get("content") == "22"
52+
break
53+
54+
2555
@pytest.mark.skip(reason="Requires uvicorn, which we don't require by default")
2656
def test_server():
2757
# os.system("pip install 'open-interpreter[server]'")

0 commit comments

Comments
 (0)