This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
🐛 Fix bug that arises when using negative max_tokens #937
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
platform/reworkd_platform/services/tokenizer/token_service.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from tiktoken import Encoding | ||
|
|
||
| from reworkd_platform.schemas import LLM_MODEL_MAX_TOKENS | ||
| from reworkd_platform.web.api.agent.model_settings import WrappedChatOpenAI | ||
|
|
||
|
|
||
| class TokenService: | ||
| def __init__(self, encoding: Encoding): | ||
| self.encoding = encoding | ||
|
|
||
| def tokenize(self, text: str) -> list[int]: | ||
| return self.encoding.encode(text) | ||
|
|
||
| def detokenize(self, tokens: list[int]) -> str: | ||
| return self.encoding.decode(tokens) | ||
|
|
||
| def count(self, text: str) -> int: | ||
| return len(self.tokenize(text)) | ||
|
|
||
| def calculate_max_tokens(self, model: WrappedChatOpenAI, *prompts: str) -> None: | ||
| max_allowed_tokens = LLM_MODEL_MAX_TOKENS.get(model.model_name, 4000) | ||
| prompt_tokens = sum([self.count(p) for p in prompts]) | ||
| requested_tokens = max_allowed_tokens - prompt_tokens | ||
|
|
||
| model.max_tokens = min(model.max_tokens, requested_tokens) | ||
| model.max_tokens = max(model.max_tokens, 1) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,105 @@ | ||
| from unittest.mock import Mock | ||
|
|
||
| import tiktoken | ||
|
|
||
| from reworkd_platform.services.tokenizer.service import TokenService | ||
| from reworkd_platform.schemas import LLM_MODEL_MAX_TOKENS | ||
| from reworkd_platform.services.tokenizer.token_service import TokenService | ||
|
|
||
| encoding = tiktoken.get_encoding("cl100k_base") | ||
|
|
||
|
|
||
| def test_happy_path(): | ||
| def test_happy_path() -> None: | ||
| service = TokenService(encoding) | ||
| text = "Hello world!" | ||
|
|
||
| validate_tokenize_and_detokenize(service, text, 3) | ||
|
|
||
|
|
||
| def test_nothing(): | ||
| def test_nothing() -> None: | ||
| service = TokenService(encoding) | ||
| text = "" | ||
|
|
||
| validate_tokenize_and_detokenize(service, text, 0) | ||
|
|
||
|
|
||
| def validate_tokenize_and_detokenize(service, text, expected_token_count): | ||
| def validate_tokenize_and_detokenize( | ||
| service: TokenService, text: str, expected_token_count: int | ||
| ) -> None: | ||
| tokens = service.tokenize(text) | ||
| assert text == service.detokenize(tokens) | ||
| assert len(tokens) == service.count(text) | ||
| assert len(tokens) == expected_token_count | ||
|
|
||
|
|
||
| def test_calculate_max_tokens_with_small_max_tokens() -> None: | ||
| initial_max_tokens = 3000 | ||
| service = TokenService(encoding) | ||
| model = Mock(spec=["model_name", "max_tokens"]) | ||
| model.model_name = "gpt-3.5-turbo" | ||
| model.max_tokens = initial_max_tokens | ||
|
|
||
| service.calculate_max_tokens(model, "Hello") | ||
|
|
||
| assert model.max_tokens == initial_max_tokens | ||
|
|
||
|
|
||
| def test_calculate_max_tokens_with_high_completion_tokens() -> None: | ||
| service = TokenService(encoding) | ||
| prompt_tokens = service.count(LONG_TEXT) | ||
| model = Mock(spec=["model_name", "max_tokens"]) | ||
| model.model_name = "gpt-3.5-turbo" | ||
| model.max_tokens = 8000 | ||
|
|
||
| service.calculate_max_tokens(model, LONG_TEXT) | ||
|
|
||
| assert model.max_tokens == ( | ||
| LLM_MODEL_MAX_TOKENS.get("gpt-3.5-turbo") - prompt_tokens | ||
| ) | ||
|
|
||
|
|
||
| def test_calculate_max_tokens_with_negative_result() -> None: | ||
| service = TokenService(encoding) | ||
| model = Mock(spec=["model_name", "max_tokens"]) | ||
| model.model_name = "gpt-3.5-turbo" | ||
| model.max_tokens = 8000 | ||
|
|
||
| service.calculate_max_tokens(model, *([LONG_TEXT] * 100)) | ||
|
|
||
| # We use the minimum length of 1 | ||
| assert model.max_tokens == 1 | ||
|
|
||
|
|
||
| LONG_TEXT = """ | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| This is some long text. This is some long text. This is some long text. | ||
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!