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

add gpt-4o support for num_tokens #2

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
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
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, see https://github.com/related-sciences/oai_utils/pull/2
]:
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
Loading