-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
150 lines (111 loc) · 4.29 KB
/
main.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
# Packages
import openai, json, subprocess, os, re
# Environment Variables
from dotenv import load_dotenv
load_dotenv()
# Run the vma_config.json compiler
subprocess.run(["python", "vma_config.py"])
# These are the different steps of the VMA Pipeline
# 1. Initialization
# Load agent config
with open("vma_config.json", "r") as config:
vma_config = json.load(config)
messages = [{"role": "system", "content": vma_config["Initialization"]["prompt"]}]
json_analysis = ""
root_dir = "to-migrate/"
# Recursive function to read everything of a directory
def scan_dir(dir):
files = []
for path in os.scandir(dir):
if path.name == ".gitkeep":
continue
if path.is_file():
files.append(f"{path.path}")
elif path.is_dir():
f = scan_dir(f"{dir}{path.name}/")
for file in f:
files.append(file)
return files
# Scan the entire 'to-migrate' directory
# Read each file individually
files = {}
for file in scan_dir(root_dir):
f = open(f"{file}", "r")
files[file] = f.read()
# 1. Initialization
def initialization():
print("1. Initialization\n")
get_response(vma_config["Initialization"]["model"])
# 2. Parsing and Analysis
def parsing_and_analysis():
print("\n2. Parsing and Analysis")
message = f"{vma_config['Parsing and Analysis']['prompt']}{files}"
global json_analysis
add_message("user", message)
json_analysis = get_response(vma_config["Parsing and Analysis"]["model"])
# 3. Transforming and Refactoring
def transforming_and_refactoring():
print("\n3. Transforming and Refactoring\n")
message = f"Provided is the JSON Analysis: {json_analysis}. {vma_config['Transformating and Refactoring']['prompt']}"
add_message("user", message)
response = get_response(vma_config["Transformating and Refactoring"]["model"])
print("DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG")
print(response)
# Write migrated code to 'migrated' folder
# This is the whole message, not just the code
# Sadly, GPT models aren't really able to just output their target without small talk
# I'm scared some 'bugs' (more cases where you will need to re-run) will arrise here because
# It's not really possible to ensure that the model will always format it's response correctly
# This shouldn't be too much of an issue though, if it is, allow UMA to process this step with a more qualified model.
global code
code = re.search(r'```(.*?)```', response, re.DOTALL).group(1)
print(code)
# 4. Processing and Parsing
def processing_and_parsing():
print("4. Processing\n")
add_message("user", vma_config["Processing and Parsing"]["prompt"])
response = get_response(vma_config["Processing and Parsing"]["model"])
global filename
filename = re.search(r'```(.*?)```', response, re.DOTALL).group(1)
print(filename)
# Write content to the file
with open(f"migrated/{filename}", "w") as f:
f.write(code)
# 5. Collaboration
def collaboration():
print("5. Collaboration\n")
add_message("user", vma_config["Collaboration"]["prompt"])
get_response(vma_config["Collaboration"]["model"])
userInput = input("\nEnter your message (/exit to quit): ")
while userInput != '/exit':
add_message("user", userInput)
get_response(vma_config["Collaboration"]["model"])
userInput = input("\nEnter your message (/exit to quit): ")
def get_response(model):
response = openai.ChatCompletion.create(
model=model,
stream=True,
temperature=0,
messages=messages,
api_key=os.getenv("OPENAI_APIKEY"),
)
collected_messages = []
for chunk in response:
try:
collected_messages.append(chunk['choices'][0]['delta']['content'])
print(chunk['choices'][0]['delta']['content'], end='')
except:
continue
return ''.join([str(elem) for elem in collected_messages])
def add_message(role, code):
messages.append({"role": role, "content": code})
if __name__ == "__main__":
if files == {}:
print("'to-migrate/' directory is empty! Nothing to migrate.")
print("Quiting...")
else:
initialization()
parsing_and_analysis()
transforming_and_refactoring()
processing_and_parsing()
collaboration()