Skip to content

Commit 365d4c1

Browse files
committed
Robuster Server
1 parent 2598140 commit 365d4c1

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

interpreter/core/async_core.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def respond(self, run_code=None):
122122
if self.stop_event.is_set():
123123
return
124124

125-
if self.print or self.debug:
125+
if self.print:
126126
if "start" in chunk:
127127
print("\n")
128128
if chunk["type"] in ["code", "console"] and "format" in chunk:
@@ -140,12 +140,15 @@ def respond(self, run_code=None):
140140
)
141141
print(content, end="", flush=True)
142142

143+
if self.debug:
144+
print("Interpreter produced this chunk:", chunk)
145+
143146
self.output_queue.sync_q.put(chunk)
144147

145148
self.output_queue.sync_q.put(complete_message)
146149

147150
if self.print or self.debug:
148-
print("Server response complete.")
151+
print("\nServer response complete.\n")
149152

150153
except Exception as e:
151154
error = traceback.format_exc() + "\n" + str(e)
@@ -464,17 +467,23 @@ async def send_output():
464467
# First, try to send any unsent messages
465468
while async_interpreter.unsent_messages:
466469
output = async_interpreter.unsent_messages[0]
467-
try:
468-
await send_message(output)
470+
if async_interpreter.debug:
471+
print("This was unsent, sending it again:", output)
472+
473+
success = await send_message(output)
474+
if success:
469475
async_interpreter.unsent_messages.popleft()
470-
except Exception:
471-
# If we can't send, break and try again later
472-
break
473476

474477
# If we've sent all unsent messages, get a new output
475478
if not async_interpreter.unsent_messages:
476479
output = await async_interpreter.output()
477-
await send_message(output)
480+
success = await send_message(output)
481+
if not success:
482+
async_interpreter.unsent_messages.append(output)
483+
if async_interpreter.debug:
484+
print(
485+
f"Added message to unsent_messages queue after failed attempts: {output}"
486+
)
478487

479488
except Exception as e:
480489
error = traceback.format_exc() + "\n" + str(e)
@@ -506,16 +515,19 @@ async def send_message(output):
506515
# time.sleep(0.5)
507516

508517
if websocket.client_state != WebSocketState.CONNECTED:
509-
break
518+
return False
510519

511520
try:
512521
# print("sending:", output)
513522

514523
if isinstance(output, bytes):
515524
await websocket.send_bytes(output)
525+
return True # Haven't set up ack for this
516526
else:
517527
if async_interpreter.require_acknowledge:
518528
output["id"] = id
529+
if async_interpreter.debug:
530+
print("Sending this over the websocket:", output)
519531
await websocket.send_text(json.dumps(output))
520532

521533
if async_interpreter.require_acknowledge:
@@ -524,31 +536,38 @@ async def send_message(output):
524536
if id in async_interpreter.acknowledged_outputs:
525537
async_interpreter.acknowledged_outputs.remove(id)
526538
acknowledged = True
539+
if async_interpreter.debug:
540+
print("This output was acknowledged:", output)
527541
break
528542
await asyncio.sleep(0.0001)
529543

530544
if acknowledged:
531-
return
545+
return True
532546
else:
533-
raise Exception("Acknowledgement not received.")
547+
if async_interpreter.debug:
548+
print("Acknowledgement not received for:", output)
549+
return False
534550
else:
535-
return
551+
return True
536552

537553
except Exception as e:
538554
print(
539555
f"Failed to send output on attempt number: {attempt + 1}. Output was: {output}"
540556
)
541557
print(f"Error: {str(e)}")
542-
await asyncio.sleep(0.05)
558+
traceback.print_exc()
559+
await asyncio.sleep(0.01)
543560

544561
# If we've reached this point, we've failed to send after 100 attempts
545562
if output not in async_interpreter.unsent_messages:
546-
async_interpreter.unsent_messages.append(output)
563+
print("Failed to send message:", output)
564+
else:
547565
print(
548-
f"Added message to unsent_messages queue after failed attempts: {output}"
566+
"Failed to send message, also it was already in unsent queue???:",
567+
output,
549568
)
550-
else:
551-
print("Why was this already in unsent_messages?", output)
569+
570+
return False
552571

553572
await asyncio.gather(receive_input(), send_output())
554573

@@ -577,7 +596,8 @@ async def post_input(payload: Dict[str, Any]):
577596
@router.post("/settings")
578597
async def set_settings(payload: Dict[str, Any]):
579598
for key, value in payload.items():
580-
print(f"Updating settings: {key} = {value}")
599+
print("Updating settings...")
600+
# print(f"Updating settings: {key} = {value}")
581601
if key in ["llm", "computer"] and isinstance(value, dict):
582602
if key == "auto_run":
583603
return {

interpreter/core/llm/llm.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,10 @@ def run(self, messages):
279279
if self.interpreter.verbose:
280280
litellm.set_verbose = True
281281

282-
if self.interpreter.debug:
283-
print("\n\n\nOPENAI COMPATIBLE MESSAGES\n\n\n")
282+
if (
283+
self.interpreter.debug == True
284+
): # debug will equal "server" if we're debugging the server specifically
285+
print("\n\n\nOPENAI COMPATIBLE MESSAGES:\n\n\n")
284286
for message in messages:
285287
if len(str(message)) > 5000:
286288
print(str(message)[:200] + "...")
@@ -400,6 +402,8 @@ def fixed_litellm_completions(**params):
400402
attempts = 4
401403
first_error = None
402404

405+
params["num_retries"] = 0
406+
403407
for attempt in range(attempts):
404408
try:
405409
yield from litellm.completion(**params)

interpreter/core/llm/utils/convert_to_openai_messages.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ def convert_to_openai_messages(
8383
new_message["name"] = "execute"
8484
if "content" not in message:
8585
print("What is this??", content)
86+
if type(message["content"]) != str:
87+
if interpreter.debug:
88+
print("\n\n\nStrange chunk found:", message, "\n\n\n")
89+
message["content"] = str(message["content"])
8690
if message["content"].strip() == "":
8791
new_message[
8892
"content"

interpreter/core/render_message.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ def render_message(interpreter, message):
2121
)
2222

2323
# Extract the output content
24-
outputs = (line["content"] for line in output if line.get("format") == "output" and "IGNORE_ALL_ABOVE_THIS_LINE" not in line["content"])
24+
outputs = (
25+
line["content"]
26+
for line in output
27+
if line.get("format") == "output"
28+
and "IGNORE_ALL_ABOVE_THIS_LINE" not in line["content"]
29+
)
2530

2631
# Replace the part with the output
2732
parts[i] = "\n".join(outputs)
2833

2934
# Join the parts back into the message
3035
rendered_message = "".join(parts).strip()
3136

32-
if interpreter.debug:
37+
if (
38+
interpreter.debug == True
39+
): # debug will equal "server" if we're debugging the server specifically
3340
print("\n\n\nSYSTEM MESSAGE\n\n\n")
3441
print(rendered_message)
3542
print("\n\n\n")

0 commit comments

Comments
 (0)