mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 06:15:00 +00:00
refactor prompts to prompts.py
This commit is contained in:
parent
0368eed9f7
commit
0186679449
2 changed files with 90 additions and 180 deletions
194
coder.py
194
coder.py
|
@ -22,6 +22,8 @@ import openai
|
|||
|
||||
from dump import dump
|
||||
|
||||
import prompts
|
||||
|
||||
history_file = '.coder.history'
|
||||
try:
|
||||
readline.read_history_file(history_file)
|
||||
|
@ -32,58 +34,6 @@ formatter = formatters.TerminalFormatter()
|
|||
|
||||
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
prompt_webdev = '''
|
||||
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.
|
||||
|
||||
Your job is to:
|
||||
- Understand what the user wants. Ask questions if needed!
|
||||
- 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:
|
||||
|
||||
path/to/filename.ext
|
||||
<<<<<<< ORIGINAL
|
||||
original lines
|
||||
to search for
|
||||
=======
|
||||
new lines to replace
|
||||
the original chunk
|
||||
>>>>>>> UPDATED
|
||||
|
||||
ONLY USE THIS ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES!
|
||||
|
||||
Example:
|
||||
|
||||
foo.py
|
||||
<<<<<<< ORIGINAL
|
||||
print(1+1)
|
||||
=======
|
||||
print(2+2)
|
||||
>>>>>>> UPDATED
|
||||
|
||||
|
||||
To add new code, anchor it by including 2-3 lines in the ORIGINAL and UPDATED portions of the diff.
|
||||
Don't just output the ENTIRE file. Turn it into an edit.
|
||||
'''
|
||||
|
||||
prompt_comments = '''
|
||||
I want you to act as a web development expert.
|
||||
I want you to answer only with comments in the code.
|
||||
Whatever the user requests, add comments in the code showing how to make the requested change and explaining why it will work.
|
||||
Just add comments to the code.
|
||||
Output the new version of the code with added comments.
|
||||
Embed lots of comments in the code explaining how and where to make changes.
|
||||
MAKE NO OTHER CHANGES!
|
||||
|
||||
For each file, output like this:
|
||||
|
||||
path/to/filename.ext
|
||||
```
|
||||
... file content ...
|
||||
```
|
||||
'''
|
||||
|
||||
|
||||
def find_index(list1, list2):
|
||||
for i in range(len(list1)):
|
||||
|
@ -94,9 +44,6 @@ def find_index(list1, list2):
|
|||
class Coder:
|
||||
fnames = dict()
|
||||
|
||||
def system(self, prompt):
|
||||
self.system_prompt = prompt
|
||||
|
||||
def add_file(self, fname):
|
||||
self.fnames[fname] = Path(fname).stat().st_mtime
|
||||
|
||||
|
@ -116,79 +63,11 @@ class Coder:
|
|||
prompt += '\n```\n'
|
||||
return prompt
|
||||
|
||||
def run_davinci(self):
|
||||
prompt = ''
|
||||
prompt += 'Original code:\n\n'
|
||||
|
||||
for fname in self.fnames:
|
||||
prompt += self.quoted_file(fname)
|
||||
|
||||
prompt += '\n###\n'
|
||||
prompt += self.request_prompt
|
||||
|
||||
prompt += '\n###\n'
|
||||
|
||||
prompt += 'Modified code including those changes:\n\n'
|
||||
|
||||
completion = openai.Completion.create(
|
||||
model="text-davinci-003",
|
||||
prompt= prompt,
|
||||
max_tokens=2048,
|
||||
temperature=0,
|
||||
stream = True,
|
||||
)
|
||||
resp = ''
|
||||
for chunk in completion:
|
||||
try:
|
||||
text = chunk.choices[0].text
|
||||
resp += text
|
||||
except AttributeError:
|
||||
continue
|
||||
|
||||
sys.stdout.write(text)
|
||||
sys.stdout.flush()
|
||||
|
||||
resp = ''.join(resp)
|
||||
self.update_files(resp)
|
||||
|
||||
|
||||
def run_edit(self):
|
||||
prompt = ''
|
||||
for fname in self.fnames:
|
||||
prompt += self.quoted_file(fname)
|
||||
|
||||
completion = openai.Edit.create(
|
||||
model="code-davinci-edit-001",
|
||||
instruction= prompt,
|
||||
input=prompt,
|
||||
#max_tokens=2048,
|
||||
temperature=0,
|
||||
)
|
||||
dump(completion)
|
||||
resp = []
|
||||
for chunk in completion:
|
||||
try:
|
||||
text = chunk.choices[0].text
|
||||
resp.append(text)
|
||||
except AttributeError:
|
||||
continue
|
||||
sys.stdout.write(text)
|
||||
sys.stdout.flush()
|
||||
|
||||
resp = ''.join(resp)
|
||||
self.update_files(resp)
|
||||
|
||||
|
||||
def get_files_content(self):
|
||||
prompt = ''
|
||||
for fname in self.fnames:
|
||||
prompt += self.quoted_file(fname)
|
||||
prompt += ''''
|
||||
|
||||
YOU CAN ONLY EDIT THESE FILES.
|
||||
NEVER REPLY WITH WHOLE FILES LIKE THIS!
|
||||
ONLY TELL ME CODE CHANGES USING ORIGINAL/UPDATED EDIT COMMANDS!
|
||||
'''
|
||||
prompt += prompts.files_content_suffix
|
||||
return prompt
|
||||
|
||||
def get_input(self):
|
||||
|
@ -209,7 +88,7 @@ ONLY TELL ME CODE CHANGES USING ORIGINAL/UPDATED EDIT COMMANDS!
|
|||
|
||||
def run(self):
|
||||
messages = [
|
||||
dict(role = 'system', content = self.system_prompt),
|
||||
dict(role = 'system', content = prompts.main_system),
|
||||
]
|
||||
|
||||
did_edits = False
|
||||
|
@ -219,9 +98,10 @@ ONLY TELL ME CODE CHANGES USING ORIGINAL/UPDATED EDIT COMMANDS!
|
|||
return
|
||||
|
||||
if did_edits:
|
||||
files_prefix = 'I made your suggested changes, here are the updated files:'
|
||||
files_prefix = prompts.files_content_prefix_edited
|
||||
else:
|
||||
files_prefix = 'Here are the files:'
|
||||
files_prefix = prompts.files_content_prefix_plain
|
||||
|
||||
files_prefix += '\n\n'
|
||||
|
||||
messages += [
|
||||
|
@ -405,35 +285,15 @@ ONLY TELL ME CODE CHANGES USING ORIGINAL/UPDATED EDIT COMMANDS!
|
|||
|
||||
fname = Path(fname)
|
||||
content = fname.read_text()
|
||||
prompt = f'''
|
||||
To complete this request:
|
||||
|
||||
{request}
|
||||
|
||||
You need to apply this change:
|
||||
|
||||
{edit}
|
||||
|
||||
To this file:
|
||||
|
||||
{fname}
|
||||
```
|
||||
{content}
|
||||
```
|
||||
|
||||
ONLY OUTPUT {fname} !!!
|
||||
'''
|
||||
sys_prompt = '''
|
||||
You are an expert code editor.
|
||||
Perform the requested edit.
|
||||
Output ONLY the new version of the file.
|
||||
Just that one file.
|
||||
Do not output explanations!
|
||||
Do not wrap the output in ``` delimiters.
|
||||
'''
|
||||
prompt = prompts.editor_user.format(
|
||||
request = request,
|
||||
edit = edit,
|
||||
fname = fname,
|
||||
content = content,
|
||||
)
|
||||
|
||||
messages = [
|
||||
dict(role = 'system', content = sys_prompt),
|
||||
dict(role = 'system', content = prompts.editor_system),
|
||||
dict(role = 'user', content = prompt),
|
||||
]
|
||||
res = self.send(messages, show_progress = len(content) + len(edit)/2)
|
||||
|
@ -458,37 +318,11 @@ Do not wrap the output in ``` delimiters.
|
|||
|
||||
return res
|
||||
|
||||
|
||||
def test_do_gpt_powered_replace(coder):
|
||||
fname = Path('../easy-chat/index.html')
|
||||
edit = '''
|
||||
../easy-chat/index.html
|
||||
<<<<<<< ORIGINAL
|
||||
<p class="user"><span class="fa fa-volume-up" onclick="speak(this.parentNode)"></span><span>Hello!</span></p>
|
||||
<p class="assistant"><span class="fa fa-volume-up" onclick="speak(this.parentNode)"></span><span>How</span> <span>can</span> <span>I</span> <span>help</span>
|
||||
<span>you?</span></p>
|
||||
=======
|
||||
<p class="user"><span>Hello!</span><span class="fa fa-volume-up" onclick="speak(this.parentNode)"></span></p>
|
||||
<p class="assistant"><span>How</span> <span>can</span> <span>I</span> <span>help</span><span>you?</span><span class="fa fa-volume-up" onclick="speak(this.parentNode)"></span></p>
|
||||
>>>>>>> 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)
|
||||
|
||||
#coder.update_files(Path('tmp.commands').read_text()) ; sys.exit()
|
||||
|
||||
coder.run()
|
||||
|
||||
'''
|
||||
Change all the speaker icons to orange.
|
||||
|
||||
Currently the speaker icons come before the text of each message. Move them so they come after the text instead.
|
||||
|
||||
Move the About and New Chat links into a hamburger menu.
|
||||
'''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue