refactor: add retry loop to simple_send_with_retries function

This commit is contained in:
Paul Gauthier (aider) 2024-10-28 15:05:07 -07:00
parent f9c45432e6
commit bc515cf74a
2 changed files with 23 additions and 15 deletions

View file

@ -1124,7 +1124,6 @@ class Coder:
exhausted = False
interrupted = False
try:
# ai: replicate this try/except retry loop...
while True:
try:
yield from self.send(messages, functions=self.functions)
@ -1137,7 +1136,6 @@ class Coder:
self.io.tool_output(f"Retrying in {retry_delay:.1f} seconds...")
time.sleep(retry_delay)
continue
# ai: ... down to here
except KeyboardInterrupt:
interrupted = True
break

View file

@ -1,5 +1,6 @@
import hashlib
import json
import time
import backoff
@ -102,18 +103,27 @@ def send_completion(
return hash_object, res
# ai: in this function!
def simple_send_with_retries(model_name, messages, extra_params=None):
try:
kwargs = {
"model_name": model_name,
"messages": messages,
"functions": None,
"stream": False,
"extra_params": extra_params,
}
retry_delay = 0.125
while True:
try:
kwargs = {
"model_name": model_name,
"messages": messages,
"functions": None,
"stream": False,
"extra_params": extra_params,
}
_hash, response = send_completion(**kwargs)
return response.choices[0].message.content
except AttributeError:
return
_hash, response = send_completion(**kwargs)
return response.choices[0].message.content
except retry_exceptions() as err:
print(str(err))
retry_delay *= 2
if retry_delay > RETRY_TIMEOUT:
break
print(f"Retrying in {retry_delay:.1f} seconds...")
time.sleep(retry_delay)
continue
except AttributeError:
return