diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.py index 314176767dc..9037e139d11 100644 --- a/litellm/litellm_core_utils/token_counter.py +++ b/litellm/litellm_core_utils/token_counter.py @@ -3,11 +3,12 @@ import base64 import io import math +import re import struct from collections.abc import Awaitable, Callable, Iterable, Mapping, Sequence from dataclasses import dataclass from functools import cached_property -from itertools import accumulate, chain +from itertools import accumulate, chain, islice from types import MappingProxyType from typing import TYPE_CHECKING, Final, Literal, Protocol, TypeAlias, cast @@ -406,8 +407,7 @@ class _PdfBox(Protocol): class _PdfContent(Protocol): - @property - def operations(self) -> Sequence[tuple[object, bytes]]: ... + def get_data(self) -> bytes: ... class _PdfPage(Protocol): @@ -1043,26 +1043,44 @@ def _pdf_xobjects(container: Mapping[str, object], inherited: _PdfXObjects) -> _ return _pdf_dictionary(_pdf_value(resources, "/XObject")) +_PDF_WHITESPACE: Final = rb"\x00\t\n\x0c\r " +_PDF_TOKEN_END: Final = _PDF_WHITESPACE + rb"()<>\[\]{}/%" +_PDF_XOBJECT_DRAW: Final = re.compile( + rb"/([^" + _PDF_TOKEN_END + rb"]*)[" + _PDF_WHITESPACE + rb"]+Do(?![^" + _PDF_TOKEN_END + rb"])" +) +_PDF_INLINE_IMAGE: Final = re.compile(rb"(? str | None: + try: + return raw.decode(charset) + except UnicodeDecodeError: + return None + + +def _pdf_name(escaped: bytes) -> str: + raw: Final = _PDF_NAME_ESCAPE.sub(lambda escape: bytes.fromhex(escape[1].decode("ascii")), escaped) + decoded: Final = (_decoded(raw, charset) for charset in _PDF_NAME_CHARSETS) + return "/" + next((name for name in decoded if name is not None), raw.decode("latin-1")) + + def _pdf_content_summary(content: _PdfContent) -> _PdfContentSummary: - operations: Final = content.operations - operand_lists: Final = (operands for operands, operator in operations if operator == b"Do") - names: Final[Iterable[object]] = ( - cast(Sequence[object], operands)[0] # cast-ok: pypdf parses an operator's operands into a list - for operands in operand_lists - if isinstance(operands, list) and operands - ) + data: Final = content.get_data() + draws: Final = islice(_PDF_XOBJECT_DRAW.finditer(data), PDF_MAX_XOBJECT_DRAWS_PER_PAGE + 1) return _PdfContentSummary( - drawn_names=tuple(name for name in names if isinstance(name, str)), - draws_inline_image=any(operator == b"INLINE IMAGE" for _, operator in operations), + drawn_names=tuple(_pdf_name(draw[1]) for draw in draws), + draws_inline_image=_PDF_INLINE_IMAGE.search(data) is not None, ) def _pdf_form_summary(form: Mapping[str, object]) -> _PdfContentSummary | None: - from pypdf.generic import ContentStream, StreamObject + from pypdf.generic import StreamObject if not isinstance(form, StreamObject): return None - return _pdf_content_summary(cast(_PdfContent, ContentStream(form, None))) # cast-ok: pypdf is untyped + return _pdf_content_summary(cast(_PdfContent, form)) # cast-ok: pypdf is untyped def _pdf_drawn_xobjects(drawing: _PdfDrawing) -> tuple[_PdfXObjectUse, ...]: diff --git a/tests/unit/litellm_core_utils/test_token_counter.py b/tests/unit/litellm_core_utils/test_token_counter.py index 9166f48d466..77e67bbd23b 100644 --- a/tests/unit/litellm_core_utils/test_token_counter.py +++ b/tests/unit/litellm_core_utils/test_token_counter.py @@ -2282,6 +2282,32 @@ def test_openai_model_finds_an_image_a_shared_form_without_resources_draws_from_ ) == _rendered_page_cost() +@pytest.mark.timeout(10) +def test_openai_model_prices_a_page_of_a_million_drawing_operators_promptly(): + """A page of a million drawing operators and no image took about 30 seconds to price, since every operator was + parsed to find the images it draws, while the stream compresses into a request a few kilobytes long.""" + model: Final = "gpt-4o" + + assert _pdf_cost(model, _pdf_base64(("",), drawing=b" q Q" * 1_000_000, page_resources="<< >>")) == 0 + + +@pytest.mark.parametrize("escaped_name", ["/Im#231", "/#C4#E3", "/caf#C3#A9"], ids=["hex_escape", "gbk", "utf8"]) +def test_openai_model_finds_an_image_drawn_by_an_escaped_name(escaped_name: str): + """A name written with #xx escapes reads the same in the content stream and in /Resources, decoded the way pypdf + decodes it, so an image drawn under such a name still prices the page as rendered.""" + model: Final = "gpt-4o" + + assert _pdf_cost( + model, + _pdf_base64( + ("",), + xobjects=(_ONE_PIXEL_IMAGE,), + drawing=f" {escaped_name} Do".encode(), + page_resources=f"<< /XObject << {escaped_name} 6 0 R >> >>", + ), + ) == _rendered_page_cost() + + @pytest.mark.timeout(10) def test_openai_model_reads_resources_shared_by_many_forms_once(): """3,000 forms that all list the same /Resources took 17 seconds to price before each dictionary was read once."""