-
Notifications
You must be signed in to change notification settings - Fork 564
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
Update token_handler.py #755
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -49,12 +49,15 @@ def _get_system_user_tokens(self, pr, encoder, vars: dict, system, user): | |||||||||
Returns: | ||||||||||
The sum of the number of tokens in the system and user strings. | ||||||||||
""" | ||||||||||
environment = Environment(undefined=StrictUndefined) | ||||||||||
system_prompt = environment.from_string(system).render(vars) | ||||||||||
user_prompt = environment.from_string(user).render(vars) | ||||||||||
system_prompt_tokens = len(encoder.encode(system_prompt)) | ||||||||||
user_prompt_tokens = len(encoder.encode(user_prompt)) | ||||||||||
return system_prompt_tokens + user_prompt_tokens | ||||||||||
try: | ||||||||||
environment = Environment(undefined=StrictUndefined) | ||||||||||
system_prompt = environment.from_string(system).render(vars) | ||||||||||
user_prompt = environment.from_string(user).render(vars) | ||||||||||
system_prompt_tokens = len(encoder.encode(system_prompt)) | ||||||||||
user_prompt_tokens = len(encoder.encode(user_prompt)) | ||||||||||
return system_prompt_tokens + user_prompt_tokens | ||||||||||
except: | ||||||||||
return -1 | ||||||||||
|
||||||||||
def count_tokens(self, patch: str) -> int: | ||||||||||
""" | ||||||||||
|
@@ -66,4 +69,4 @@ def count_tokens(self, patch: str) -> int: | |||||||||
Returns: | ||||||||||
The number of tokens in the patch string. | ||||||||||
""" | ||||||||||
return len(self.encoder.encode(patch, disallowed_special=())) | ||||||||||
return len(self.encoder.encode(patch, disallowed_special=())) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The use of
Suggested change
|
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.
Suggestion: It's recommended to catch specific exceptions instead of using a bare
except:
. This prevents the code from silently ignoring unexpected errors, which can make debugging more difficult. Identify the specific exceptions thatEnvironment
andencoder.encode
might raise, and catch those specifically. [best practice]