Skip to content

Commit a14bb72

Browse files
committed
Add Count Tokens Samples
1 parent 3a7cabf commit a14bb72

File tree

6 files changed

+182
-67
lines changed

6 files changed

+182
-67
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 compute_tokens() -> int:
17+
# [START googlegenaisdk_count_tokens_compute_with_txt]
18+
from google import genai
19+
20+
client = genai.Client()
21+
response = client.models.compute_tokens(
22+
model="gemini-2.0-flash-001",
23+
contents="What's the longest word in the English language?",
24+
)
25+
print(response)
26+
27+
# Example output:
28+
# tokens_info=[TokensInfo(role='user', token_ids=[1841, 235303, 235256, 573, 32514, 2204, 575, 573, 4645, 5255, 235336], tokens=[b'What', b"'", b's', b' the', b' longest', b' word', b' in', b' the', b' English', b' language', b'?'])]
29+
30+
# [END googlegenaisdk_count_tokens_compute_with_txt]
31+
return response.tokens_info
32+
33+
34+
if __name__ == "__main__":
35+
compute_tokens()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 generate_content() -> int:
17+
# [START googlegenaisdk_count_tokens_resp_with_txt]
18+
from google import genai
19+
20+
client = genai.Client()
21+
22+
prompt = "Why is the sky blue?"
23+
24+
# Send text to Gemini
25+
response = client.models.generate_content(
26+
model="gemini-2.0-flash-001", contents=prompt
27+
)
28+
29+
# Prompt and response tokens count
30+
print(response.usage_metadata)
31+
32+
# Example output:
33+
# cached_content_token_count=None candidates_token_count=311 prompt_token_count=6 total_token_count=317
34+
35+
# [END googlegenaisdk_count_tokens_resp_with_txt]
36+
return response.usage_metadata
37+
38+
39+
if __name__ == "__main__":
40+
generate_content()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 count_tokens() -> int:
17+
# [START googlegenaisdk_count_tokens_with_txt]
18+
from google import genai
19+
20+
client = genai.Client()
21+
response = client.models.count_tokens(
22+
model="gemini-2.0-flash-001",
23+
contents="What's the highest mountain in Africa?",
24+
)
25+
print(response)
26+
27+
# Example output:
28+
# total_tokens=10 cached_content_token_count=None
29+
30+
# [END googlegenaisdk_count_tokens_with_txt]
31+
return response.total_tokens
32+
33+
34+
if __name__ == "__main__":
35+
count_tokens()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 count_tokens() -> int:
17+
# [START googlegenaisdk_count_tokens_with_txt_img_vid]
18+
from google import genai
19+
from google.genai.types import Part
20+
21+
client = genai.Client()
22+
23+
contents = [
24+
Part.from_uri(
25+
file_uri="gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
26+
mime_type="video/mp4",
27+
),
28+
"Provide a description of the video.",
29+
]
30+
31+
response = client.models.count_tokens(
32+
model="gemini-2.0-flash-001",
33+
contents=contents,
34+
)
35+
print(response)
36+
37+
# Example output:
38+
# total_tokens=10 cached_content_token_count=None
39+
40+
# [END googlegenaisdk_count_tokens_with_txt_img_vid]
41+
return response.total_tokens
42+
43+
44+
if __name__ == "__main__":
45+
count_tokens()

genai/count_tokens/counttoken_with_txt.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

genai/count_tokens/test_count_tokens_samples.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,34 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import advanced_example
16-
import simple_example
15+
#
16+
# Using Google Cloud Vertex AI to test the code samples.
17+
#
18+
19+
import os
20+
21+
import count_tokens_compute_with_txt
22+
import count_tokens_resp_with_txt
23+
import count_tokens_with_txt
24+
import count_tokens_with_txt_img_vid
25+
26+
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
27+
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
28+
# The project name is included in the CICD pipeline
29+
# os.environ['GOOGLE_CLOUD_PROJECT'] = "add-your-project-name"
30+
31+
32+
def test_count_tokens_compute_with_txt() -> None:
33+
assert count_tokens_compute_with_txt.compute_tokens()
34+
35+
36+
def test_count_tokens_resp_with_txt() -> None:
37+
assert count_tokens_resp_with_txt.generate_content()
1738

1839

19-
def test_simple_example() -> None:
20-
response = simple_example.simple_example()
21-
assert response
40+
def test_count_tokens_with_txt() -> None:
41+
assert count_tokens_with_txt.count_tokens()
2242

2343

24-
def test_advanced_example() -> None:
25-
response = advanced_example.advanced_example()
26-
assert response
44+
def test_count_tokens_with_txt_img_vid() -> None:
45+
assert count_tokens_with_txt_img_vid.count_tokens()

0 commit comments

Comments
 (0)