-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev.py
147 lines (113 loc) · 3.58 KB
/
dev.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
import json
import subprocess
from chat import chat_with_gpt4
def read_file(file):
with open(file, "r") as f:
return f.read()
def read_files(files):
contents = ""
ret = ""
for file in files:
try:
contents = read_file(file)
except FileNotFoundError:
contents = "File not found"
except Exception as e:
contents = f"Error reading file: {str(e)}"
ret += f"{file}\n---\n{contents}\n\n"
return ret
def build_template(
template_file,
execute_result_json,
log_json,
message_from_user,
files_before_execute_json,
files_after_execute_json,
):
return read_file(template_file).format(
log=log_json,
execute_result=execute_result_json,
message_from_user=message_from_user,
files_before_execute=files_before_execute_json,
files_after_execution=files_after_execute_json,
)
def parse_response(response):
return json.loads(response)
def execute_commands(execute):
execute_result = []
for command in execute:
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
stdout, stderr = process.communicate()
exitcode = process.returncode
execute_result.append(
{
"command": command,
"stdout": stdout.decode("utf-8"),
"stderr": stderr.decode("utf-8"),
"exitCode": exitcode,
}
)
return execute_result
def add_to_log_file(log_file, summary):
with open(log_file, "a") as file:
file.write("\n" + summary)
def load_from_file(filename):
with open(filename, "r") as file:
return json.load(file)
def write_to_file(filename, content):
with open(filename, "w") as file:
json.dump(content, file)
write_to_file("execute_result.json", {})
write_to_file("log.json", [])
message_to_user = ["start"]
files_before_execution = {}
files_after_execution = {}
while True:
if message_to_user:
user_input = input("\n".join(message_to_user) + "\n>")
else:
user_input = ""
execute_result = load_from_file("execute_result.json")
log = read_file("log.json")
text = build_template(
"prompt",
execute_result,
log,
user_input,
json.dumps(files_before_execution, indent=4),
files_after_execution,
)
print("\n-------\n")
print(text)
gpt4_response = chat_with_gpt4(text)
print(gpt4_response)
response = parse_response(gpt4_response)
if response.get("files"):
files_before_execution = read_files(response.get("files"))
if response.get("execute"):
execute_result = {
"intention": response.get("intention"),
"exec": execute_commands(response["execute"]),
}
print(execute_result)
write_to_file("execute_result.json", execute_result)
else:
write_to_file("execute_result.json", {})
if response.get("files"):
files_after_execution = read_files(response.get("files"))
if response.get("summary"):
add_to_log_file(
"log.json",
"---\n"
+ "\n".join(
[
"{}: {}".format(it, "; ".join(response.get(it)))
for it in ["thinking", "summary", "intention", "message_to_user"]
if response.get(it)
]
)
+ (("\nmessage from user: " + user_input) if user_input else ""),
)
message_to_user = response.get("message_to_user")