mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-05 19:15:34 +00:00
refactor(translator): extract get_translation method (#112)
- Extracted get_translation method to improve code readability and maintainability - Moved the code that handles API key and completion creation to the new method
This commit is contained in:
parent
1ef61a75c8
commit
f566feabcb
@ -15,48 +15,43 @@ class ChatGPTAPI(Base):
|
|||||||
def rotate_key(self):
|
def rotate_key(self):
|
||||||
openai.api_key = next(self.keys)
|
openai.api_key = next(self.keys)
|
||||||
|
|
||||||
def translate(self, text):
|
def get_translation(self, text):
|
||||||
print(text)
|
|
||||||
self.rotate_key()
|
self.rotate_key()
|
||||||
|
completion = openai.ChatCompletion.create(
|
||||||
|
model="gpt-3.5-turbo",
|
||||||
|
messages=[
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": f"Please help me to translate,`{text}` to {self.language}, please return only translated content not include the origin text",
|
||||||
|
}
|
||||||
|
],
|
||||||
|
)
|
||||||
|
t_text = (
|
||||||
|
completion["choices"][0]
|
||||||
|
.get("message")
|
||||||
|
.get("content")
|
||||||
|
.encode("utf8")
|
||||||
|
.decode()
|
||||||
|
)
|
||||||
|
return t_text
|
||||||
|
|
||||||
|
def translate(self, text):
|
||||||
|
# todo: Determine whether to print according to the cli option
|
||||||
|
print(text)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
completion = openai.ChatCompletion.create(
|
t_text = self.get_translation(text)
|
||||||
model="gpt-3.5-turbo",
|
|
||||||
messages=[
|
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
# english prompt here to save tokens
|
|
||||||
"content": f"Please help me to translate,`{text}` to {self.language}, please return only translated content not include the origin text",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
)
|
|
||||||
t_text = (
|
|
||||||
completion["choices"][0]
|
|
||||||
.get("message")
|
|
||||||
.get("content")
|
|
||||||
.encode("utf8")
|
|
||||||
.decode()
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# TIME LIMIT for open api please pay
|
# todo: better sleep time? why sleep alawys about key_len
|
||||||
|
# 1. openai server error or own network interruption, sleep for a fixed time
|
||||||
|
# 2. an apikey has no money or reach limit, don’t sleep, just replace it with another apikey
|
||||||
|
# 3. all apikey reach limit, then use current sleep
|
||||||
sleep_time = int(60 / self.key_len)
|
sleep_time = int(60 / self.key_len)
|
||||||
time.sleep(sleep_time)
|
time.sleep(sleep_time)
|
||||||
print(e, f"will sleep {sleep_time} seconds")
|
print(e, f"will sleep {sleep_time} seconds")
|
||||||
self.rotate_key()
|
|
||||||
completion = openai.ChatCompletion.create(
|
t_text = self.get_translation(text)
|
||||||
model="gpt-3.5-turbo",
|
|
||||||
messages=[
|
# todo: Determine whether to print according to the cli option
|
||||||
{
|
|
||||||
"role": "user",
|
|
||||||
"content": f"Please help me to translate,`{text}` to {self.language}, please return only translated content not include the origin text",
|
|
||||||
}
|
|
||||||
],
|
|
||||||
)
|
|
||||||
t_text = (
|
|
||||||
completion["choices"][0]
|
|
||||||
.get("message")
|
|
||||||
.get("content")
|
|
||||||
.encode("utf8")
|
|
||||||
.decode()
|
|
||||||
)
|
|
||||||
print(t_text)
|
print(t_text)
|
||||||
return t_text
|
return t_text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user