From 04382f0b2b8f400b8741db6a4789f83008d89ae0 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 8 Apr 2023 11:59:31 -0700 Subject: [PATCH] do_gpt_powered_replace --- coder.py | 78 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/coder.py b/coder.py index f5ec6f8ac..5d4fd7a54 100755 --- a/coder.py +++ b/coder.py @@ -281,30 +281,83 @@ The ``` delimiters are very important! def update_files(self, content): for match in self.pattern.finditer(content): path, original, updated = match.groups() - self.do_replace(path, original, updated) + if self.do_replace(path, original, updated): + continue + edit = match.group() + self.do_gpt_powered_replace(path, edit) - def do_replace(self, fname, before, after): + def do_replace(self, fname, before_text, after_text): fname = Path(fname) content = fname.read_text().splitlines() - before = [l.strip() for l in before.splitlines()] + before_lines = [l.strip() for l in before_text.splitlines()] stripped_content = [l.strip() for l in content] - where = find_index(stripped_content, before) + where = find_index(stripped_content, before_lines) if where < 0: - raise ValueError(before) + return new_content = content[:where] - new_content += after.splitlines() - new_content += content[where+len(before):] + new_content += after_text.splitlines() + new_content += content[where+len(before_lines):] new_content = '\n'.join(new_content) + '\n' fname.write_text(new_content) print('Applied CHANGE', fname) + return True + + def do_gpt_powered_replace(self, fname, edit): + print(f'Could not find CURRENT block in {fname}, asking GPT to make the edit...') + fname = Path(fname) + content = fname.read_text() + prompt = f''' +Apply this change: + +{edit} + +To this file: + +{fname} +``` +{content} +``` +''' + sys_prompt = ''' +You are an expert code editor. +Perform the requested edit. +Output ONLY the new version of the file. +Do not output explanations! +Do not wrap the output in ``` delimiters. +Just the content of the file! +''' + + messages = [ + dict(role = 'system', content = sys_prompt), + dict(role = 'user', content = prompt), + ] + res = self.send(messages) + + fname.write_text(res) + + +def test_do_gpt_powered_replace(coder): + fname = Path('../easy-chat/index.html') + edit = ''' +../easy-chat/index.html +<<<<<<< ORIGINAL +

Hello!

+

How can I help + you?

+======= +

Hello!

+

How can I helpyou?

+>>>>>>> UPDATED +''' + coder.do_gpt_powered_replace(fname, edit) coder = Coder() +#test_do_gpt_powered_replace(coder) ; sys.exit() coder.system(prompt_webdev) - for fname in sys.argv[1:]: coder.add_file(fname) @@ -312,15 +365,6 @@ for fname in sys.argv[1:]: coder.run() -#dname = Path('../easy-chat') -#coder.file(dname / 'index.html') -#coder.file(dname / 'chat.css') -#coder.file(dname / 'chat.js') - -#for fname in coder.fnames: -# print(coder.quoted_file(fname)) -#sys.exit() - ''' Change all the speaker icons to orange.