Skip to content

Commit a34f85b

Browse files
Thoughtseize1riathakkar
authored andcommitted
refactor: (GenAI) Reorganized Token Count Samples (Group C) (GoogleCloudPlatform#12616)
* Created new Token Count folder and new Sample with test. * Rename file to resolve conflicts * Changed model
1 parent b51ea18 commit a34f85b

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright 2024 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+
import os
15+
16+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
17+
18+
19+
def count_token_service() -> int:
20+
# [START generativeaionvertexai_token_count_sample_with_genai]
21+
import vertexai
22+
from vertexai.generative_models import GenerativeModel
23+
24+
# TODO(developer): Update and un-comment below line
25+
# PROJECT_ID = "your-project-id"
26+
vertexai.init(project=PROJECT_ID, location="us-central1")
27+
28+
# using Vertex AI Model as tokenzier
29+
model = GenerativeModel("gemini-1.5-flash")
30+
31+
prompt = "hello world"
32+
response = model.count_tokens(prompt)
33+
print(f"Prompt Token Count: {response.total_tokens}")
34+
print(f"Prompt Character Count: {response.total_billable_characters}")
35+
36+
prompt = ["hello world", "what's the weather today"]
37+
response = model.count_tokens(prompt)
38+
print(f"Prompt Token Count: {response.total_tokens}")
39+
print(f"Prompt Character Count: {response.total_billable_characters}")
40+
# Example response:
41+
# Prompt Token Count: 2
42+
# Prompt Character Count: 10
43+
# Prompt Token Count: 8
44+
# Prompt Character Count: 31
45+
46+
# [END generativeaionvertexai_token_count_sample_with_genai]
47+
return response.total_tokens
48+
49+
50+
if __name__ == "__main__":
51+
count_token_service()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2024 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_token_locally() -> int:
17+
# [START generativeaionvertexai_token_count_sample_with_local_sdk]
18+
from vertexai.preview.tokenization import get_tokenizer_for_model
19+
20+
# Using local tokenzier
21+
tokenizer = get_tokenizer_for_model("gemini-1.5-flash")
22+
23+
prompt = "hello world"
24+
response = tokenizer.count_tokens(prompt)
25+
print(f"Prompt Token Count: {response.total_tokens}")
26+
27+
prompt = ["hello world", "what's the weather today"]
28+
response = tokenizer.count_tokens(prompt)
29+
print(f"Prompt Token Count: {response.total_tokens}")
30+
# Example response:
31+
# Prompt Token Count: 2
32+
# Prompt Token Count: 8
33+
34+
# [END generativeaionvertexai_token_count_sample_with_local_sdk]
35+
return response.total_tokens
36+
37+
38+
if __name__ == "__main__":
39+
count_token_locally()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2024 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+
import os
15+
16+
from vertexai.generative_models import GenerationResponse
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def count_tokens_multimodal() -> GenerationResponse:
22+
# [START generativeaionvertexai_gemini_token_count_multimodal]
23+
import vertexai
24+
from vertexai.generative_models import GenerativeModel, Part
25+
26+
# TODO(developer): Update and un-comment below line
27+
# PROJECT_ID = "your-project-id"
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
model = GenerativeModel("gemini-1.5-flash-002")
31+
32+
contents = [
33+
Part.from_uri(
34+
"gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
35+
mime_type="video/mp4",
36+
),
37+
"Provide a description of the video.",
38+
]
39+
40+
# Prompt tokens count
41+
response = model.count_tokens(contents)
42+
print(f"Prompt Token Count: {response.total_tokens}")
43+
print(f"Prompt Character Count: {response.total_billable_characters}")
44+
45+
# Send text to Gemini
46+
response = model.generate_content(contents)
47+
usage_metadata = response.usage_metadata
48+
49+
# Response tokens count
50+
print(f"Prompt Token Count: {usage_metadata.prompt_token_count}")
51+
print(f"Candidates Token Count: {usage_metadata.candidates_token_count}")
52+
print(f"Total Token Count: {usage_metadata.total_token_count}")
53+
# Example response:
54+
# Prompt Token Count: 16822
55+
# Prompt Character Count: 30
56+
# Prompt Token Count: 16822
57+
# Candidates Token Count: 71
58+
# Total Token Count: 16893
59+
60+
# [END generativeaionvertexai_gemini_token_count_multimodal]
61+
return response
62+
63+
64+
if __name__ == "__main__":
65+
count_tokens_multimodal()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2024 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+
import os
15+
16+
from vertexai.generative_models import GenerationResponse
17+
18+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
19+
20+
21+
def count_tokens() -> GenerationResponse:
22+
# [START generativeaionvertexai_gemini_token_count]
23+
import vertexai
24+
from vertexai.generative_models import GenerativeModel
25+
26+
# TODO(developer): Update and un-comment below line
27+
# PROJECT_ID = "your-project-id"
28+
vertexai.init(project=PROJECT_ID, location="us-central1")
29+
30+
model = GenerativeModel("gemini-1.5-flash-002")
31+
32+
prompt = "Why is the sky blue?"
33+
# Prompt tokens count
34+
response = model.count_tokens(prompt)
35+
print(f"Prompt Token Count: {response.total_tokens}")
36+
print(f"Prompt Character Count: {response.total_billable_characters}")
37+
38+
# Send text to Gemini
39+
response = model.generate_content(prompt)
40+
41+
# Response tokens count
42+
usage_metadata = response.usage_metadata
43+
print(f"Prompt Token Count: {usage_metadata.prompt_token_count}")
44+
print(f"Candidates Token Count: {usage_metadata.candidates_token_count}")
45+
print(f"Total Token Count: {usage_metadata.total_token_count}")
46+
# Example response:
47+
# Prompt Token Count: 6
48+
# Prompt Character Count: 16
49+
# Prompt Token Count: 6
50+
# Candidates Token Count: 315
51+
# Total Token Count: 321
52+
53+
# [END generativeaionvertexai_gemini_token_count]
54+
return response
55+
56+
57+
if __name__ == "__main__":
58+
count_tokens()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2024 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+
import api_example
16+
import local_sdk_example
17+
import multimodal_token_count_example
18+
import simple_example
19+
20+
21+
def test_count_token() -> None:
22+
assert local_sdk_example.count_token_locally()
23+
assert api_example.count_token_service()
24+
25+
26+
def test_gemini_count_token_example() -> None:
27+
response = simple_example.count_tokens()
28+
assert response
29+
assert response.usage_metadata
30+
31+
response = multimodal_token_count_example.count_tokens_multimodal()
32+
assert response
33+
assert response.usage_metadata

0 commit comments

Comments
 (0)