|
21 | 21 |
|
22 | 22 |
|
23 | 23 | 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 | + |
24 | 37 | def test_tokens_text_only(self):
|
25 | 38 | # [START tokens_text_only]
|
26 | 39 | 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) |
28 | 51 | # [END tokens_text_only]
|
29 | 52 |
|
| 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 | + |
30 | 61 | def test_tokens_chat(self):
|
31 | 62 | # [START tokens_chat]
|
32 | 63 | model = genai.GenerativeModel("models/gemini-1.5-flash")
|
| 64 | + |
33 | 65 | chat = model.start_chat(
|
34 | 66 | history=[
|
35 |
| - {"role": "user", "parts": "Hi, my name is Bob."}, |
| 67 | + {"role": "user", "parts": "Hi my name is Bob"}, |
36 | 68 | {"role": "model", "parts": "Hi Bob!"},
|
37 | 69 | ]
|
38 | 70 | )
|
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) |
40 | 80 |
|
| 81 | + # TODO add comment... |
41 | 82 | from google.generativeai.types.content_types import to_contents
|
42 | 83 |
|
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?"))) |
44 | 85 | # [END tokens_chat]
|
45 | 86 |
|
| 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 | + |
46 | 97 | def test_tokens_multimodal_image_inline(self):
|
47 | 98 | # [START tokens_multimodal_image_inline]
|
| 99 | + import PIL.Image |
| 100 | + |
48 | 101 | model = genai.GenerativeModel("models/gemini-1.5-flash")
|
49 |
| - import PIL |
50 | 102 |
|
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) |
53 | 115 | # [END tokens_multimodal_image_inline]
|
54 | 116 |
|
| 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 | + |
55 | 125 | def test_tokens_multimodal_image_file_api(self):
|
56 | 126 | # [START tokens_multimodal_image_file_api]
|
57 | 127 | 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) |
60 | 142 | # [END tokens_multimodal_image_file_api]
|
61 | 143 |
|
| 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 | + |
62 | 152 | def test_tokens_multimodal_video_audio_file_api(self):
|
63 | 153 | # [START tokens_multimodal_video_audio_file_api]
|
| 154 | + import time |
| 155 | + |
64 | 156 | 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 | + |
67 | 178 | # [END tokens_multimodal_video_audio_file_api]
|
68 | 179 |
|
| 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 | + |
69 | 189 | def test_tokens_cached_content(self):
|
70 | 190 | # [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 | + |
73 | 197 | 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], |
76 | 203 | )
|
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() |
78 | 219 | # [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] |
80 | 229 |
|
81 | 230 | def test_tokens_system_instruction(self):
|
82 | 231 | # [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 | + |
84 | 238 | 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." |
87 | 240 | )
|
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.")) |
89 | 245 | # [END tokens_system_instruction]
|
90 | 246 |
|
| 247 | + # [START tokens_system_instruction_return] |
| 248 | + # total_tokens: 10 |
| 249 | + # |
| 250 | + # total_tokens: 21 |
| 251 | + # [END tokens_system_instruction_return] |
| 252 | + |
91 | 253 | def test_tokens_tools(self):
|
92 | 254 | # [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 | + |
93 | 265 | def add(a: float, b: float):
|
94 | 266 | """returns a + b."""
|
95 | 267 | return a + b
|
@@ -117,6 +289,12 @@ def divide(a: float, b: float):
|
117 | 289 | )
|
118 | 290 | # [END tokens_tools]
|
119 | 291 |
|
| 292 | + # [START tokens_tools_return] |
| 293 | + # total_tokens: 22 |
| 294 | + # |
| 295 | + # total_tokens: 206 |
| 296 | + # [END tokens_tools_return] |
| 297 | + |
120 | 298 |
|
121 | 299 | if __name__ == "__main__":
|
122 | 300 | absltest.main()
|
0 commit comments