mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-06 11:35:49 +00:00
feat: add exponential backoff to gemini
This commit is contained in:
parent
3d0ad1c9ca
commit
09fb35c26f
@ -100,6 +100,11 @@ class Gemini(Base):
|
|||||||
genai.configure(api_key=next(self.keys))
|
genai.configure(api_key=next(self.keys))
|
||||||
|
|
||||||
def translate(self, text):
|
def translate(self, text):
|
||||||
|
delay = 1
|
||||||
|
exponential_base = 2
|
||||||
|
attempt_count = 0
|
||||||
|
max_attempts = 7
|
||||||
|
|
||||||
t_text = ""
|
t_text = ""
|
||||||
print(text)
|
print(text)
|
||||||
# same for caiyun translate src issue #279 gemini for #374
|
# same for caiyun translate src issue #279 gemini for #374
|
||||||
@ -108,26 +113,40 @@ class Gemini(Base):
|
|||||||
if len(text_list) > 1:
|
if len(text_list) > 1:
|
||||||
if text_list[0].isdigit():
|
if text_list[0].isdigit():
|
||||||
num = text_list[0]
|
num = text_list[0]
|
||||||
|
|
||||||
|
while attempt_count < max_attempts:
|
||||||
try:
|
try:
|
||||||
self.convo.send_message(
|
self.convo.send_message(
|
||||||
self.DEFAULT_PROMPT.format(text=text, language=self.language)
|
self.prompt.format(text=text, language=self.language)
|
||||||
)
|
)
|
||||||
print(text)
|
|
||||||
t_text = self.convo.last.text.strip()
|
t_text = self.convo.last.text.strip()
|
||||||
|
break
|
||||||
except StopCandidateException as e:
|
except StopCandidateException as e:
|
||||||
match = re.search(r'content\s*{\s*parts\s*{\s*text:\s*"([^"]+)"', str(e))
|
print(
|
||||||
if match:
|
f"Translation failed due to StopCandidateException: {e} Attempting to switch model..."
|
||||||
t_text = match.group(1)
|
)
|
||||||
t_text = re.sub(r"\\n", "\n", t_text)
|
self.rotate_model()
|
||||||
else:
|
|
||||||
t_text = "Can not translate"
|
|
||||||
except BlockedPromptException as e:
|
except BlockedPromptException as e:
|
||||||
print(str(e))
|
print(
|
||||||
t_text = "Can not translate by SAFETY reason.(因安全问题不能翻译)"
|
f"Translation failed due to BlockedPromptException: {e} Attempting to switch model..."
|
||||||
|
)
|
||||||
|
self.rotate_model()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(
|
||||||
t_text = "Can not translate by other reason.(因安全问题不能翻译)"
|
f"Translation failed due to {type(e).__name__}: {e} Will sleep {delay} seconds"
|
||||||
|
)
|
||||||
|
time.sleep(delay)
|
||||||
|
delay *= exponential_base
|
||||||
|
|
||||||
|
self.rotate_key()
|
||||||
|
if attempt_count >= 1:
|
||||||
|
self.rotate_model()
|
||||||
|
|
||||||
|
attempt_count += 1
|
||||||
|
|
||||||
|
if attempt_count == max_attempts:
|
||||||
|
print(f"Translation failed after {max_attempts} attempts.")
|
||||||
|
return
|
||||||
|
|
||||||
if self.context_flag:
|
if self.context_flag:
|
||||||
if len(self.convo.history) > 10:
|
if len(self.convo.history) > 10:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user