Skip to content

Commit 0d7b9e7

Browse files
authored
Merge pull request #13 from pamelafox/supporttoolsfallback
Support fallback when calculating tokens for non-openAI models
2 parents 3b9df19 + a78ea98 commit 0d7b9e7

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

.github/workflows/python.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
python -m pip install --upgrade pip
2626
python3 -m pip install -e '.[dev]'
2727
- name: Lint with ruff
28-
run: ruff .
28+
run: ruff check .
2929
- name: Check formatting with black
3030
run: black . --check --verbose
3131
- name: Run unit tests

src/openai_messages_token_helper/model_helper.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def count_tokens_for_system_and_tools(
144144
if system_message:
145145
tokens += count_tokens_for_message(model, system_message, default_to_cl100k)
146146
if tools:
147-
encoding = tiktoken.encoding_for_model(model)
148147
tokens += len(encoding.encode(format_function_definitions(tools)))
149148
tokens += 9 # Additional tokens for function definition of tools
150149
# If there's a system message and tools are present, subtract four tokens

tests/test_modelhelper.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from openai_messages_token_helper import count_tokens_for_message, count_tokens_for_system_and_tools, get_token_limit
33

4-
from .functions import FUNCTION_COUNTS
4+
from .functions import FUNCTION_COUNTS, search_sources_toolchoice_auto
55
from .messages import system_message, system_message_with_name, text_and_image_message, user_message
66

77

@@ -105,3 +105,17 @@ def test_count_tokens_for_system_and_tools(function_count_pair):
105105
assert (
106106
diff >= 0 and diff <= 3
107107
), f"Expected {expected_tokens} tokens, got {counted_tokens}. Counted tokens is only allowed to be off by 3 in the over-counting direction."
108+
109+
110+
def test_count_tokens_for_system_and_tools_fallback(caplog):
111+
function_count_pair = search_sources_toolchoice_auto
112+
with caplog.at_level("WARNING"):
113+
counted_tokens = count_tokens_for_system_and_tools(
114+
"llama-3.1",
115+
function_count_pair["system_message"],
116+
function_count_pair["tools"],
117+
function_count_pair["tool_choice"],
118+
default_to_cl100k=True,
119+
)
120+
assert counted_tokens == function_count_pair["count"]
121+
assert "Model llama-3.1 not found, defaulting to CL100k encoding" in caplog.text

0 commit comments

Comments
 (0)