Skip to content

Commit 81a3a9e

Browse files
committed
Updated tiktoken warning + removed magix number
1 parent 30b09b7 commit 81a3a9e

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

plain2code_console.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from rich.style import Style
55
from rich.tree import Tree
66

7+
CHARACTERS_TO_TOKENS_RULE_OF_THUMB_RATIO = 4
8+
79

810
class Plain2CodeConsole(Console):
911
INFO_STYLE = Style()
@@ -20,7 +22,10 @@ def __init__(self):
2022

2123
self.llm_encoding = tiktoken.get_encoding("cl100k_base")
2224
except Exception as e:
23-
logging.warning(f"Failed to import tiktoken: {e}. Using character count / 4 instead.")
25+
logging.warning(
26+
"Failed to import optional library tiktoken. Using approximate instead of exact token count."
27+
)
28+
logging.debug(f"Exception: {e}")
2429
self.llm_encoding = None
2530

2631
def info(self, *args, **kwargs):
@@ -107,7 +112,7 @@ def _count_tokens(self, text):
107112
return len(self.llm_encoding.encode(text))
108113
except Exception:
109114
pass
110-
return len(text) // 4
115+
return len(text) // CHARACTERS_TO_TOKENS_RULE_OF_THUMB_RATIO
111116

112117
def print_resources(self, resources_list, linked_resources):
113118
if len(resources_list) == 0:

0 commit comments

Comments
 (0)