-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.py
78 lines (52 loc) · 2.17 KB
/
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
import os
import subprocess
import json
from chat_gpt import ChatGPT
class PythonResult:
def __init__(self, output: str, result_code: int):
self.output = output
self.result_code = result_code
def run_python_code(code: str) -> PythonResult:
temp_file = "/tmp/code.py"
with open(temp_file, "w") as f:
f.write(code)
result = subprocess.run(["python3", temp_file], capture_output=True, text=True)
os.remove(temp_file)
return PythonResult(result.stdout, result.returncode)
def input_message(message: str = "") -> str:
return input(message).strip()
def python(code: str) -> str:
styled_code = "\n".join(f"# {row.ljust(45)} #" for row in code.split("\n"))
print("\n" + "#" * 49)
print("# I WANT TO RUN THIS PYTHON CODE: #")
print(styled_code)
print("#" * 49)
answer = ""
while answer not in ["yes", "no"]:
answer = input_message("\nDo you want to run this code? (yes/no)")
if answer != "yes":
print("\nSKIPPED RUNNING CODE")
return "User rejected code. Please ask for changes or what to do next."
result = run_python_code(code)
return json.dumps({"output": result.output, "result_code": result.result_code})
if not os.path.exists("./data"):
os.makedirs("./data")
# Assuming a Python version of ChatGPT is available in the current directory
chatgpt = ChatGPT(os.environ.get("OPENAI_API_KEY"))
chatgpt.smessage("You are an AI assistant...")
chatgpt.add_function(python)
print("################################################")
print("# Python CodeInterpreter #")
print("################################################\n")
print("GPT: Hello! I am the Python CodeInterpreter! What would you like to do?\nYou: ", end="")
while True:
message = input_message()
if message == "":
print("You haven't provided anything. Please try again.\nYou: ")
continue
if message in ["exit", "quit", "stop"]:
print("\nGPT: Thanks!\n")
exit()
# Assuming the Python version of ChatGPT has a method 'umessage' and 'response'
chatgpt.umessage(message)
print("\n\nGPT:", chatgpt.response()['content'], "\nYou: ", end="")