catch ^c in commit() and use default commit message

This commit is contained in:
Paul Gauthier 2023-05-09 00:38:45 -07:00
parent cb63d15b52
commit 9d3ed13359

View file

@ -38,13 +38,15 @@ class Coder:
except FileNotFoundError: except FileNotFoundError:
pass pass
self.main_model = main_model
if pretty: if pretty:
self.console = Console() self.console = Console()
else: else:
self.console = Console(force_terminal=True, no_color=True) self.console = Console(force_terminal=True, no_color=True)
self.main_model = main_model
if main_model == 'gpt-3.5-turbo':
self.console.print(f'[red bold]This tool will almost certainly fail to work with {main_model}')
for fname in files: for fname in files:
fname = Path(fname) fname = Path(fname)
if not fname.exists(): if not fname.exists():
@ -289,22 +291,23 @@ class Coder:
import time import time
from openai.error import RateLimitError from openai.error import RateLimitError
while True: self.resp = ""
try:
completion = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
stream=True,
)
break
except RateLimitError as e:
retry_after = e.retry_after
print(f"Rate limit exceeded. Retrying in {retry_after} seconds.")
time.sleep(retry_after)
interrupted = False interrupted = False
try: try:
while True:
try:
completion = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
stream=True,
)
break
except RateLimitError as e:
retry_after = e.retry_after
print(f"Rate limit exceeded. Retrying in {retry_after} seconds.")
time.sleep(retry_after)
if self.pretty and not silent: if self.pretty and not silent:
self.show_send_output_color(completion) self.show_send_output_color(completion)
else: else:
@ -315,8 +318,6 @@ class Coder:
return self.resp, interrupted return self.resp, interrupted
def show_send_output_plain(self, completion, silent): def show_send_output_plain(self, completion, silent):
self.resp = ""
for chunk in completion: for chunk in completion:
if chunk.choices[0].finish_reason not in (None, "stop"): if chunk.choices[0].finish_reason not in (None, "stop"):
dump(chunk.choices[0].finish_reason) dump(chunk.choices[0].finish_reason)
@ -331,8 +332,6 @@ class Coder:
sys.stdout.flush() sys.stdout.flush()
def show_send_output_color(self, completion): def show_send_output_color(self, completion):
self.resp = ""
with Live(vertical_overflow="scroll") as live: with Live(vertical_overflow="scroll") as live:
for chunk in completion: for chunk in completion:
if chunk.choices[0].finish_reason not in (None, "stop"): if chunk.choices[0].finish_reason not in (None, "stop"):
@ -463,7 +462,7 @@ class Coder:
commit_message = commit_message.strip().strip('"').strip() commit_message = commit_message.strip().strip('"').strip()
if interrupted: if interrupted:
raise KeyboardInterrupt commit_message = 'Saving dirty files before chat'
if prefix: if prefix:
commit_message = prefix + commit_message commit_message = prefix + commit_message