mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-25 14:55:00 +00:00
cleanup; readline
This commit is contained in:
parent
96209d67e1
commit
6b0f9e1afb
1 changed files with 25 additions and 65 deletions
90
coder.py
90
coder.py
|
@ -8,6 +8,7 @@ import copy
|
||||||
import random
|
import random
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import readline
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
@ -17,6 +18,12 @@ import openai
|
||||||
|
|
||||||
from dump import dump
|
from dump import dump
|
||||||
|
|
||||||
|
history_file = '.coder.history'
|
||||||
|
try:
|
||||||
|
readline.read_history_file(history_file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
openai.api_key = os.getenv("OPENAI_API_KEY")
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||||
|
|
||||||
prompt_webdev = '''
|
prompt_webdev = '''
|
||||||
|
@ -40,7 +47,7 @@ the original chunk
|
||||||
>>>>>>> UPDATED
|
>>>>>>> UPDATED
|
||||||
|
|
||||||
NEVER REPLY WITH AN ENTIRE FILE!
|
NEVER REPLY WITH AN ENTIRE FILE!
|
||||||
ONLY USE THE ABOVE ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES!
|
ONLY USE THIS ORIGINAL/UPDATED FORMAT TO DESCRIBE CODE CHANGES!
|
||||||
'''
|
'''
|
||||||
|
|
||||||
prompt_comments = '''
|
prompt_comments = '''
|
||||||
|
@ -155,8 +162,8 @@ class Coder:
|
||||||
self.update_files(resp)
|
self.update_files(resp)
|
||||||
|
|
||||||
|
|
||||||
def get_files_message(self):
|
def get_files_content(self):
|
||||||
prompt = 'Here is the current content of the files. NEVER OUTPUT ENTIRE FILES! NEVER OUTPUT IN THIS FORMAT!\n'
|
prompt = 'Here is the content of the files. NEVER OUTPUT ENTIRE FILES! NEVER OUTPUT IN THIS FORMAT!\n'
|
||||||
for fname in self.fnames:
|
for fname in self.fnames:
|
||||||
prompt += self.quoted_file(fname)
|
prompt += self.quoted_file(fname)
|
||||||
return prompt
|
return prompt
|
||||||
|
@ -171,54 +178,30 @@ MAKE ANY CHANGES BASED OFF THESE FILES!
|
||||||
|
|
||||||
print()
|
print()
|
||||||
print('='*60)
|
print('='*60)
|
||||||
sys.stdout.write('> ')
|
inp = input('> ')
|
||||||
sys.stdout.flush()
|
|
||||||
inp = input()
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
if inp == 'fix':
|
#readline.add_history(inp)
|
||||||
inp = '''
|
readline.write_history_file(history_file)
|
||||||
It looks like you are trying to specify code changes. Repeat your previous message, but use the exact BEFORE/AFTER command format, like this:
|
|
||||||
|
|
||||||
BEFORE path/to/filename.ext
|
|
||||||
```
|
|
||||||
... unchanged lines from the original file ...
|
|
||||||
... only include lines around needed changes! ...
|
|
||||||
... NEVER INCLUDE AN ENTIRE FILE! ...
|
|
||||||
```
|
|
||||||
AFTER
|
|
||||||
```
|
|
||||||
... new lines to replace them with ...
|
|
||||||
```
|
|
||||||
|
|
||||||
The ``` delimiters are very important!
|
|
||||||
'''
|
|
||||||
return inp
|
return inp
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
inp = self.get_input()
|
|
||||||
|
|
||||||
prompt = ''
|
|
||||||
prompt += inp
|
|
||||||
prompt += '\n###\n'
|
|
||||||
prompt += 'Here is the content of the files. DO NOT OUTPUT CODE USING THIS FORMAT!!\n'
|
|
||||||
prompt += self.get_files_message()
|
|
||||||
|
|
||||||
messages = [
|
messages = [
|
||||||
dict(role = 'system', content = self.system_prompt),
|
dict(role = 'system', content = self.system_prompt),
|
||||||
dict(role = 'user', content = prompt),
|
|
||||||
]
|
]
|
||||||
file_msg_no = 1
|
|
||||||
|
|
||||||
content = self.send(messages)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
messages.append(
|
inp = self.get_input()
|
||||||
dict(
|
if len(messages) == 1:
|
||||||
role = 'assistant',
|
inp += '\n' + self.get_files_content()
|
||||||
content = content,
|
|
||||||
)
|
message = dict(role = 'user', content = inp)
|
||||||
)
|
messages.append(message)
|
||||||
|
|
||||||
|
content = self.send(messages)
|
||||||
|
message = dict(role = 'assistant', content = content)
|
||||||
|
messages.append(message)
|
||||||
|
|
||||||
print()
|
print()
|
||||||
try:
|
try:
|
||||||
|
@ -228,32 +211,9 @@ The ``` delimiters are very important!
|
||||||
print(err)
|
print(err)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
inp = self.get_input()
|
|
||||||
|
|
||||||
if False and self.files_modified():
|
|
||||||
for fname in self.fnames:
|
|
||||||
self.add_file(fname)
|
|
||||||
|
|
||||||
print('Files have changed, informing ChatGPT.')
|
|
||||||
print()
|
|
||||||
|
|
||||||
messages[file_msg_no] = dict(role = 'user', content = '<<outdated list of the files and their content -- removed>>')
|
|
||||||
messages.append(
|
|
||||||
dict(
|
|
||||||
role = 'user',
|
|
||||||
content = self.change_notice + self.get_files_message(),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
file_msg_no = len(messages)-1
|
|
||||||
|
|
||||||
message = dict(role = 'user', content = inp)
|
|
||||||
messages.append(message)
|
|
||||||
content = self.send(messages)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def send(self, messages):
|
def send(self, messages):
|
||||||
#dump(messages)
|
for msg in messages:
|
||||||
|
dump(msg)
|
||||||
|
|
||||||
completion = openai.ChatCompletion.create(
|
completion = openai.ChatCompletion.create(
|
||||||
model="gpt-3.5-turbo",
|
model="gpt-3.5-turbo",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue