more tolerant edit parsing

This commit is contained in:
Paul Gauthier 2023-04-08 18:57:11 -07:00
parent f0d56e3cce
commit 69c55becde

View file

@ -35,22 +35,23 @@ I want you to act as an expert software engineer and pair programmer.
You are an expert at understanding code and proposing code changes in response to user requests. You are an expert at understanding code and proposing code changes in response to user requests.
Your job is to: Your job is to:
- Understand what the user wants. Ask questions if needed. - Understand what the user wants. Ask questions if needed!
- Suggest changes to the code. - Suggest changes to the code by performing search and replace using the syntax below.
FOR EACH CHANGE TO THE CODE, DESCRIBE IT USING THIS FORMAT: FOR EACH CHANGE TO THE CODE, DESCRIBE IT USING THIS FORMAT:
path/to/filename.ext path/to/filename.ext
<<<<<<< ORIGINAL <<<<<<< ORIGINAL
a chunk of the **exact** lines a chunk of the **exact** lines
from the original file from the current file that needs to be changed
that needs to be changed MUST BE THE EXACT LINES FROM THE CURRENT FILE
======= =======
new lines to replace new lines to replace
the original chunk the original chunk
>>>>>>> UPDATED >>>>>>> UPDATED
ONLY USE THIS ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES! ONLY USE THIS ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES!
DO NOT USE ``` DELIMITERS!
''' '''
prompt_comments = ''' prompt_comments = '''
@ -236,8 +237,8 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
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",
@ -338,6 +339,9 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
return did_edits return did_edits
def do_replace(self, fname, before_text, after_text): def do_replace(self, fname, before_text, after_text):
before_text = self.strip_quoted_wrapping(before_text, fname)
dump(repr(before_text))
fname = Path(fname) fname = Path(fname)
content = fname.read_text().splitlines() content = fname.read_text().splitlines()
before_lines = [l.strip() for l in before_text.splitlines()] before_lines = [l.strip() for l in before_text.splitlines()]
@ -358,6 +362,7 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
def do_gpt_powered_replace(self, fname, edit): def do_gpt_powered_replace(self, fname, edit):
print(f'Asking GPT to apply ambiguous edit to {fname}...') print(f'Asking GPT to apply ambiguous edit to {fname}...')
print(repr(edit))
fname = Path(fname) fname = Path(fname)
content = fname.read_text() content = fname.read_text()
prompt = f''' prompt = f'''
@ -386,17 +391,25 @@ Just the content of the file!
dict(role = 'user', content = prompt), dict(role = 'user', content = prompt),
] ]
res = self.send(messages, show_progress = len(content) + len(edit)/2) res = self.send(messages, show_progress = len(content) + len(edit)/2)
dump(repr(res))
res = self.strip_quoted_wrapping(res, fname)
fname.write_text(res)
def strip_quoted_wrapping(self, res, fname=None):
res = res.splitlines() res = res.splitlines()
if res[0].strip == str(fname):
if fname and res[0].strip().endswith(Path(fname).name):
res = res[1:] res = res[1:]
if res[0].strip() == '```' and res[-1].strip() == '```':
if res[0].startswith('```') and res[-1].startswith('```'):
res = res[1:-1] res = res[1:-1]
res = '\n'.join(res) res = '\n'.join(res)
if res[-1] != '\n': if res[-1] != '\n':
res += '\n' res += '\n'
fname.write_text(res)
return res
def test_do_gpt_powered_replace(coder): def test_do_gpt_powered_replace(coder):