Skip to content

Commit

Permalink
add gpt-4o num_tokens support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklamiller committed Jun 24, 2024
1 parent 474d2cd commit 119beb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion oai_utils/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def num_tokens(messages: list[dict[str, str]] | str, model: str = "gpt-4") -> in
4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
)
tokens_per_name = -1 # if there's a name, the role is omitted
elif model == "gpt-4":
elif model in [
"gpt-4",
"gpt-4o", # TODO: confirm gpt-4o rules are same as gpt-4, appears so with example messages
]:
tokens_per_message = 3
tokens_per_name = 1
else:
Expand Down
11 changes: 11 additions & 0 deletions oai_utils/tests/openai_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
chat_completion_batch,
embedding,
)
from oai_utils.openai_utils import num_tokens

pytestmark = pytest.mark.openai_api_tests


@pytest.mark.parametrize("model", ["gpt-3.5-turbo", "gpt-4", "gpt-4o"])
def test_support_different_model_tokens(model):
assert (
num_tokens(
"What is the answer to life, the universe, and everything?", model=model
)
== 13
)


def test_chat_completion__dummy():
r = chat_completion("What is the answer to life, the universe, and everything?")
assert r.request.messages == [
Expand Down

0 comments on commit 119beb5

Please sign in to comment.