Skip to content

Commit c917a21

Browse files
committed
Added jupyter retries, minor robust fixes
1 parent 0e95f01 commit c917a21

File tree

8 files changed

+209
-200
lines changed

8 files changed

+209
-200
lines changed

interpreter/core/async_core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,16 @@ async def receive_input():
441441
"type": "error",
442442
"content": traceback.format_exc() + "\n" + str(e),
443443
}
444-
await websocket.send_text(json.dumps(error_message))
445-
await websocket.send_text(json.dumps(complete_message))
446-
print("\n\n--- SENT ERROR: ---\n\n")
444+
if websocket.client_state == WebSocketState.CONNECTED:
445+
await websocket.send_text(json.dumps(error_message))
446+
await websocket.send_text(json.dumps(complete_message))
447+
print("\n\n--- SENT ERROR: ---\n\n")
448+
else:
449+
print(
450+
"\n\n--- ERROR (not sent due to disconnected state): ---\n\n"
451+
)
447452
print(error)
448-
print("\n\n--- (ERROR ABOVE WAS SENT) ---\n\n")
453+
print("\n\n--- (ERROR ABOVE) ---\n\n")
449454

450455
async def send_output():
451456
while True:

interpreter/core/computer/terminal/languages/jupyter_language.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import ast
77
import logging
8-
import sys
98
import os
109
import queue
1110
import re
11+
import sys
1212
import threading
1313
import time
1414
import traceback
@@ -21,8 +21,8 @@
2121

2222
# When running from an executable, ipykernel calls itself infinitely
2323
# This is a workaround to detect it and launch it manually
24-
if 'ipykernel_launcher' in sys.argv:
25-
if sys.path[0] == '':
24+
if "ipykernel_launcher" in sys.argv:
25+
if sys.path[0] == "":
2626
del sys.path[0]
2727

2828
from ipykernel import kernelapp as app
@@ -127,6 +127,7 @@ def run(self, code):
127127

128128
def _execute_code(self, code, message_queue):
129129
def iopub_message_listener():
130+
max_retries = 100
130131
while True:
131132
# If self.finish_flag = True, and we didn't set it (we do below), we need to stop. That's our "stop"
132133
if self.finish_flag == True:
@@ -138,6 +139,12 @@ def iopub_message_listener():
138139
msg = self.kc.iopub_channel.get_msg(timeout=0.05)
139140
except queue.Empty:
140141
continue
142+
except Exception as e:
143+
max_retries -= 1
144+
if max_retries < 0:
145+
raise
146+
print("Jupyter error, retrying:", str(e))
147+
continue
141148

142149
if DEBUG_MODE:
143150
print("-----------" * 10)
@@ -248,6 +255,8 @@ def detect_active_line(self, line):
248255

249256
def _capture_output(self, message_queue):
250257
while True:
258+
time.sleep(0.1)
259+
251260
# For async usage
252261
if (
253262
hasattr(self.computer.interpreter, "stop_event")
@@ -263,10 +272,17 @@ def _capture_output(self, message_queue):
263272
yield output
264273
except queue.Empty:
265274
if self.finish_flag:
266-
if DEBUG_MODE:
267-
print("we're done")
268-
break
269-
time.sleep(0.1)
275+
time.sleep(0.1)
276+
277+
try:
278+
output = message_queue.get(timeout=0.1)
279+
if DEBUG_MODE:
280+
print(output)
281+
yield output
282+
except queue.Empty:
283+
if DEBUG_MODE:
284+
print("we're done")
285+
break
270286

271287
def stop(self):
272288
self.finish_flag = True

interpreter/core/llm/llm.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ def run(self, messages):
286286
print(message)
287287
print("\n")
288288
print("\n\n\n")
289-
time.sleep(5)
290289

291290
if self.supports_functions:
292291
# yield from run_function_calling_llm(self, params)

interpreter/core/llm/run_tool_calling_llm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ def run_tool_calling_llm(llm, request_params):
312312
else:
313313
# If name exists and it's not "execute" or "python" or "functions", who knows what's going on.
314314
if "name" in accumulated_deltas["function_call"]:
315-
yield {
316-
"type": "code",
317-
"format": "python",
318-
"content": accumulated_deltas["function_call"]["name"],
319-
}
315+
# yield {
316+
# "type": "code",
317+
# "format": "python",
318+
# "content": str(delta),
319+
# }
320320
return

interpreter/terminal_interface/start_terminal_interface.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ def start_terminal_interface(interpreter):
2222
Meant to be used from the command line. Parses arguments, starts OI's terminal interface.
2323
"""
2424

25+
# Instead use an async interpreter, which has a server. Set settings on that
26+
if "--server" in sys.argv:
27+
from interpreter import AsyncInterpreter
28+
29+
interpreter = AsyncInterpreter()
30+
2531
arguments = [
2632
{
2733
"name": "profile",
@@ -371,12 +377,6 @@ def print_help(self, *args, **kwargs):
371377

372378
args, unknown_args = parser.parse_known_args()
373379

374-
if args.server:
375-
# Instead use an async interpreter, which has a server. Set settings on that
376-
from interpreter import AsyncInterpreter
377-
378-
interpreter = AsyncInterpreter()
379-
380380
# handle unknown arguments
381381
if unknown_args:
382382
print(f"\nUnrecognized argument(s): {unknown_args}")

0 commit comments

Comments
 (0)