Skip to content

Commit f0975ea

Browse files
authored
Merge pull request #129 from FSoft-AI4Code/fix/tiktoken-special-tokens
fix: count literal special tokens like <|endoftext|> as plain text
2 parents 64c2b60 + 28d3734 commit f0975ea

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

‎codewiki/src/be/utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def count_tokens(text: str) -> int:
5656
"""
5757
Count the number of tokens in a text.
5858
"""
59-
length = len(enc.encode(text))
59+
# source code may contain literal special tokens such as "<|endoftext|>"
60+
# (e.g. CLIP tokenizers); count them as plain text instead of raising
61+
length = len(enc.encode(text, disallowed_special=()))
6062
# logger.debug(f"Number of tokens: {length}")
6163
return length
6264

0 commit comments

Comments
 (0)