This commit is contained in:
Paul Gauthier 2023-04-03 21:31:37 -07:00
parent 41aed411ea
commit 6fc98423c6

View file

@ -18,10 +18,8 @@ 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. You are to carefully study the provided code and follow the user instructions.
Make the requested change to the provided code and output the changed code. Be detail oriented, explicit and thorough in following user instructions.
MAKE NO OTHER CHANGES!
Do not provide explanations!
''' '''
class Chat: class Chat:
@ -33,8 +31,20 @@ class Chat:
def request(self, prompt): def request(self, prompt):
self.request_prompt = prompt self.request_prompt = prompt
def run(self): def plan(self):
prompt = self.request_prompt + '\n###\n' prompt = '''
Briefly describe all the code changes needed to complete the user request.
Think carefully about the code and the request!
Just describe ALL the changes needed to complete the request.
Just describe the changes, don't output code for them.
Be thorough. Describe ALL the changes needed to complete the request.
Only describe changes related to the request.
Don't output the changed code!
Just briefly describe the changes.
Request:
'''
prompt += self.request_prompt + '\n###\n'
for fname in self.fnames: for fname in self.fnames:
prompt += '\n' prompt += '\n'
@ -43,36 +53,70 @@ class Chat:
prompt += fname.read_text() prompt += fname.read_text()
prompt += '\n```\n' prompt += '\n```\n'
messages = [ ###
#print(self.system_prompt)
#print(prompt)
#sys.exit()
self.messages = [
dict(role = 'system', content = self.system_prompt), dict(role = 'system', content = self.system_prompt),
dict(role = 'user', content = prompt), dict(role = 'user', content = prompt),
] ]
self.plan = self.send(self.messages)
self.messages.append(dict(role = 'assistant', content = self.plan))
def send(self, messages):
completion = openai.ChatCompletion.create( completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",
messages=messages, messages=messages,
stream = True temperature=0,
stream = True,
) )
resp = []
for chunk in completion: for chunk in completion:
try: try:
text = chunk.choices[0].delta.content text = chunk.choices[0].delta.content
resp.append(text)
except AttributeError: except AttributeError:
continue continue
sys.stdout.write(text) sys.stdout.write(text)
sys.stdout.flush() sys.stdout.flush()
print()
print('='*40)
resp = ''.join(resp)
return resp
def update_files(self):
for fname in self.fnames:
self.update_file(fname)
def update_file(self, fname):
messages = list(self.messages)
messages.append(
dict(role = 'user',
content = f'''
Make the requested changes to {fname.name} and output the changed code.
MAKE NO OTHER CHANGES!
JUST OUTPUT CODE.
NO EXPLANATIONS.
'''
))
new_content = chat.send(messages)
fname.write_text(new_content)
chat = Chat() chat = Chat()
chat.system(prompt_webdev) chat.system(prompt_webdev)
chat.request('''
Replace *ALL* the speaker icons with a speech bubble icon.
''')
dname = Path('../easy-chat') dname = Path('../easy-chat')
chat.file(dname / 'index.html') chat.file(dname / 'index.html')
chat.file(dname / 'chat.js')
chat.file(dname / 'chat.css') chat.file(dname / 'chat.css')
chat.file(dname / 'chat.js')
chat.run() chat.request('Change ALL the speaker icons to speech bubble icons.')
chat.plan()
chat.update_files()