|  | 
| 4 | 4 | 
 | 
| 5 | 5 | import litellm | 
| 6 | 6 | import pytest | 
|  | 7 | +import tiktoken | 
| 7 | 8 | from litellm.caching import Cache, InMemoryCache | 
| 8 | 9 | from pytest_subtests import SubTests | 
| 9 | 10 | 
 | 
|  | 
| 20 | 21 | from lmi.utils import VCR_DEFAULT_MATCH_ON, encode_image_as_url | 
| 21 | 22 | 
 | 
| 22 | 23 | 
 | 
| 23 |  | -def test_estimate_tokens(stub_png_image: bytes) -> None: | 
| 24 |  | -    # Test text-only | 
| 25 |  | -    text_only = "Hello world" | 
| 26 |  | -    assert estimate_tokens(text_only) == 2.75 | 
|  | 24 | +def test_estimate_tokens(subtests: SubTests, stub_png_image: bytes) -> None: | 
|  | 25 | +    with subtests.test(msg="text only"): | 
|  | 26 | +        text_only = "Hello world" | 
|  | 27 | +        text_only_estimated_token_count = estimate_tokens(text_only) | 
|  | 28 | +        assert text_only_estimated_token_count == 2.75, ( | 
|  | 29 | +            "Expected a reasonable token estimate" | 
|  | 30 | +        ) | 
|  | 31 | +        text_only_actual_token_count = len( | 
|  | 32 | +            tiktoken.get_encoding("cl100k_base").encode(text_only) | 
|  | 33 | +        ) | 
|  | 34 | +        assert text_only_estimated_token_count == pytest.approx( | 
|  | 35 | +            text_only_actual_token_count, abs=1 | 
|  | 36 | +        ), "Estimation should be within one token of what tiktoken" | 
| 27 | 37 | 
 | 
| 28 | 38 |     # Test multimodal (text + image) | 
| 29 |  | -    multimodal = [ | 
| 30 |  | -        "What is in this image?", | 
| 31 |  | -        encode_image_as_url(image_type="png", image_data=stub_png_image), | 
| 32 |  | -    ] | 
| 33 |  | -    assert estimate_tokens(multimodal) == 90.5 | 
|  | 39 | +    with subtests.test(msg="multimodal"):  # Text + image | 
|  | 40 | +        multimodal = [ | 
|  | 41 | +            "What is in this image?", | 
|  | 42 | +            encode_image_as_url(image_type="png", image_data=stub_png_image), | 
|  | 43 | +        ] | 
|  | 44 | +        assert estimate_tokens(multimodal) == 90.5, ( | 
|  | 45 | +            "Expected a reasonable token estimate" | 
|  | 46 | +        ) | 
| 34 | 47 | 
 | 
| 35 | 48 | 
 | 
| 36 | 49 | class TestLiteLLMEmbeddingModel: | 
|  | 
0 commit comments