mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-22 21:34:59 +00:00
better
This commit is contained in:
parent
eea2c87210
commit
ca813ca19c
1 changed files with 47 additions and 31 deletions
78
coder.py
78
coder.py
|
@ -19,9 +19,33 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||||
prompt_webdev = '''
|
prompt_webdev = '''
|
||||||
I want you to act as a web development expert.
|
I want you to act as a web development expert.
|
||||||
I want you to answer only with code.
|
I want you to answer only with code.
|
||||||
Make the requested change to the provided code and output the changed code.
|
Make all the requested changes to the provided code and output the changed code.
|
||||||
MAKE NO OTHER CHANGES!
|
MAKE NO OTHER CHANGES!
|
||||||
Do not provide explanations!
|
Do not provide explanations!
|
||||||
|
|
||||||
|
For each file that has changes, output it like this:
|
||||||
|
|
||||||
|
filename.ext
|
||||||
|
```
|
||||||
|
... file content ...
|
||||||
|
```
|
||||||
|
'''
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
filename.ext
|
||||||
|
```
|
||||||
|
... file content ...
|
||||||
|
```
|
||||||
'''
|
'''
|
||||||
|
|
||||||
class Coder:
|
class Coder:
|
||||||
|
@ -41,7 +65,7 @@ class Coder:
|
||||||
prompt += '\n```\n'
|
prompt += '\n```\n'
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
def setup(self):
|
def run(self):
|
||||||
prompt = ''
|
prompt = ''
|
||||||
for fname in self.fnames:
|
for fname in self.fnames:
|
||||||
prompt += self.quoted_file(fname)
|
prompt += self.quoted_file(fname)
|
||||||
|
@ -49,7 +73,13 @@ class Coder:
|
||||||
prompt += '\n###\n'
|
prompt += '\n###\n'
|
||||||
prompt += self.request_prompt
|
prompt += self.request_prompt
|
||||||
|
|
||||||
self.prompt = prompt
|
messages = [
|
||||||
|
dict(role = 'system', content = self.system_prompt),
|
||||||
|
dict(role = 'user', content = prompt),
|
||||||
|
]
|
||||||
|
|
||||||
|
content = self.send(messages)
|
||||||
|
self.update_files(content)
|
||||||
|
|
||||||
def send(self, messages):
|
def send(self, messages):
|
||||||
completion = openai.ChatCompletion.create(
|
completion = openai.ChatCompletion.create(
|
||||||
|
@ -74,36 +104,23 @@ class Coder:
|
||||||
resp = ''.join(resp)
|
resp = ''.join(resp)
|
||||||
return resp
|
return resp
|
||||||
|
|
||||||
def update_files(self):
|
def update_files(self, content):
|
||||||
random.shuffle(self.fnames)
|
|
||||||
for fname in self.fnames:
|
for fname in self.fnames:
|
||||||
self.update_file(fname)
|
dump(fname)
|
||||||
|
self.update_file(fname, content)
|
||||||
|
|
||||||
def update_file(self, fname):
|
def update_file(self, fname, content):
|
||||||
prompt = self.prompt
|
start = f'{fname.name}\n```\n'
|
||||||
prompt += f'''
|
end = '\n```'
|
||||||
Output the new {fname.name} which includes all the requested changes.
|
|
||||||
MAKE NO OTHER CHANGES.
|
|
||||||
Just output {fname.name}.
|
|
||||||
'''
|
|
||||||
|
|
||||||
messages = [
|
if start not in content:
|
||||||
dict(role = 'system', content = self.system_prompt),
|
print(f'No content for {fname}')
|
||||||
dict(role = 'user', content = prompt),
|
|
||||||
]
|
|
||||||
|
|
||||||
content = self.send(messages)
|
|
||||||
if content.strip() == 'NONE':
|
|
||||||
return
|
return
|
||||||
|
|
||||||
lines = content.splitlines()
|
content = content.split(start)[1]
|
||||||
if lines[0].startswith(fname.name):
|
content = content.split(end)[0]
|
||||||
lines = lines[1:]
|
|
||||||
if lines[0].startswith('```'):
|
fname.write_text(content)
|
||||||
lines = lines[1:]
|
|
||||||
if lines[-1].startswith('```'):
|
|
||||||
lines = lines[:-1]
|
|
||||||
fname.write_text('\n'.join(lines))
|
|
||||||
|
|
||||||
coder = Coder()
|
coder = Coder()
|
||||||
|
|
||||||
|
@ -119,8 +136,7 @@ coder.file(dname / 'chat.js')
|
||||||
#sys.exit()
|
#sys.exit()
|
||||||
|
|
||||||
coder.request('''
|
coder.request('''
|
||||||
Make the speaker icons red.
|
Refactor the css and remove any redundant or useless code.
|
||||||
''')
|
''')
|
||||||
|
|
||||||
coder.setup()
|
coder.run()
|
||||||
coder.update_files()
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue