Skip to content

fix(generative-ai): Update count tokens sample #11713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion generative_ai/embedding_model_tuning_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def test_tune_embedding_model() -> None:
credentials=credentials,
)
tuning_job = embedding_model_tuning.tune_embedding_model(
aiplatform_init.global_config.api_endpoint)
aiplatform_init.global_config.api_endpoint
)
try:
assert tuning_job._status.name != "PIPELINE_STATE_FAILED"
finally:
Expand Down
63 changes: 58 additions & 5 deletions generative_ai/gemini_count_token_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,74 @@
# limitations under the License.


def count_tokens(project_id: str) -> str:
from vertexai.generative_models import GenerationResponse


def count_tokens(project_id: str) -> GenerationResponse:
# [START generativeaionvertexai_gemini_token_count]
import vertexai

from vertexai.generative_models import GenerativeModel

# TODO(developer): Update and un-comment below line
# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"

vertexai.init(project=project_id, location="us-central1")

model = GenerativeModel(model_name="gemini-1.0-pro-002")

response = model.count_tokens("Why is sky blue?")
print(response)
prompt = "Why is the sky blue?"

# Prompt tokens count
response = model.count_tokens(prompt)
usage_metadata = response.usage_metadata
print(f"Prompt Token Count: {usage_metadata.prompt_token_count}")

# Send text to Gemini
response = model.generate_content(prompt)

# Response tokens count
usage_metadata = response.usage_metadata
print(f"Prompt Token Count: {usage_metadata.prompt_token_count}")
print(f"Candidates Token Count: {usage_metadata.candidates_token_count}")
print(f"Total Token Count: {usage_metadata.total_token_count}")

# [END generativeaionvertexai_gemini_token_count]
return response


def count_tokens_multimodal(project_id: str) -> GenerationResponse:
# [START generativeaionvertexai_gemini_token_count_multimodal]
import vertexai
from vertexai.generative_models import GenerativeModel, Part

# TODO(developer): Update and un-comment below lines
# project_id = "PROJECT_ID"

vertexai.init(project=project_id, location="us-central1")

model = GenerativeModel(model_name="gemini-1.0-pro-002")

contents = [
Part.from_uri(
"gs://cloud-samples-data/generative-ai/video/pixel8.mp4",
mime_type="video/mp4",
),
"Provide a description of the video.",
]

# Prompt tokens count
response = model.count_tokens(contents)
usage_metadata = response.usage_metadata
print(f"Prompt Token Count: {usage_metadata.prompt_token_count}")

# Send text to Gemini
response = model.generate_content(contents)
usage_metadata = response.usage_metadata

# Response tokens count
print(f"Prompt Token Count: {usage_metadata.prompt_token_count}")
print(f"Candidates Token Count: {usage_metadata.candidates_token_count}")
print(f"Total Token Count: {usage_metadata.total_token_count}")

# [END generativeaionvertexai_gemini_token_count_multimodal]
return response
5 changes: 5 additions & 0 deletions generative_ai/test_gemini_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def test_gemini_multi_image_example() -> None:
def test_gemini_count_token_example() -> None:
response = gemini_count_token_example.count_tokens(PROJECT_ID)
assert response
assert response.usage_metadata

response = gemini_count_token_example.count_tokens_multimodal(PROJECT_ID)
assert response
assert response.usage_metadata


def test_gemini_safety_config_example() -> None:
Expand Down
Loading