Skip to content

Commit 5c579fd

Browse files
authored
Merge pull request #22 from pamelafox/imagedetail
Assume auto for detail
2 parents eb59b9a + 4131c10 commit 5c579fd

File tree

9 files changed

+38
-7
lines changed

9 files changed

+38
-7
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.6.0
3+
rev: v5.0.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
88
- repo: https://github.com/astral-sh/ruff-pre-commit
9-
rev: v0.4.1
9+
rev: v0.9.0
1010
hooks:
1111
- id: ruff
1212
- repo: https://github.com/psf/black
13-
rev: 24.4.0
13+
rev: 24.10.0
1414
hooks:
1515
- id: black

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.1.11] - Jan 10, 2025
6+
7+
- When no "detail" is provided for an "image_url" message part, "auto" is now assumed.
8+
59
## [0.1.10] - Aug 7, 2024
610

711
- Add additional OpenAI.com model names to the `get_token_limit` function.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[project]
22
name = "openai-messages-token-helper"
33
description = "A helper library for estimating tokens used by messages sent through OpenAI Chat Completions API."
4-
version = "0.1.10"
4+
version = "0.1.11"
55
authors = [{name = "Pamela Fox"}]
66
requires-python = ">=3.9"
77
readme = "README.md"

src/openai_messages_token_helper/message_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def normalize_content(content: Union[str, Iterable[ChatCompletionContentPartPara
2626
return unicodedata.normalize("NFC", content)
2727
else:
2828
for part in content:
29-
if "image_url" not in part:
29+
if part["type"] == "text":
3030
part["text"] = unicodedata.normalize("NFC", part["text"])
3131
return content
3232

src/openai_messages_token_helper/model_helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ def count_tokens_for_message(model: str, message: ChatCompletionMessageParam, de
115115
if item["type"] == "text":
116116
num_tokens += len(encoding.encode(item["text"]))
117117
elif item["type"] == "image_url":
118-
num_tokens += count_tokens_for_image(item["image_url"]["url"], item["image_url"]["detail"], model)
118+
num_tokens += count_tokens_for_image(
119+
item["image_url"]["url"], item["image_url"].get("detail", "auto"), model
120+
)
119121
elif isinstance(value, str):
120122
num_tokens += len(encoding.encode(value))
121123
else:

tests/image_messages.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
"count_4o_mini": 8511,
1717
}
1818

19+
text_and_tiny_image_message_nodetail = {
20+
"message": {
21+
"role": "user",
22+
"content": [
23+
{"type": "text", "text": "Describe this picture:"},
24+
{
25+
"type": "image_url",
26+
"image_url": {
27+
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z/C/HgAGgwJ/lK3Q6wAAAABJRU5ErkJggg=="
28+
},
29+
},
30+
],
31+
},
32+
"count": 266,
33+
"count_4o_mini": 8511,
34+
}
35+
1936
text_and_tiny_image_message_low = {
2037
"message": {
2138
"role": "user",
@@ -52,4 +69,9 @@
5269
"count_4o_mini": 19842,
5370
}
5471

55-
IMAGE_MESSAGE_COUNTS = [text_and_tiny_image_message, text_and_tiny_image_message_low, text_and_large_image_message]
72+
IMAGE_MESSAGE_COUNTS = [
73+
text_and_tiny_image_message,
74+
text_and_tiny_image_message_nodetail,
75+
text_and_tiny_image_message_low,
76+
text_and_large_image_message,
77+
]

tests/test_imageshelper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22

33
import pytest
4+
45
from openai_messages_token_helper import count_tokens_for_image
56

67

tests/test_messagebuilder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
ChatCompletionToolChoiceOptionParam,
77
ChatCompletionToolParam,
88
)
9+
910
from openai_messages_token_helper import build_messages, count_tokens_for_message
1011

1112
from .functions import search_sources_toolchoice_auto

tests/test_modelhelper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
23
from openai_messages_token_helper import count_tokens_for_message, count_tokens_for_system_and_tools, get_token_limit
34

45
from .functions import FUNCTION_COUNTS, search_sources_toolchoice_auto

0 commit comments

Comments
 (0)