-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocoder.py
190 lines (151 loc) · 6.2 KB
/
autocoder.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
from tenacity import retry, wait_exponential, stop_after_attempt
import os
from openai import OpenAI
from termcolor import colored
from utils import read_yaml_file, write_html_css_js_to_file
import json
from typing import Dict
client = OpenAI(api_key= os.environ.get("OPENAI_API_KEY"))
MODEL_RETRY_SETTINGS = {
'wait': wait_exponential(multiplier=1, min=4, max=10),
'stop': stop_after_attempt(4),
'reraise': True,
}
# prompt user
print(colored("Welcome to the Full Stack Web Developer Assistant", "green"))
print(colored("WARNING: Deleting HTML, CSS, & JS files will reset the assistant to start from scratch.", "red"))
instruction = input("Press any key to continue or type 'exit' to exit")
if instruction.strip().lower() == "exit":
exit()
# load params
yaml_contents = read_yaml_file('params.yml')
# functions
functions=[
{
"name": "write_html_css_js_to_file",
"description": "This function takes in html, css, js and writes them to their respective files",
"parameters": {
"type": "object",
"properties": {
"plan": {
"type": "string",
"description": "Your detailed plan in markdown to solve errors or improve code to be written to plan file",
},
"html": {
"type": "string",
"description": "The html code to be written to html file",
},
"css": {
"type": "string",
"description": "The css code to be written to css file",
},
"js": {
"type": "string",
"description": "The js code to be written to js file",
},
},
"required": ["plan", "html", "css", "js"]
}
}
]
# returns code
@retry(**MODEL_RETRY_SETTINGS)
def return_code_and_user_input(model: str) -> Dict[str, str]:
"""
Prompt the user for input, capture it, and interact with an AI model to generate code files.
Args:
model (str): The name or identifier of the AI model to use.
Returns:
Dict[str, str]: A dictionary containing the arguments for the 'write_html_css_js_to_file' function,
including 'plan', 'html', 'css', and 'js' code strings.
Example:
result = return_code("my_model")
if result:
print("Function arguments:", result)
"""
entry = ''
print(colored("\n NOTE: This input accepts multiple lines. Feel free to provide as much detail as you'd like.", "green"))
print(colored("What type of website can I code for you? (Type 'done' on a new line and press 'enter' when finished.)", "yellow"))
while True:
response = input()
if response.strip().lower() == 'done':
break
entry += response + '\n'
print(colored("Starting to code...", "green"))
response = client.chat.completions.create(
model=model,
messages=[
{"role": "assistant", "content": f"{yaml_contents['SYSTEM_PROMPT_ONE']['MESSAGE']}"},
{"role": "user", "content": f"{entry}"},
],
function_call={"name": "write_html_css_js_to_file"},
functions= functions
)
if response.choices[0].message.function_call:
function_args = json.loads(response.choices[0].message.function_call.arguments)
return function_args, entry
else:
print(colored(response.choices[0].message.content))
return {}
# create files
if not os.path.exists('index.html'):
function_args, previous_user_input = return_code_and_user_input(yaml_contents['MODEL'])
write_html_css_js_to_file(**function_args)
# code improvement vars
x = 1
number_of_autonomous_runs_without_user_feedback = 1
retry_due_to_error = False
while True:
# retrieve code to be improved
previous_plan = open('plan.md').read()
previous_html = open('index.html').read()
previous_css = open('style.css').read()
previous_js = open('script.js').read()
if retry_due_to_error:
entry = previous_user_input
elif x % number_of_autonomous_runs_without_user_feedback == 0:
entry = ''
print(colored("\n NOTE: This input accepts multiple lines. Feel free to provide as much detail as you'd like.", "green"))
print(colored("What type of improvements do you want for this website? (Type 'done' on a new line and press 'enter' when finished.)", "yellow"))
while True:
response = input()
if response.strip().lower() == 'done':
break
entry += response + '\n'
else:
entry = yaml_contents['IMPROVEMENT_PROMPT']['MESSAGE']
print(colored("Starting to code...", "green"))
print(colored("Improvement attempt: " + str(x), "magenta"))
response = client.chat.completions.create(
model= yaml_contents['MODEL'],
messages= [
{"role": "assistant", "content": f"{yaml_contents['SYSTEM_PROMPT_TWO']['MESSAGE']}"},
{"role": "user", "content": f"""
Previous Plan: (Retain the beneficial parts and remove the undesirable ones)
{previous_plan}
Current HTML file
{previous_html}
Current CSS file
{previous_css}
Current JS file
{previous_js}
User Input
{entry}
"""},
],
functions= functions,
function_call={"name": "write_html_css_js_to_file"},
)
if response.choices[0].message.content:
print(colored(response.choices[0].message.content))
if response.choices[0].message.function_call:
try:
function_args = json.loads(response.choices[0].message.function_call.arguments)
except Exception as e:
retry_due_to_error = True
previous_user_input = entry
print(colored(e, "red"))
else:
print(colored(response.choices[0].message.content, "green"))
write_html_css_js_to_file(**function_args)
x += 1