Skip to content

Commit a4e501e

Browse files
authored
Sync count tokens examples (#445)
* Sync count tokens examples Change-Id: Idd9cd0956ad9b2fa7d95d8ab792b1673dc1e88a8 * format Change-Id: I7e7ecd1b4c0e060ef91bad0f5083616f0394705a * Fix video file name. Change-Id: Id1ff1196fa8072cc90d1a3921846687687b180b4
1 parent b733acb commit a4e501e

File tree

1 file changed

+199
-21
lines changed

1 file changed

+199
-21
lines changed

Diff for: samples/count_tokens.py

+199-21
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,247 @@
2121

2222

2323
class UnitTests(absltest.TestCase):
24+
def test_tokens_context_window(self):
25+
# [START tokens_context_window]
26+
model_info = genai.get_model("models/gemini-1.0-pro-001")
27+
# Returns the "context window" for the model (the combined input and output token limits)
28+
print(f"{model_info.input_token_limit=}")
29+
print(f"{model_info.output_token_limit=}")
30+
# [END tokens_context_window]
31+
32+
# [START tokens_context_window_return]
33+
# input_token_limit=30720
34+
# output_token_limit=2048
35+
# [END tokens_context_window_return]
36+
2437
def test_tokens_text_only(self):
2538
# [START tokens_text_only]
2639
model = genai.GenerativeModel("models/gemini-1.5-flash")
27-
print(model.count_tokens("The quick brown fox jumps over the lazy dog."))
40+
41+
prompt = "The quick brown fox jumps over the lazy dog."
42+
43+
# Call `count_tokens` to get the input token count (`total_tokens`).
44+
print("total_tokens: ", model.count_tokens(prompt))
45+
46+
response = model.generate_content(prompt)
47+
48+
# Use `usage_metadata` to get both input and output token counts
49+
# (`prompt_token_count` and `candidates_token_count`, respectively).
50+
print(response.usage_metadata)
2851
# [END tokens_text_only]
2952

53+
# [START tokens_text_only_return]
54+
# total_tokens: total_tokens: 10
55+
#
56+
# prompt_token_count: 11
57+
# candidates_token_count: 73
58+
# total_token_count: 84
59+
# [END tokens_text_only_return]
60+
3061
def test_tokens_chat(self):
3162
# [START tokens_chat]
3263
model = genai.GenerativeModel("models/gemini-1.5-flash")
64+
3365
chat = model.start_chat(
3466
history=[
35-
{"role": "user", "parts": "Hi, my name is Bob."},
67+
{"role": "user", "parts": "Hi my name is Bob"},
3668
{"role": "model", "parts": "Hi Bob!"},
3769
]
3870
)
39-
model.count_tokens(chat.history)
71+
# Call `count_tokens` to get the input token count (`total_tokens`).
72+
print(model.count_tokens(chat.history))
73+
74+
response = chat.send_message(
75+
"In one sentence, explain how a computer works to a young child."
76+
)
77+
# Use `usage_metadata` to get both input and output token counts
78+
# (`prompt_token_count` and `candidates_token_count`, respectively).
79+
print(response.usage_metadata)
4080

81+
# TODO add comment...
4182
from google.generativeai.types.content_types import to_contents
4283

43-
model.count_tokens(chat.history + to_contents("What is the meaning of life?"))
84+
print(model.count_tokens(chat.history + to_contents("What is the meaning of life?")))
4485
# [END tokens_chat]
4586

87+
# [START tokens_chat_return]
88+
# total_tokens: 10
89+
#
90+
# prompt_token_count: 25
91+
# candidates_token_count: 21
92+
# total_token_count: 46
93+
#
94+
# total_tokens: 56
95+
# [END tokens_chat_return]
96+
4697
def test_tokens_multimodal_image_inline(self):
4798
# [START tokens_multimodal_image_inline]
99+
import PIL.Image
100+
48101
model = genai.GenerativeModel("models/gemini-1.5-flash")
49-
import PIL
50102

51-
organ = PIL.Image.open(media / "organ.jpg")
52-
print(model.count_tokens(["Tell me about this instrument", organ]))
103+
prompt = "Tell me about this image"
104+
your_image_file = PIL.Image.open("image.jpg")
105+
106+
# Call `count_tokens` to get input token count of the combined text and file (`total_tokens`).
107+
# An image's display size does not affect its token count.
108+
# Optionally, you can call `count_tokens` for the prompt and file separately.
109+
print(model.count_tokens([prompt, your_image_file]))
110+
111+
response = model.generate_content([prompt, your_image_file])
112+
# Use `usage_metadata` to get both input and output token counts
113+
# (`prompt_token_count` and `candidates_token_count`, respectively).
114+
print(response.usage_metadata)
53115
# [END tokens_multimodal_image_inline]
54116

117+
# [START tokens_multimodal_image_inline_return]
118+
# total_tokens: 263
119+
#
120+
# prompt_token_count: 264
121+
# candidates_token_count: 81
122+
# total_token_count: 345
123+
# [END tokens_multimodal_image_inline_return]
124+
55125
def test_tokens_multimodal_image_file_api(self):
56126
# [START tokens_multimodal_image_file_api]
57127
model = genai.GenerativeModel("models/gemini-1.5-flash")
58-
organ_upload = genai.upload_file(media / "organ.jpg")
59-
print(model.count_tokens(["Tell me about this instrument", organ_upload]))
128+
129+
prompt = "Tell me about this image"
130+
your_image_file = genai.upload_file(path="image.jpg")
131+
132+
# Call `count_tokens` to get input token count of the combined text and file (`total_tokens`).
133+
# An image's display size does not affect its token count.
134+
# Optionally, you can call `count_tokens` for the prompt and file separately.
135+
print(model.count_tokens([prompt, your_image_file]))
136+
137+
response = model.generate_content([prompt, your_image_file])
138+
response.text
139+
# Use `usage_metadata` to get both input and output token counts
140+
# (`prompt_token_count` and `candidates_token_count`, respectively).
141+
print(response.usage_metadata)
60142
# [END tokens_multimodal_image_file_api]
61143

144+
# [START tokens_multimodal_image_file_api_return]
145+
# total_tokens: 263
146+
#
147+
# prompt_token_count: 264
148+
# candidates_token_count: 80
149+
# total_token_count: 344
150+
# [END tokens_multimodal_image_file_api_return]
151+
62152
def test_tokens_multimodal_video_audio_file_api(self):
63153
# [START tokens_multimodal_video_audio_file_api]
154+
import time
155+
64156
model = genai.GenerativeModel("models/gemini-1.5-flash")
65-
audio_upload = genai.upload_file(media / "sample.mp3")
66-
print(model.count_tokens(audio_upload))
157+
158+
prompt = "Tell me about this video"
159+
your_file = genai.upload_file(path=media / "Big_Buck_Bunny.mp4")
160+
161+
# Videos need to be processed before you can use them.
162+
while your_file.state.name == "PROCESSING":
163+
print("processing video...")
164+
time.sleep(5)
165+
your_file = genai.get_file(your_file.name)
166+
167+
# Call `count_tokens` to get input token count of the combined text and file (`total_tokens`).
168+
# A video or audio file is converted to tokens at a fixed rate of tokens per second.
169+
# Optionally, you can call `count_tokens` for the prompt and file separately.
170+
print(model.count_tokens([prompt, your_file]))
171+
172+
response = model.generate_content([prompt, your_file])
173+
174+
# Use `usage_metadata` to get both input and output token counts
175+
# (`prompt_token_count` and `candidates_token_count`, respectively).
176+
print(response.usage_metadata)
177+
67178
# [END tokens_multimodal_video_audio_file_api]
68179

180+
# [START tokens_multimodal_video_audio_file_api_return]
181+
# processing video...
182+
# total_tokens: 300
183+
#
184+
# prompt_token_count: 301
185+
# candidates_token_count: 60
186+
# total_token_count: 361
187+
# [END tokens_multimodal_video_audio_file_api_return]
188+
69189
def test_tokens_cached_content(self):
70190
# [START tokens_cached_content]
71-
document = genai.upload_file(path=media / "a11.txt")
72-
model_name = "gemini-1.5-flash-001"
191+
import time
192+
193+
model = genai.GenerativeModel("models/gemini-1.5-flash")
194+
195+
your_file = genai.upload_file(path=media / "a11.txt")
196+
73197
cache = genai.caching.CachedContent.create(
74-
model=model_name,
75-
contents=[document],
198+
model="models/gemini-1.5-flash-001",
199+
# You could set the system_instruction and tools
200+
system_instruction=None,
201+
tools=None,
202+
contents=["Here the Apollo 11 transcript:", your_file],
76203
)
77-
print(genai.GenerativeModel().count_tokens(cache))
204+
205+
model = genai.GenerativeModel.from_cached_content(cache)
206+
207+
# Call `count_tokens` to get input token count of the combined text and file (`total_tokens`).
208+
# A video or audio file is converted to tokens at a fixed rate of tokens per second.
209+
# Optionally, you can call `count_tokens` for the prompt and file separately.
210+
prompt = "Please give a short summary of this file."
211+
print(model.count_tokens(prompt))
212+
213+
response = model.generate_content(prompt)
214+
# Use `usage_metadata` to get both input and output token counts
215+
# (`prompt_token_count` and `candidates_token_count`, respectively).
216+
print(response.usage_metadata)
217+
218+
cache.delete()
78219
# [END tokens_cached_content]
79-
cache.delete() # Clear
220+
221+
# [START tokens_cached_content_return]
222+
# total_tokens: 9
223+
#
224+
# prompt_token_count: 323393
225+
# cached_content_token_count: 323383
226+
# candidates_token_count: 64
227+
# total_token_count: 323457
228+
# [END tokens_cached_content_return]
80229

81230
def test_tokens_system_instruction(self):
82231
# [START tokens_system_instruction]
83-
document = genai.upload_file(path=media / "a11.txt")
232+
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
233+
234+
# The total token count includes everything sent to the generate_content request.
235+
print(model.count_tokens("The quick brown fox jumps over the lazy dog."))
236+
# total_tokens: 10
237+
84238
model = genai.GenerativeModel(
85-
"models/gemini-1.5-flash-001",
86-
system_instruction="You are an expert analyzing transcripts. Give a summary of this document.",
239+
model_name="gemini-1.5-flash", system_instruction="You are a cat. Your name is Neko."
87240
)
88-
print(model.count_tokens(document))
241+
242+
# The total token count includes everything sent to the generate_content request.
243+
# When you use system instructions, the total token count increases.
244+
print(model.count_tokens("The quick brown fox jumps over the lazy dog."))
89245
# [END tokens_system_instruction]
90246

247+
# [START tokens_system_instruction_return]
248+
# total_tokens: 10
249+
#
250+
# total_tokens: 21
251+
# [END tokens_system_instruction_return]
252+
91253
def test_tokens_tools(self):
92254
# [START tokens_tools]
255+
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
256+
257+
# The total token count includes everything sent to the generate_content request.
258+
print(
259+
model.count_tokens(
260+
"I have 57 cats, each owns 44 mittens, how many mittens is that in total?"
261+
)
262+
)
263+
# total_tokens: 10
264+
93265
def add(a: float, b: float):
94266
"""returns a + b."""
95267
return a + b
@@ -117,6 +289,12 @@ def divide(a: float, b: float):
117289
)
118290
# [END tokens_tools]
119291

292+
# [START tokens_tools_return]
293+
# total_tokens: 22
294+
#
295+
# total_tokens: 206
296+
# [END tokens_tools_return]
297+
120298

121299
if __name__ == "__main__":
122300
absltest.main()

0 commit comments

Comments
 (0)