Spaces:
Build error
Build error
Commit ·
970307c
1
Parent(s): ca7cb92
Disable image compression until token counting is accurate
Browse filesImage compression inflated savings metrics by 3-4x because the tokenizer
counted base64 data as text tokens (330K per 1MB image). The compressor
itself works, but reported metrics were wrong.
TODO: re-enable once tokenizer extracts image dimensions and uses
Anthropic's formula (width*height/750) for accurate counting.
headroom/proxy/handlers/anthropic.py
CHANGED
|
@@ -39,7 +39,6 @@ class AnthropicHandlerMixin:
|
|
| 39 |
from headroom.proxy.helpers import (
|
| 40 |
MAX_MESSAGE_ARRAY_LENGTH,
|
| 41 |
MAX_REQUEST_BODY_SIZE,
|
| 42 |
-
_get_image_compressor,
|
| 43 |
_read_request_json,
|
| 44 |
)
|
| 45 |
from headroom.proxy.models import RequestLog
|
|
@@ -107,18 +106,24 @@ class AnthropicHandlerMixin:
|
|
| 107 |
if _bypass:
|
| 108 |
logger.info(f"[{request_id}] Bypass: skipping compression (header)")
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
# Extract headers and tags
|
| 124 |
headers = dict(request.headers.items())
|
|
|
|
| 39 |
from headroom.proxy.helpers import (
|
| 40 |
MAX_MESSAGE_ARRAY_LENGTH,
|
| 41 |
MAX_REQUEST_BODY_SIZE,
|
|
|
|
| 42 |
_read_request_json,
|
| 43 |
)
|
| 44 |
from headroom.proxy.models import RequestLog
|
|
|
|
| 106 |
if _bypass:
|
| 107 |
logger.info(f"[{request_id}] Bypass: skipping compression (header)")
|
| 108 |
|
| 109 |
+
# TODO: Re-enable image compression once token counting is accurate.
|
| 110 |
+
# Image compression was disabled because the tokenizer counted base64
|
| 111 |
+
# image data as text tokens (330K per 1MB image), inflating savings by
|
| 112 |
+
# 3-4x. The compressor itself works, but reported metrics were wrong.
|
| 113 |
+
# To re-enable: fix tokenizer to extract image dimensions and use
|
| 114 |
+
# Anthropic's formula (width*height/750), then uncomment below.
|
| 115 |
+
#
|
| 116 |
+
# if self.config.image_optimize and messages and not _bypass:
|
| 117 |
+
# compressor = _get_image_compressor()
|
| 118 |
+
# if compressor and compressor.has_images(messages):
|
| 119 |
+
# messages = compressor.compress(messages, provider="anthropic")
|
| 120 |
+
# if compressor.last_result:
|
| 121 |
+
# logger.info(
|
| 122 |
+
# f"Image compression: {compressor.last_result.technique.value} "
|
| 123 |
+
# f"({compressor.last_result.savings_percent:.0f}% saved, "
|
| 124 |
+
# f"{compressor.last_result.original_tokens} -> "
|
| 125 |
+
# f"{compressor.last_result.compressed_tokens} tokens)"
|
| 126 |
+
# )
|
| 127 |
|
| 128 |
# Extract headers and tags
|
| 129 |
headers = dict(request.headers.items())
|
headroom/proxy/handlers/openai.py
CHANGED
|
@@ -39,7 +39,6 @@ class OpenAIHandlerMixin:
|
|
| 39 |
COMPRESSION_TIMEOUT_SECONDS,
|
| 40 |
MAX_MESSAGE_ARRAY_LENGTH,
|
| 41 |
MAX_REQUEST_BODY_SIZE,
|
| 42 |
-
_get_image_compressor,
|
| 43 |
_read_request_json,
|
| 44 |
)
|
| 45 |
from headroom.tokenizers import get_tokenizer
|
|
@@ -103,18 +102,20 @@ class OpenAIHandlerMixin:
|
|
| 103 |
if _bypass:
|
| 104 |
logger.info(f"[{request_id}] Bypass: skipping compression (header)")
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
|
| 119 |
headers = dict(request.headers.items())
|
| 120 |
headers.pop("host", None)
|
|
|
|
| 39 |
COMPRESSION_TIMEOUT_SECONDS,
|
| 40 |
MAX_MESSAGE_ARRAY_LENGTH,
|
| 41 |
MAX_REQUEST_BODY_SIZE,
|
|
|
|
| 42 |
_read_request_json,
|
| 43 |
)
|
| 44 |
from headroom.tokenizers import get_tokenizer
|
|
|
|
| 102 |
if _bypass:
|
| 103 |
logger.info(f"[{request_id}] Bypass: skipping compression (header)")
|
| 104 |
|
| 105 |
+
# TODO: Re-enable image compression once token counting is accurate.
|
| 106 |
+
# See anthropic.py handler for details on why this is disabled.
|
| 107 |
+
#
|
| 108 |
+
# if self.config.image_optimize and messages and not _bypass:
|
| 109 |
+
# compressor = _get_image_compressor()
|
| 110 |
+
# if compressor and compressor.has_images(messages):
|
| 111 |
+
# messages = compressor.compress(messages, provider="openai")
|
| 112 |
+
# if compressor.last_result:
|
| 113 |
+
# logger.info(
|
| 114 |
+
# f"Image compression: {compressor.last_result.technique.value} "
|
| 115 |
+
# f"({compressor.last_result.savings_percent:.0f}% saved, "
|
| 116 |
+
# f"{compressor.last_result.original_tokens} -> "
|
| 117 |
+
# f"{compressor.last_result.compressed_tokens} tokens)"
|
| 118 |
+
# )
|
| 119 |
|
| 120 |
headers = dict(request.headers.items())
|
| 121 |
headers.pop("host", None)
|