File size: 7,901 Bytes
66214f3 |
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 |
import json
import re
import subprocess
from enum import Enum
from typing import Callable, TypeVar
from gpt_engineer.ai import AI
from gpt_engineer.chat_to_files import to_files
from gpt_engineer.db import DBs
def setup_sys_prompt(dbs):
return (
dbs.preprompts["generate"] + "\nUseful to know:\n" + dbs.preprompts["philosophy"]
)
Step = TypeVar("Step", bound=Callable[[AI, DBs], list[dict]])
def simple_gen(ai: AI, dbs: DBs):
"""Run the AI on the main prompt and save the results"""
messages = ai.start(
setup_sys_prompt(dbs),
dbs.input["main_prompt"],
)
to_files(messages[-1]["content"], dbs.workspace)
return messages
def clarify(ai: AI, dbs: DBs):
"""
Ask the user if they want to clarify anything and save the results to the workspace
"""
messages = [ai.fsystem(dbs.preprompts["qa"])]
user = dbs.input["main_prompt"]
while True:
messages = ai.next(messages, user)
if messages[-1]["content"].strip().lower().startswith("no"):
break
print()
user = input('(answer in text, or "c" to move on)\n')
print()
if not user or user == "c":
break
user += (
"\n\n"
"Is anything else unclear? If yes, only answer in the form:\n"
"{remaining unclear areas} remaining questions.\n"
"{Next question}\n"
'If everything is sufficiently clear, only answer "no".'
)
print()
return messages
def gen_spec(ai: AI, dbs: DBs):
"""
Generate a spec from the main prompt + clarifications and save the results to
the workspace
"""
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
ai.fsystem(f"Instructions: {dbs.input['main_prompt']}"),
]
messages = ai.next(messages, dbs.preprompts["spec"])
dbs.memory["specification"] = messages[-1]["content"]
return messages
def respec(ai: AI, dbs: DBs):
messages = json.loads(dbs.logs[gen_spec.__name__])
messages += [ai.fsystem(dbs.preprompts["respec"])]
messages = ai.next(messages)
messages = ai.next(
messages,
(
"Based on the conversation so far, please reiterate the specification for "
"the program. "
"If there are things that can be improved, please incorporate the "
"improvements. "
"If you are satisfied with the specification, just write out the "
"specification word by word again."
),
)
dbs.memory["specification"] = messages[-1]["content"]
return messages
def gen_unit_tests(ai: AI, dbs: DBs):
"""
Generate unit tests based on the specification, that should work.
"""
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
ai.fuser(f"Instructions: {dbs.input['main_prompt']}"),
ai.fuser(f"Specification:\n\n{dbs.memory['specification']}"),
]
messages = ai.next(messages, dbs.preprompts["unit_tests"])
dbs.memory["unit_tests"] = messages[-1]["content"]
to_files(dbs.memory["unit_tests"], dbs.workspace)
return messages
def gen_clarified_code(ai: AI, dbs: DBs):
# get the messages from previous step
messages = json.loads(dbs.logs[clarify.__name__])
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
] + messages[1:]
messages = ai.next(messages, dbs.preprompts["use_qa"])
to_files(messages[-1]["content"], dbs.workspace)
return messages
def gen_code(ai: AI, dbs: DBs):
# get the messages from previous step
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
ai.fuser(f"Instructions: {dbs.input['main_prompt']}"),
ai.fuser(f"Specification:\n\n{dbs.memory['specification']}"),
ai.fuser(f"Unit tests:\n\n{dbs.memory['unit_tests']}"),
]
messages = ai.next(messages, dbs.preprompts["use_qa"])
to_files(messages[-1]["content"], dbs.workspace)
return messages
def execute_entrypoint(ai, dbs):
command = dbs.workspace["run.sh"]
print("Do you want to execute this code?")
print()
print(command)
print()
print('If yes, press enter. Otherwise, type "no"')
print()
if input() not in ["", "y", "yes"]:
print("Ok, not executing the code.")
return []
print("Executing the code...")
print(
"\033[92m" # green color
+ "Note: If it does not work as expected, please consider running the code'"
+ " in another way than above."
+ "\033[0m"
)
print()
subprocess.run("bash run.sh", shell=True, cwd=dbs.workspace.path)
return []
def gen_entrypoint(ai, dbs):
messages = ai.start(
system=(
"You will get information about a codebase that is currently on disk in "
"the current folder.\n"
"From this you will answer with code blocks that includes all the necessary "
"unix terminal commands to "
"a) install dependencies "
"b) run all necessary parts of the codebase (in parallell if necessary).\n"
"Do not install globally. Do not use sudo.\n"
"Do not explain the code, just give the commands.\n"
"Do not use placeholders, use example values (like . for a folder argument) "
"if necessary.\n"
),
user="Information about the codebase:\n\n" + dbs.workspace["all_output.txt"],
)
print()
regex = r"```\S*\n(.+?)```"
matches = re.finditer(regex, messages[-1]["content"], re.DOTALL)
dbs.workspace["run.sh"] = "\n".join(match.group(1) for match in matches)
return messages
def use_feedback(ai: AI, dbs: DBs):
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
ai.fuser(f"Instructions: {dbs.input['main_prompt']}"),
ai.fassistant(dbs.workspace["all_output.txt"]),
ai.fsystem(dbs.preprompts["use_feedback"]),
]
messages = ai.next(messages, dbs.input["feedback"])
to_files(messages[-1]["content"], dbs.workspace)
return messages
def fix_code(ai: AI, dbs: DBs):
code_output = json.loads(dbs.logs[gen_code.__name__])[-1]["content"]
messages = [
ai.fsystem(setup_sys_prompt(dbs)),
ai.fuser(f"Instructions: {dbs.input['main_prompt']}"),
ai.fuser(code_output),
ai.fsystem(dbs.preprompts["fix_code"]),
]
messages = ai.next(messages, "Please fix any errors in the code above.")
to_files(messages[-1]["content"], dbs.workspace)
return messages
class Config(str, Enum):
DEFAULT = "default"
BENCHMARK = "benchmark"
SIMPLE = "simple"
TDD = "tdd"
TDD_PLUS = "tdd+"
CLARIFY = "clarify"
RESPEC = "respec"
EXECUTE_ONLY = "execute_only"
USE_FEEDBACK = "use_feedback"
# Different configs of what steps to run
STEPS = {
Config.DEFAULT: [
clarify,
gen_clarified_code,
gen_entrypoint,
execute_entrypoint,
],
Config.BENCHMARK: [simple_gen, gen_entrypoint],
Config.SIMPLE: [simple_gen, gen_entrypoint, execute_entrypoint],
Config.TDD: [
gen_spec,
gen_unit_tests,
gen_code,
gen_entrypoint,
execute_entrypoint,
],
Config.TDD_PLUS: [
gen_spec,
gen_unit_tests,
gen_code,
fix_code,
gen_entrypoint,
execute_entrypoint,
],
Config.CLARIFY: [
clarify,
gen_clarified_code,
gen_entrypoint,
execute_entrypoint,
],
Config.RESPEC: [
gen_spec,
respec,
gen_unit_tests,
gen_code,
fix_code,
gen_entrypoint,
execute_entrypoint,
],
Config.USE_FEEDBACK: [use_feedback, gen_entrypoint, execute_entrypoint],
Config.EXECUTE_ONLY: [gen_entrypoint, execute_entrypoint],
}
# Future steps that can be added:
# run_tests_and_fix_files
# execute_entrypoint_and_fix_files_if_needed
|