|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def counttoken_localtokenizer_compute_with_txt() -> int: |
| 17 | + # [START googlegenaisdk_counttoken_localtokenizer_compute_with_txt] |
| 18 | + from google.genai.local_tokenizer import LocalTokenizer |
| 19 | + |
| 20 | + tokenizer = LocalTokenizer(model_name="gemini-2.5-flash") |
| 21 | + response = tokenizer.compute_tokens("What's the longest word in the English language?") |
| 22 | + print(response) |
| 23 | + # Example output: |
| 24 | + # tokens_info=[TokensInfo( |
| 25 | + # role='user', |
| 26 | + # token_ids=[3689, 236789, 236751, 506, |
| 27 | + # 27801, 3658, 528, 506, 5422, 5192, 236881], |
| 28 | + # tokens=[b'What', b"'", b's', b' the', b' longest', |
| 29 | + # b' word', b' in', b' the', b' English', b' language', b'?'] |
| 30 | + # )] |
| 31 | + # [END googlegenaisdk_counttoken_localtokenizer_compute_with_txt] |
| 32 | + return response.tokens_info |
| 33 | + |
| 34 | + |
| 35 | +if __name__ == "__main__": |
| 36 | + counttoken_localtokenizer_compute_with_txt() |
0 commit comments