Skip to content

Commit

Permalink
Bump the dependencies group across 1 directory with 12 updates (#1331)
Browse files Browse the repository at this point in the history
Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Collin Dutter <[email protected]>
  • Loading branch information
dependabot[bot] and collindutter authored Nov 11, 2024
1 parent c720288 commit dcbde3c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 50 deletions.
15 changes: 13 additions & 2 deletions griptape/tokenizers/anthropic_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

if TYPE_CHECKING:
from anthropic import Anthropic
from anthropic.types.beta import BetaMessageParam


@define()
Expand All @@ -21,5 +22,15 @@ class AnthropicTokenizer(BaseTokenizer):
kw_only=True,
)

def count_tokens(self, text: str) -> int:
return self.client.count_tokens(text)
def count_tokens(self, text: str | list[BetaMessageParam]) -> int:
types = import_optional_dependency("anthropic.types.beta")

# TODO: Refactor all Tokenizers to support Prompt Stack as an input.
messages = [types.BetaMessageParam(role="user", content=text)] if isinstance(text, str) else text

usage = self.client.beta.messages.count_tokens(
model=self.model,
messages=messages,
)

return usage.input_tokens
93 changes: 46 additions & 47 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ filetype = "^1.2"

# drivers
cohere = { version = "^5.11.2", optional = true }
anthropic = { version = "^0.37.1", optional = true }
anthropic = { version = "^0.39.0", optional = true }
transformers = { version = "^4.41.1", optional = true}
huggingface-hub = { version = "^0.26.2", optional = true }
boto3 = { version = "^1.34.119", optional = true }
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/tokenizers/test_anthropic_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import Mock

import pytest

from griptape.tokenizers import AnthropicTokenizer
Expand All @@ -8,6 +10,16 @@ class TestAnthropicTokenizer:
def tokenizer(self, request):
return AnthropicTokenizer(model=request.param)

@pytest.fixture(autouse=True)
def mock_client(self, mocker):
mock_client = mocker.patch("anthropic.Anthropic")

mock_client.return_value = Mock(
beta=Mock(messages=Mock(count_tokens=Mock(return_value=Mock(input_tokens=5, output_tokens=10))))
)

return mock_client

@pytest.mark.parametrize(
("tokenizer", "expected"),
[("claude-2.1", 5), ("claude-2.0", 5), ("claude-3-opus", 5), ("claude-3-sonnet", 5), ("claude-3-haiku", 5)],
Expand Down

0 comments on commit dcbde3c

Please sign in to comment.