This commit is contained in:
Paul Gauthier 2023-04-08 13:49:34 -07:00
parent 2ede239dd5
commit 266c2ef1cc

View file

@ -47,7 +47,6 @@ new lines to replace
the original chunk the original chunk
>>>>>>> UPDATED >>>>>>> UPDATED
NEVER REPLY WITH AN ENTIRE FILE!
ONLY USE THIS ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES! ONLY USE THIS ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES!
''' '''
@ -164,9 +163,10 @@ class Coder:
def get_files_content(self): def get_files_content(self):
prompt = 'Here is the content of the files. NEVER OUTPUT ENTIRE FILES! NEVER OUTPUT IN THIS FORMAT!\n' prompt = ''
for fname in self.fnames: for fname in self.fnames:
prompt += self.quoted_file(fname) prompt += self.quoted_file(fname)
prompt += '\n\nRemember, NEVER REPLY WITH WHOLE FILES LIKE THIS. ONLY TELL ME CODE CHANGES USING ORIGINAL/UPDATED EDIT COMMANDS!\n'
return prompt return prompt
change_notice = ''' change_notice = '''
@ -192,10 +192,19 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
dict(role = 'system', content = self.system_prompt), dict(role = 'system', content = self.system_prompt),
] ]
did_edits = False
while True: while True:
inp = self.get_input() inp = self.get_input()
if did_edits:
files_prefix = 'I made your suggested changes, here are the updated files:'
else:
files_prefix = 'Here are the files:'
files_prefix += '\n\n'
messages += [ messages += [
dict(role = 'user', content = self.get_files_content()), dict(role = 'user', content = files_prefix + self.get_files_content()),
dict(role = 'assistant', content = "Ok."), dict(role = 'assistant', content = "Ok."),
dict(role = 'user', content = inp), dict(role = 'user', content = inp),
] ]
@ -207,17 +216,19 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
messages.append(user_msg) messages.append(user_msg)
messages.append(dict(role = 'assistant', content = content)) messages.append(dict(role = 'assistant', content = content))
print()
print() print()
try: try:
if self.update_files(content): did_edits = self.update_files(content)
if did_edits:
print() print()
except Exception as err: except Exception as err:
print(err) print(err)
print() print()
def send(self, messages, show_progress = 0): def send(self, messages, show_progress = 0):
for msg in messages: #for msg in messages:
dump(msg) # dump(msg)
completion = openai.ChatCompletion.create( completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",
@ -243,6 +254,10 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
sys.stdout.write(text) sys.stdout.write(text)
sys.stdout.flush() sys.stdout.flush()
if show_progress:
pbar.update(show_progress)
pbar.close()
resp = ''.join(resp) resp = ''.join(resp)
return resp return resp
@ -250,13 +265,18 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
pattern = re.compile(r'^(\S+)\n<<<<<<< ORIGINAL\n(.+?)\n=======\n(.+?)\n>>>>>>> UPDATED$', re.MULTILINE | re.DOTALL) pattern = re.compile(r'^(\S+)\n<<<<<<< ORIGINAL\n(.+?)\n=======\n(.+?)\n>>>>>>> UPDATED$', re.MULTILINE | re.DOTALL)
def update_files(self, content): def update_files(self, content):
did_edits = False
for match in self.pattern.finditer(content): for match in self.pattern.finditer(content):
did_edits = True
path, original, updated = match.groups() path, original, updated = match.groups()
if self.do_replace(path, original, updated): if self.do_replace(path, original, updated):
continue continue
edit = match.group() edit = match.group()
self.do_gpt_powered_replace(path, edit) self.do_gpt_powered_replace(path, edit)
return did_edits
def do_replace(self, fname, before_text, after_text): def do_replace(self, fname, before_text, after_text):
fname = Path(fname) fname = Path(fname)
content = fname.read_text().splitlines() content = fname.read_text().splitlines()
@ -273,11 +293,11 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
new_content = '\n'.join(new_content) + '\n' new_content = '\n'.join(new_content) + '\n'
fname.write_text(new_content) fname.write_text(new_content)
print('Applied CHANGE', fname) print('Applied edit to', fname)
return True return True
def do_gpt_powered_replace(self, fname, edit): def do_gpt_powered_replace(self, fname, edit):
print(f'Could not find ORIGINAL block in {fname}, asking GPT to make the edit...') print(f'Asking GPT to apply ambiguous edit to {fname}...')
fname = Path(fname) fname = Path(fname)
content = fname.read_text() content = fname.read_text()
prompt = f''' prompt = f'''
@ -305,7 +325,7 @@ Just the content of the file!
dict(role = 'system', content = sys_prompt), dict(role = 'system', content = sys_prompt),
dict(role = 'user', content = prompt), dict(role = 'user', content = prompt),
] ]
res = self.send(messages, show_progress = len(content) + len(edit)) res = self.send(messages, show_progress = len(content) + len(edit)/2)
res = res.splitlines() res = res.splitlines()
if res[0].strip == str(fname): if res[0].strip == str(fname):