From c908a5043cb9c8f2fa503e78ddcdc22c17c68f83 Mon Sep 17 00:00:00 2001 From: Mayuresh Agashe Date: Thu, 14 Mar 2024 13:45:01 +0530 Subject: [PATCH] Re-add demo to show `Count tokens` (#296) * Add `GenerativeModel.count_tokens` demonstration * Format notebook with nbfmt * Update information about tokens * Update python_quickstart.ipynb * Update python_quickstart.ipynb * nbfmt --------- Co-authored-by: Mark McDonald --- site/en/tutorials/python_quickstart.ipynb | 58 +++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/site/en/tutorials/python_quickstart.ipynb b/site/en/tutorials/python_quickstart.ipynb index a3be1f52f..167e74369 100644 --- a/site/en/tutorials/python_quickstart.ipynb +++ b/site/en/tutorials/python_quickstart.ipynb @@ -1034,6 +1034,64 @@ " display(to_markdown(f'**{message.role}**: {message.parts[0].text}'))" ] }, + { + "cell_type": "markdown", + "metadata": { + "id": "AEgVOYu0pAr4" + }, + "source": [ + "## Count tokens\n", + "\n", + "Large language models have a context window, and the context length is often measured in terms of the **number of tokens**. With the Gemini API, you can determine the number of tokens per any `glm.Content` object. In the simplest case, you can pass a query string to the `GenerativeModel.count_tokens` method as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "eLjBmPCLpElk" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total_tokens: 7" + ] + } + ], + "source": [ + "model.count_tokens(\"What is the meaning of life?\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oM2_U8pmpHQA" + }, + "source": [ + "Similarly, you can check `token_count` for your `ChatSession`:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "i0MUU4BZpG4_" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "total_tokens: 501" + ] + } + ], + "source": [ + "model.count_tokens(chat.history)" + ] + }, { "cell_type": "markdown", "metadata": {