fix (#90) split text if token larger than 4096#106
Open
jeffery9 wants to merge 6 commits intoyihong0618:mainfrom
Open
fix (#90) split text if token larger than 4096#106jeffery9 wants to merge 6 commits intoyihong0618:mainfrom
jeffery9 wants to merge 6 commits intoyihong0618:mainfrom
Conversation
Contributor
Author
|
should fix #90 |
yihong0618
reviewed
Mar 10, 2023
Comment on lines
+22
to
+106
|
|
||
| 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.") |
Owner
There was a problem hiding this comment.
this functions is too long let's split it
Contributor
Author
|
have passed ' black . --check' local, but not pass ci. |
Owner
|
pip insall -U black |
Contributor
Author
already formatted. |
Owner
|
no worry I will take a look tonight or tomorrow. |
Merged
wayhome
pushed a commit
to wayhome/bilingual_book_maker
that referenced
this pull request
Aug 29, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
split text when token larger than the quota