-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix (#90) split text if token larger than 4096 #106
base: main
Are you sure you want to change the base?
Conversation
should fix #90 |
|
||
message_log = [ | ||
{ | ||
"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", | ||
} | ||
] | ||
count_tokens = num_tokens_from_messages(message_log) | ||
consumed_tokens = 0 | ||
t_text = "" | ||
if count_tokens > 4000: | ||
print("too long!") | ||
|
||
splits = count_tokens // 4000 + 1 | ||
|
||
text_list = text.split(".") | ||
sub_text = "" | ||
t_sub_text = "" | ||
for n in range(splits): | ||
text_segment = text_list[n * splits : (n + 1) * splits] | ||
sub_text = ".".join(text_segment) | ||
print(sub_text) | ||
|
||
completion = openai.ChatCompletion.create( | ||
model="gpt-3.5-turbo", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
# english prompt here to save tokens | ||
"content": f"Please help me to translate,`{sub_text}` to {self.language}, please return only translated content not include the origin text", | ||
} | ||
], | ||
) | ||
t_sub_text = ( | ||
completion["choices"][0] | ||
.get("message") | ||
.get("content") | ||
.encode("utf8") | ||
.decode() | ||
) | ||
print(t_sub_text) | ||
consumed_tokens += completion["usage"]["prompt_tokens"] | ||
|
||
t_text = t_text + t_sub_text | ||
|
||
else: | ||
try: | ||
completion = openai.ChatCompletion.create( | ||
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() | ||
) | ||
consumed_tokens += completion["usage"]["prompt_tokens"] | ||
|
||
except Exception as e: | ||
# TIME LIMIT for open api please pay | ||
key_len = self.key.count(",") + 1 | ||
sleep_time = int(60 / key_len) | ||
time.sleep(sleep_time) | ||
print(e, f"will sleep {sleep_time} seconds") | ||
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() | ||
) | ||
consumed_tokens += completion["usage"]["prompt_tokens"] | ||
|
||
print(t_text) | ||
print(f"{consumed_tokens} prompt tokens used.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this functions is too long let's split it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verified
have passed ' black . --check' local, but not pass ci. |
pip insall -U black |
already formatted.
|
no worry I will take a look tonight or tomorrow. |
split text when token larger than the quota