Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Update count_tokens.py#459

Merged
logankilpatrick merged 1 commit intogoogle-gemini:mainfrom
rachelsaunders:patch-1
Jul 12, 2024
Merged

Update count_tokens.py#459
logankilpatrick merged 1 commit intogoogle-gemini:mainfrom
rachelsaunders:patch-1

Conversation

@rachelsaunders
Copy link
Contributor

Description of the change

  • integrated returns into main snippets
  • updated code comments for clarity and completeness
  • pulled text of prompts out of the requests to generate_content

Motivation

Alignment and clarification of docs snippets

Type of change

Documentation - code snippets

Checklist

  • I have performed a self-review of my code.
  • I have added detailed comments to my code where applicable.
  • I have verified that my change does not break existing code.
  • My PR is based on the latest changes of the main branch (if unsure, please run git pull --rebase upstream main).
  • I am familiar with the Google Style Guide for the language I have coded in.
  • I have read through the Contributing Guide and signed the Contributor License Agreement.
- integrated returns into main snippet
- updated code comments
- pulled text of prompts out of the requests to generate_content
@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:python sdk Issue/PR related to Python SDK labels Jul 12, 2024
@logankilpatrick logankilpatrick merged commit 4e1dcd6 into google-gemini:main Jul 12, 2024
@github-actions github-actions bot removed the status:awaiting review PR awaiting review from a maintainer label Jul 12, 2024
Copy link

@eliben eliben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive-by review


# Returns the "context window" for the model,
# which is the combined input and output token limits.
print(f"{model_info.input_token_limit=}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the output is printed as key=value, whereas in most other places it's key: value.
Is there some system/method behind this?

# (`prompt_token_count` and `candidates_token_count`, respectively),
# as well as the combined token count (`total_token_count`).
print(response.usage_metadata)
# ( prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the token count differs between the two approaches: 10 vs 11

# (`prompt_token_count` and `candidates_token_count`, respectively),
# as well as the combined token count (`total_token_count`).
print(response.usage_metadata)
# ( prompt_token_count: 264, candidates_token_count: 80, total_token_count: 345 )
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some off-by-1 issues here: prompt token count 263 vs 264
Also 264+80 != 345

# Optionally, you can call `count_tokens` for the prompt and file separately.
prompt = "Please give a short summary of this file."

# Call `count_tokens` to get input token count
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample uploads a text file, not a video - should the comment be amended?

eliben added a commit to google/generative-ai-go that referenced this pull request Jul 12, 2024
Aligning with the Python samples per
google-gemini/deprecated-generative-ai-python#459

Main change is in the way output of snippets is marked
TechRanger101 added a commit to TechRanger101/Generative-AI-GoLang that referenced this pull request Nov 27, 2024
Aligning with the Python samples per
google-gemini/deprecated-generative-ai-python#459

Main change is in the way output of snippets is marked
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

component:python sdk Issue/PR related to Python SDK

3 participants