forked from OpenInterpreter/open-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_interpreter.py
1327 lines (1093 loc) · 49.9 KB
/
test_interpreter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import platform
import signal
import time
from random import randint
import pytest
#####
from interpreter import AsyncInterpreter, OpenInterpreter
from interpreter.terminal_interface.utils.count_tokens import (
count_messages_tokens,
count_tokens,
)
interpreter = OpenInterpreter()
#####
import multiprocessing
import threading
import time
import pytest
from websocket import create_connection
def test_hallucinations():
# We should be resiliant to common hallucinations.
code = """10+12executeexecute\n"""
interpreter.messages = [
{"role": "assistant", "type": "code", "format": "python", "content": code}
]
for chunk in interpreter._respond_and_store():
if chunk.get("format") == "output":
assert chunk.get("content") == "22"
break
code = """{
"language": "python",
"code": "10+12"
}"""
interpreter.messages = [
{"role": "assistant", "type": "code", "format": "python", "content": code}
]
for chunk in interpreter._respond_and_store():
if chunk.get("format") == "output":
assert chunk.get("content") == "22"
break
code = """functions.execute({
"language": "python",
"code": "10+12"
})"""
interpreter.messages = [
{"role": "assistant", "type": "code", "format": "python", "content": code}
]
for chunk in interpreter._respond_and_store():
if chunk.get("format") == "output":
assert chunk.get("content") == "22"
break
code = """{language: "python", code: "print('hello')" }"""
interpreter.messages = [
{"role": "assistant", "type": "code", "format": "python", "content": code}
]
for chunk in interpreter._respond_and_store():
if chunk.get("format") == "output":
assert chunk.get("content").strip() == "hello"
break
def run_auth_server():
os.environ["INTERPRETER_REQUIRE_ACKNOWLEDGE"] = "True"
os.environ["INTERPRETER_API_KEY"] = "testing"
async_interpreter = AsyncInterpreter()
async_interpreter.print = False
async_interpreter.server.run()
# @pytest.mark.skip(reason="Requires uvicorn, which we don't require by default")
def test_authenticated_acknowledging_breaking_server():
"""
Test the server when we have authentication and acknowledging one.
I know this is bad, just trying to test quickly!
"""
# Start the server in a new process
process = multiprocessing.Process(target=run_auth_server)
process.start()
# Give the server a moment to start
time.sleep(2)
import asyncio
import json
import requests
import websockets
async def test_fastapi_server():
import asyncio
async with websockets.connect("ws://localhost:8000/") as websocket:
# Connect to the websocket
print("Connected to WebSocket")
# Sending message via WebSocket
await websocket.send(json.dumps({"auth": "testing"}))
# Sending POST request
post_url = "http://localhost:8000/settings"
settings = {
"llm": {
"model": "gpt-4o",
"execution_instructions": "",
"supports_functions": False,
},
"system_message": "You are a poem writing bot. Do not do anything but respond with a poem.",
"auto_run": True,
}
response = requests.post(
post_url, json=settings, headers={"X-API-KEY": "testing"}
)
print("POST request sent, response:", response.json())
# Sending messages via WebSocket
await websocket.send(
json.dumps({"role": "user", "type": "message", "start": True})
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "message",
"content": "Write a short poem about Seattle.",
}
)
)
await websocket.send(
json.dumps({"role": "user", "type": "message", "end": True})
)
print("WebSocket chunks sent")
max_chunks = 5
poem = ""
while True:
max_chunks -= 1
if max_chunks == 0:
break
message = await websocket.recv()
message_data = json.loads(message)
if "id" in message_data:
await websocket.send(json.dumps({"ack": message_data["id"]}))
if "error" in message_data:
raise Exception(str(message_data))
print("Received from WebSocket:", message_data)
if type(message_data.get("content")) == str:
poem += message_data.get("content")
print(message_data.get("content"), end="", flush=True)
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
raise (
Exception(
"It shouldn't have finished this soon, accumulated_content is: "
+ accumulated_content
)
)
await websocket.close()
print("Disconnected from WebSocket")
time.sleep(3)
# Now let's hilariously keep going
print("RESUMING")
async with websockets.connect("ws://localhost:8000/") as websocket:
# Connect to the websocket
print("Connected to WebSocket")
# Sending message via WebSocket
await websocket.send(json.dumps({"auth": "testing"}))
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "id" in message_data:
await websocket.send(json.dumps({"ack": message_data["id"]}))
if "error" in message_data:
raise Exception(str(message_data))
print("Received from WebSocket:", message_data)
message_data.pop("id", "")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
break
if type(message_data.get("content")) == str:
poem += message_data.get("content")
print(message_data.get("content"), end="", flush=True)
time.sleep(1)
print("Is this a normal poem?")
print(poem)
time.sleep(1)
# Get the current event loop and run the test function
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(test_fastapi_server())
finally:
# Kill server process
process.terminate()
os.kill(process.pid, signal.SIGKILL) # Send SIGKILL signal
process.join()
def run_server():
os.environ["INTERPRETER_REQUIRE_ACKNOWLEDGE"] = "False"
if "INTERPRETER_API_KEY" in os.environ:
del os.environ["INTERPRETER_API_KEY"]
async_interpreter = AsyncInterpreter()
async_interpreter.print = False
async_interpreter.server.run()
# @pytest.mark.skip(reason="Requires uvicorn, which we don't require by default")
def test_server():
# Start the server in a new process
process = multiprocessing.Process(target=run_server)
process.start()
# Give the server a moment to start
time.sleep(2)
import asyncio
import json
import requests
import websockets
async def test_fastapi_server():
import asyncio
async with websockets.connect("ws://localhost:8000/") as websocket:
# Connect to the websocket
print("Connected to WebSocket")
# Sending message via WebSocket
await websocket.send(json.dumps({"auth": "dummy-api-key"}))
# Sending POST request
post_url = "http://localhost:8000/settings"
settings = {
"llm": {"model": "gpt-4o-mini"},
"messages": [
{
"role": "user",
"type": "message",
"content": "The secret word is 'crunk'.",
},
{"role": "assistant", "type": "message", "content": "Understood."},
],
"custom_instructions": "",
"auto_run": True,
}
response = requests.post(post_url, json=settings)
print("POST request sent, response:", response.json())
# Sending messages via WebSocket
await websocket.send(
json.dumps({"role": "user", "type": "message", "start": True})
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "message",
"content": "What's the secret word?",
}
)
)
await websocket.send(
json.dumps({"role": "user", "type": "message", "end": True})
)
print("WebSocket chunks sent")
# Wait for a specific response
accumulated_content = ""
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "error" in message_data:
raise Exception(message_data["content"])
print("Received from WebSocket:", message_data)
if type(message_data.get("content")) == str:
accumulated_content += message_data.get("content")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
print("Received expected message from server")
break
assert "crunk" in accumulated_content
# Send another POST request
post_url = "http://localhost:8000/settings"
settings = {
"llm": {"model": "gpt-4o-mini"},
"messages": [
{
"role": "user",
"type": "message",
"content": "The secret word is 'barloney'.",
},
{"role": "assistant", "type": "message", "content": "Understood."},
],
"custom_instructions": "",
"auto_run": True,
}
response = requests.post(post_url, json=settings)
print("POST request sent, response:", response.json())
# Sending messages via WebSocket
await websocket.send(
json.dumps({"role": "user", "type": "message", "start": True})
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "message",
"content": "What's the secret word?",
}
)
)
await websocket.send(
json.dumps({"role": "user", "type": "message", "end": True})
)
print("WebSocket chunks sent")
# Wait for a specific response
accumulated_content = ""
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "error" in message_data:
raise Exception(message_data["content"])
print("Received from WebSocket:", message_data)
if message_data.get("content"):
accumulated_content += message_data.get("content")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
print("Received expected message from server")
break
assert "barloney" in accumulated_content
# Send another POST request
post_url = "http://localhost:8000/settings"
settings = {
"messages": [],
"custom_instructions": "",
"auto_run": False,
"verbose": False,
}
response = requests.post(post_url, json=settings)
print("POST request sent, response:", response.json())
# Sending messages via WebSocket
await websocket.send(
json.dumps({"role": "user", "type": "message", "start": True})
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "message",
"content": "What's 239023*79043? Use Python.",
}
)
)
await websocket.send(
json.dumps({"role": "user", "type": "message", "end": True})
)
print("WebSocket chunks sent")
# Wait for response
accumulated_content = ""
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "error" in message_data:
raise Exception(message_data["content"])
print("Received from WebSocket:", message_data)
if message_data.get("content"):
accumulated_content += message_data.get("content")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
print("Received expected message from server")
break
time.sleep(5)
# Send a GET request to /settings/messages
get_url = "http://localhost:8000/settings/messages"
response = requests.get(get_url)
print("GET request sent, response:", response.json())
# Assert that the last message has a type of 'code'
response_json = response.json()
if isinstance(response_json, str):
response_json = json.loads(response_json)
messages = response_json["messages"] if "messages" in response_json else []
assert messages[-1]["type"] == "code"
assert "18893094989" not in accumulated_content.replace(",", "")
# Send go message
await websocket.send(
json.dumps({"role": "user", "type": "command", "start": True})
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "command",
"content": "go",
}
)
)
await websocket.send(
json.dumps({"role": "user", "type": "command", "end": True})
)
# Wait for a specific response
accumulated_content = ""
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "error" in message_data:
raise Exception(message_data["content"])
print("Received from WebSocket:", message_data)
if message_data.get("content"):
if type(message_data.get("content")) == str:
accumulated_content += message_data.get("content")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
print("Received expected message from server")
break
assert "18893094989" in accumulated_content.replace(",", "")
#### TEST FILE ####
# Send another POST request
post_url = "http://localhost:8000/settings"
settings = {"messages": [], "auto_run": True}
response = requests.post(post_url, json=settings)
print("POST request sent, response:", response.json())
# Sending messages via WebSocket
await websocket.send(json.dumps({"role": "user", "start": True}))
print("sent", json.dumps({"role": "user", "start": True}))
await websocket.send(
json.dumps(
{
"role": "user",
"type": "message",
"content": "Does this file exist?",
}
)
)
print(
"sent",
{
"role": "user",
"type": "message",
"content": "Does this file exist?",
},
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "file",
"format": "path",
"content": "/something.txt",
}
)
)
print(
"sent",
{
"role": "user",
"type": "file",
"format": "path",
"content": "/something.txt",
},
)
await websocket.send(json.dumps({"role": "user", "end": True}))
print("WebSocket chunks sent")
# Wait for response
accumulated_content = ""
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "error" in message_data:
raise Exception(message_data["content"])
print("Received from WebSocket:", message_data)
if type(message_data.get("content")) == str:
accumulated_content += message_data.get("content")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
print("Received expected message from server")
break
# Get messages
get_url = "http://localhost:8000/settings/messages"
response_json = requests.get(get_url).json()
print("GET request sent, response:", response_json)
if isinstance(response_json, str):
response_json = json.loads(response_json)
messages = response_json["messages"]
response = interpreter.computer.ai.chat(
str(messages)
+ "\n\nIn the conversation above, does the assistant think the file exists? Yes or no? Only reply with one word— 'yes' or 'no'."
)
assert response.strip(" \n.").lower() == "no"
#### TEST IMAGES ####
# Send another POST request
post_url = "http://localhost:8000/settings"
settings = {"messages": [], "auto_run": True}
response = requests.post(post_url, json=settings)
print("POST request sent, response:", response.json())
base64png = "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMO0TAAD//2Anhf4QtqobAAAAAElFTkSuQmCC"
# Sending messages via WebSocket
await websocket.send(json.dumps({"role": "user", "start": True}))
await websocket.send(
json.dumps(
{
"role": "user",
"type": "message",
"content": "describe this image",
}
)
)
await websocket.send(
json.dumps(
{
"role": "user",
"type": "image",
"format": "base64.png",
"content": base64png,
}
)
)
# await websocket.send(
# json.dumps(
# {
# "role": "user",
# "type": "image",
# "format": "path",
# "content": "/Users/killianlucas/Documents/GitHub/open-interpreter/screen.png",
# }
# )
# )
await websocket.send(json.dumps({"role": "user", "end": True}))
print("WebSocket chunks sent")
# Wait for response
accumulated_content = ""
while True:
message = await websocket.recv()
message_data = json.loads(message)
if "error" in message_data:
raise Exception(message_data["content"])
print("Received from WebSocket:", message_data)
if type(message_data.get("content")) == str:
accumulated_content += message_data.get("content")
if message_data == {
"role": "server",
"type": "status",
"content": "complete",
}:
print("Received expected message from server")
break
# Get messages
get_url = "http://localhost:8000/settings/messages"
response_json = requests.get(get_url).json()
print("GET request sent, response:", response_json)
if isinstance(response_json, str):
response_json = json.loads(response_json)
messages = response_json["messages"]
response = interpreter.computer.ai.chat(
str(messages)
+ "\n\nIn the conversation above, does the assistant appear to be able to describe the image of a gradient? Yes or no? Only reply with one word— 'yes' or 'no'."
)
assert response.strip(" \n.").lower() == "yes"
# Sending POST request to /run endpoint with code to kill a thread in Python
# actually wait i dont think this will work..? will just kill the python interpreter
post_url = "http://localhost:8000/run"
code_data = {
"code": "import os, signal; os.kill(os.getpid(), signal.SIGINT)",
"language": "python",
}
response = requests.post(post_url, json=code_data)
print("POST request sent, response:", response.json())
# Get the current event loop and run the test function
loop = asyncio.get_event_loop()
loop.run_until_complete(test_fastapi_server())
# Kill server process
process.terminate()
os.kill(process.pid, signal.SIGKILL) # Send SIGKILL signal
process.join()
@pytest.mark.skip(reason="Mac only")
def test_sms():
sms = interpreter.computer.sms
# Get the last 5 messages
messages = sms.get(limit=5)
print(messages)
# Search messages for a substring
search_results = sms.get(substring="i love you", limit=100)
print(search_results)
assert False
@pytest.mark.skip(reason="Mac only")
def test_pytes():
import os
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
files_on_desktop = [f for f in os.listdir(desktop_path) if f.endswith(".png")]
if files_on_desktop:
first_file = files_on_desktop[0]
first_file_path = os.path.join(desktop_path, first_file)
print(first_file_path)
ocr = interpreter.computer.vision.ocr(path=first_file_path)
print(ocr)
print("what")
else:
print("No files found on Desktop.")
assert False
def test_ai_chat():
print(interpreter.computer.ai.chat("hi"))
def test_generator():
"""
Sends two messages, makes sure everything is correct with display both on and off.
"""
interpreter.llm.model = "gpt-4o-mini"
for tests in [
{"query": "What's 38023*40334? Use Python", "display": True},
{"query": "What's 2334*34335555? Use Python", "display": True},
{"query": "What's 3545*22? Use Python", "display": False},
{"query": "What's 0.0021*3433335555? Use Python", "display": False},
]:
assistant_message_found = False
console_output_found = False
active_line_found = False
flag_checker = []
for chunk in interpreter.chat(
tests["query"]
+ "\nNo talk or plan, just immediately code, then tell me the answer.",
stream=True,
display=True,
):
print(chunk)
# Check if chunk has the right schema
assert "role" in chunk, "Chunk missing 'role'"
assert "type" in chunk, "Chunk missing 'type'"
if "start" not in chunk and "end" not in chunk:
assert "content" in chunk, "Chunk missing 'content'"
if "format" in chunk:
assert isinstance(chunk["format"], str), "'format' should be a string"
flag_checker.append(chunk)
# Check if assistant message, console output, and active line are found
if chunk["role"] == "assistant" and chunk["type"] == "message":
assistant_message_found = True
if chunk["role"] == "computer" and chunk["type"] == "console":
console_output_found = True
if "format" in chunk:
if (
chunk["role"] == "computer"
and chunk["type"] == "console"
and chunk["format"] == "active_line"
):
active_line_found = True
# Ensure all flags are proper
assert (
flag_checker.count(
{"role": "assistant", "type": "code", "format": "python", "start": True}
)
== 1
), "Incorrect number of 'assistant code start' flags"
assert (
flag_checker.count(
{"role": "assistant", "type": "code", "format": "python", "end": True}
)
== 1
), "Incorrect number of 'assistant code end' flags"
assert (
flag_checker.count({"role": "assistant", "type": "message", "start": True})
== 1
), "Incorrect number of 'assistant message start' flags"
assert (
flag_checker.count({"role": "assistant", "type": "message", "end": True})
== 1
), "Incorrect number of 'assistant message end' flags"
assert (
flag_checker.count({"role": "computer", "type": "console", "start": True})
== 1
), "Incorrect number of 'computer console output start' flags"
assert (
flag_checker.count({"role": "computer", "type": "console", "end": True})
== 1
), "Incorrect number of 'computer console output end' flags"
# Assert that assistant message, console output, and active line were found
assert assistant_message_found, "No assistant message was found"
assert console_output_found, "No console output was found"
assert active_line_found, "No active line was found"
@pytest.mark.skip(reason="Requires open-interpreter[local]")
def test_localos():
interpreter.computer.emit_images = False
interpreter.computer.view()
interpreter.computer.emit_images = True
assert False
@pytest.mark.skip(reason="Requires open-interpreter[local]")
def test_m_vision():
base64png = "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMO0TAAD//2Anhf4QtqobAAAAAElFTkSuQmCC"
messages = [
{"role": "user", "type": "message", "content": "describe this image"},
{
"role": "user",
"type": "image",
"format": "base64.png",
"content": base64png,
},
]
interpreter.llm.supports_vision = False
interpreter.llm.model = "gpt-4o-mini"
interpreter.llm.supports_functions = True
interpreter.llm.context_window = 110000
interpreter.llm.max_tokens = 4096
interpreter.loop = True
interpreter.chat(messages)
interpreter.loop = False
import time
time.sleep(10)
@pytest.mark.skip(reason="Computer with display only + no way to fail test")
def test_point():
# interpreter.computer.debug = True
interpreter.computer.mouse.move(icon="gear")
interpreter.computer.mouse.move(icon="refresh")
interpreter.computer.mouse.move(icon="play")
interpreter.computer.mouse.move(icon="magnifying glass")
interpreter.computer.mouse.move("Spaces:")
assert False
@pytest.mark.skip(reason="Aifs not ready")
def test_skills():
import sys
if sys.version_info[:2] == (3, 12):
print(
"skills.search is only for python 3.11 for now, because it depends on unstructured. skipping this test."
)
return
import json
interpreter.llm.model = "gpt-4o-mini"
messages = ["USER: Hey can you search the web for me?\nAI: Sure!"]
combined_messages = "\\n".join(json.dumps(x) for x in messages[-3:])
query_msg = interpreter.chat(
f"This is the conversation so far: {combined_messages}. What is a hypothetical python function that might help resolve the user's query? Respond with nothing but the hypothetical function name exactly."
)
query = query_msg[0]["content"]
# skills_path = '/01OS/server/skills'
# interpreter.computer.skills.path = skills_path
print(interpreter.computer.skills.path)
if os.path.exists(interpreter.computer.skills.path):
for file in os.listdir(interpreter.computer.skills.path):
os.remove(os.path.join(interpreter.computer.skills.path, file))
print("Path: ", interpreter.computer.skills.path)
print("Files in the path: ")
interpreter.computer.run("python", "def testing_skilsl():\n print('hi')")
for file in os.listdir(interpreter.computer.skills.path):
print(file)
interpreter.computer.run("python", "def testing_skill():\n print('hi')")
print("Files in the path: ")
for file in os.listdir(interpreter.computer.skills.path):
print(file)
try:
skills = interpreter.computer.skills.search(query)
except ImportError:
print("Attempting to install unstructured[all-docs]")
import subprocess
subprocess.run(["pip", "install", "unstructured[all-docs]"], check=True)
skills = interpreter.computer.skills.search(query)
lowercase_skills = [skill[0].lower() + skill[1:] for skill in skills]
output = "\\n".join(lowercase_skills)
assert "testing_skilsl" in str(output)
@pytest.mark.skip(reason="Local only")
def test_browser():
interpreter.computer.api_base = "http://0.0.0.0:80/v0"
print(
interpreter.computer.browser.search("When's the next Dune showing in Seattle?")
)
assert False
@pytest.mark.skip(reason="Computer with display only + no way to fail test")
def test_display_api():
start = time.time()
# interpreter.computer.display.find_text("submit")
# assert False
def say(icon_name):
import subprocess
subprocess.run(["say", "-v", "Fred", icon_name])
icons = [
"Submit",
"Yes",
"Profile picture icon",
"Left arrow",
"Magnifying glass",
"star",
"record icon icon",
"age text",
"call icon icon",
"account text",
"home icon",
"settings text",
"form text",
"gear icon icon",
"trash icon",
"new folder icon",
"phone icon icon",
"home button",
"trash button icon",
"folder icon icon",
"black heart icon icon",
"white heart icon icon",
"image icon",
"test@mail.com text",
]
# from random import shuffle
# shuffle(icons)
say("The test will begin in 3")
time.sleep(1)
say("2")
time.sleep(1)
say("1")
time.sleep(1)
import pyautogui
pyautogui.mouseDown()
for icon in icons:
if icon.endswith("icon icon"):
say("click the " + icon)
interpreter.computer.mouse.move(icon=icon.replace("icon icon", "icon"))
elif icon.endswith("icon"):
say("click the " + icon)
interpreter.computer.mouse.move(icon=icon.replace(" icon", ""))
elif icon.endswith("text"):
say("click " + icon)
interpreter.computer.mouse.move(icon.replace(" text", ""))
else:
say("click " + icon)
interpreter.computer.mouse.move(icon=icon)
# interpreter.computer.mouse.move(icon="caution")
# interpreter.computer.mouse.move(icon="bluetooth")
# interpreter.computer.mouse.move(icon="gear")
# interpreter.computer.mouse.move(icon="play button")
# interpreter.computer.mouse.move(icon="code icon with '>_' in it")
print(time.time() - start)
assert False
@pytest.mark.skip(reason="Server is not a stable feature")
def test_websocket_server():
# Start the server in a new thread
server_thread = threading.Thread(target=interpreter.server)
server_thread.start()
# Give the server a moment to start
time.sleep(3)
# Connect to the server
ws = create_connection("ws://localhost:8000/")
# Send the first message
ws.send(
"Hello, interpreter! What operating system are you on? Also, what time is it in Seattle?"
)
# Wait for a moment before sending the second message
time.sleep(1)
ws.send("Actually, nevermind. Thank you!")
# Receive the responses
responses = []
while True:
response = ws.recv()
print(response)
responses.append(response)
# Check the responses
assert responses # Check that some responses were received
ws.close()
@pytest.mark.skip(reason="Server is not a stable feature")
def test_i():
import requests
url = "http://localhost:8000/"
data = "Hello, interpreter! What operating system are you on? Also, what time is it in Seattle?"
headers = {"Content-Type": "text/plain"}