-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTIL2.py
184 lines (132 loc) · 5.65 KB
/
UTIL2.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
from typing import Callable, List, TypeVar,Optional
import inspect
import json
import os
import re
import subprocess
import random
import tempfile
from enum import Enum
from termcolor import colored
from UTIL import Agent,tofiles,DBs,database
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
from dataclasses_json import dataclass_json
Step = TypeVar("Step", bound=Callable[[Agent, DBs], List[dict]])
#########################################################################################################
def colored(*args):
return args[0]
def setup_sysprompt(dbs: DBs) -> str:
return (
dbs.preprompts["generate"] + "\nUseful to know:\n" + dbs.preprompts["philosophy"]
)
def getprompt(dbs: DBs) -> str:
assert (
"prompt" in dbs.input or "main_prompt" in dbs.input
), "Please put your prompt in the file `prompt` in the project directory"
if "prompt" not in dbs.input:
print(
colored("Please put the prompt in the file `prompt`, not `main_prompt", "red")
)
print()
return dbs.input["main_prompt"]
return dbs.input["prompt"]
def currentfn() -> str:
return inspect.stack()[1].function
def simplegen(ai: Agent, dbs: DBs) -> List[dict]:
messages = ai.start(setup_sysprompt(dbs), getprompt(dbs), step_name=currentfn())
tofiles(messages[-1]["content"], dbs.workspace)
return messages
def clarify(ai: Agent, dbs: DBs) -> List[dict]:
messages = [ai.parsemsg("system",dbs.preprompts["qa"])]
userinput = getprompt(dbs)
while True:
messages = ai.next(messages, userinput, step_name=currentfn())
if messages[-1]["content"].strip() == "Nothing more to clarify.":
breaks
userinput = input('(answer in text, or "c" to move on)\n')
if not userinput or userinput == "c":
print("(LLM makes its own assumptions)")
messages = ai.next(
messages,
"Make your own assumptions and state them explicitly before starting",
step_name=currentfn(),
)
print()
return messages
userinput += (
"\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 "Nothing more to clarify.".'
)
return messages
def genspec(ai: Agent, dbs: DBs) -> List[dict]:
messages = [
ai.parsemsg("system",setup_sysmsg(dbs)),
ai.parsemsg("system",f"Instructions: {dbs.input['prompt']}"),
]
messages = ai.next(messages, dbs.preprompts["spec"], step_name=currentfn())
dbs.memory["specification"] = messages[-1]["content"]
return messages
def respec(ai: Agent, dbs: DBs) -> List[dict]:
messages = json.loads(dbs.logs[genspec.__name__])
messages += [ai.parsemsg("system",dbs.preprompts["respec"])]
messages = ai.next(messages, step_name=currentfn())
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."
),
step_name=currentfn(),
)
dbs.memory["specification"] = messages[-1]["content"]
return messages
def gen_clearcode(ai: Agent, dbs: DBs) -> List[dict]:
messages = json.loads(dbs.logs[clarify.__name__])
messages = [
ai.parsemsg("system",setup_sysprompt(dbs)),
] + messages[1:]
messages = ai.next(messages, dbs.preprompts["use_qa"], step_name=currentfn())
tofiles(messages[-1]["content"], dbs.workspace)
return messages
def gen_code(ai: Agent, dbs: DBs) -> List[dict]:
messages = [
ai.parsemsg("system",setup_sysprompt(dbs)),
ai.parsemsg("user",f"Instructions: {dbs.input['prompt']}"),
ai.parsemsg("user",f"Specification:\n\n{dbs.memory['specification']}"),
]
messages = ai.next(messages, dbs.preprompts["use_qa"], step_name=currentfn())
tofiles(messages[-1]["content"], dbs.workspace)
return messages
def gen_entrypoint(ai: Agent, dbs: DBs) -> List[dict]:
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 parallel 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["output.txt"],
step_name=currentfn(),
)
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
CHECKS=[clarify,gen_clearcode,gen_entrypoint]
###################################################################################################################################
###################################################################################################################################