Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pr_agent/algo/token_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +52 to +60
Copy link
Contributor

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 that Environment and encoder.encode might raise, and catch those specifically. [best practice]

Suggested change
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
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 (TemplateError, EncodeError) as e:
# Consider logging the exception here
return -1


def count_tokens(self, patch: str) -> int:
"""
Expand All @@ -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=()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: The use of disallowed_special=() as an argument in encoder.encode might indicate that you're allowing all special characters without any restrictions. If this is intentional, it's fine, but if there are specific special characters that should not be encoded, consider specifying them explicitly for clarity and security. [possible issue]

Suggested change
return len(self.encoder.encode(patch, disallowed_special=()))
# If there are specific special characters you want to disallow, specify them like this:
# disallowed_special=('special_char1', 'special_char2')
return len(self.encoder.encode(patch, disallowed_special=()))

Loading