瞭解及計算符記


Gemini 和其他生成式 AI 模型會以稱為「符記」的精細程度處理輸入和輸出內容。

關於符記

符記可以是單一字元 (例如 z),也可以是整個字詞 (例如 cat)。長字詞會拆成多個字元。模型使用的所有符記集合稱為詞彙,將文字分割為符記的程序稱為符記化

對於 Gemini 模型,一個符記相當於約 4 個字元。100 個符記大約等於 60 到 80 個英文單字。

啟用帳單功能後,對 Gemini API 的呼叫費用部分取決於輸入和輸出符記的數量,因此瞭解如何計算符記會很有幫助。

試著在 Colab 中計算符記

您可以使用 Colab 試試看如何計算符記。

背景期間

透過 Gemini API 提供的模型,其脈絡窗口以符記為單位。脈絡窗口會定義您可提供的輸入量,以及模型可產生的輸出量。您可以呼叫 getModels 端點,或參閱模型說明文件,來判斷內容視窗的大小。

在以下範例中,您可以看到 gemini-1.5-flash 模型的輸入限制約為 1,000,000 個符記,輸出限制約為 8,000 個符記,也就是說,情境視窗為 1,000,000 個符記。

import google.generativeai as genai

model_info = genai.get_model("models/gemini-1.5-flash")

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

計算符記

所有輸入至 Gemini API 的輸入內容和輸出內容都會經過符號化,包括文字、圖片檔案和其他非文字模式。

您可以透過下列方式計算符記:

  • 使用要求的輸入內容呼叫 count_tokens
    這會傳回僅輸入中詞元的總數。您可以在將輸入內容傳送至模型之前,先發出這項呼叫,以便檢查要求的大小。

  • 呼叫 generate_content 後,請在 response 物件上使用 usage_metadata 屬性。
    這會傳回輸入和輸出中詞元的總數:total_token_count
    它也會分別傳回輸入和輸出的符記數量:prompt_token_count (輸入符記) 和 candidates_token_count (輸出符記)。

計算文字符號

如果您使用僅文字的輸入內容呼叫 count_tokens,系統會傳回僅輸入內容 (total_tokens) 中的符記數量。您可以在呼叫 generate_content 之前發出這項呼叫,以便檢查要求的大小。

另一個選項是呼叫 generate_content,然後使用 response 物件的 usage_metadata 屬性,取得以下內容:

  • 輸入 (prompt_token_count) 和輸出 (candidates_token_count) 的個別符號計數
  • 輸入和輸出中分詞的總數 (total_token_count)
import google.generativeai as genai

model = genai.GenerativeModel("models/gemini-1.5-flash")

prompt = "The quick brown fox jumps over the lazy dog."

# Call `count_tokens` to get the input token count (`total_tokens`).
print("total_tokens: ", model.count_tokens(prompt))
# ( total_tokens: 10 )

response = model.generate_content(prompt)

# On the response for `generate_content`, use `usage_metadata`
# to get separate input and output token counts
# (`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 )

計算多輪 (聊天) 符記

如果您使用對話記錄呼叫 count_tokens,系統會傳回對話中每個角色的文字總符記數 (total_tokens)。

另一個做法是呼叫 send_message,然後使用 response 物件的 usage_metadata 屬性,取得以下內容:

  • 輸入 (prompt_token_count) 和輸出 (candidates_token_count) 的個別符號計數
  • 輸入和輸出中的符號總數 (total_token_count)

如要瞭解下一個對話輪次的大小,您需要在呼叫 count_tokens 時將其附加至記錄。

import google.generativeai as genai

model = genai.GenerativeModel("models/gemini-1.5-flash")

chat = model.start_chat(
    history=[
        {"role": "user", "parts": "Hi my name is Bob"},
        {"role": "model", "parts": "Hi Bob!"},
    ]
)
# Call `count_tokens` to get the input token count (`total_tokens`).
print(model.count_tokens(chat.history))
# ( total_tokens: 10 )

response = chat.send_message(
    "In one sentence, explain how a computer works to a young child."
)

# On the response for `send_message`, use `usage_metadata`
# to get separate input and output token counts
# (`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: 25, candidates_token_count: 21, total_token_count: 46 )

from google.generativeai.types.content_types import to_contents

# You can call `count_tokens` on the combined history and content of the next turn.
print(model.count_tokens(chat.history + to_contents("What is the meaning of life?")))
# ( total_tokens: 56 )

計算多模態符記

所有 Gemini API 輸入內容都會經過符號化,包括文字、圖片檔案和其他非文字模式。請注意以下關於 Gemini API 處理期間多模態輸入內容標記化的重點:

  • 在 Gemini 2.0 中,如果圖片輸入內容的兩個維度都小於或等於 384 像素,系統會將其計為 258 個符記。圖片的其中一個或兩個尺寸較大時,系統會視需要裁剪及縮放圖片,使其成為 768x768 像素的圖塊,每個圖塊會計為 258 個符記。在 Gemini 2.0 之前,圖片使用的是固定的 258 個符記。

  • 影片和音訊檔案會以下列固定的比率轉換為符記:影片為每秒 263 個符記,音訊為每秒 32 個符記。

圖片檔

如果您使用文字和圖片輸入內容呼叫 count_tokens,系統會在僅輸入內容 (total_tokens) 中傳回文字和圖片的組合符記數。您可以在呼叫 generate_content 之前發出這項呼叫,以便檢查要求的大小。您也可以選擇分別對文字和檔案呼叫 count_tokens

另一個做法是呼叫 generate_content,然後使用 response 物件的 usage_metadata 屬性,取得以下內容:

  • 輸入 (prompt_token_count) 和輸出 (candidates_token_count) 的個別符號計數
  • 輸入和輸出中的符號總數 (total_token_count)

使用 File API 上傳的圖片的範例:

import google.generativeai as genai

model = genai.GenerativeModel("models/gemini-1.5-flash")

prompt = "Tell me about this image"
your_image_file = genai.upload_file(path=media / "organ.jpg")

# Call `count_tokens` to get the input token count
# of the combined text and file (`total_tokens`).
# An image's display or file size does not affect its token count.
# Optionally, you can call `count_tokens` for the text and file separately.
print(model.count_tokens([prompt, your_image_file]))
# ( total_tokens: 263 )

response = model.generate_content([prompt, your_image_file])
response.text
# On the response for `generate_content`, use `usage_metadata`
# to get separate input and output token counts
# (`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 )

以下範例會以內嵌資料提供圖片:

import google.generativeai as genai

import PIL.Image

model = genai.GenerativeModel("models/gemini-1.5-flash")

prompt = "Tell me about this image"
your_image_file = PIL.Image.open(media / "organ.jpg")

# Call `count_tokens` to get the input token count
# of the combined text and file (`total_tokens`).
# An image's display or file size does not affect its token count.
# Optionally, you can call `count_tokens` for the text and file separately.
print(model.count_tokens([prompt, your_image_file]))
# ( total_tokens: 263 )

response = model.generate_content([prompt, your_image_file])

# On the response for `generate_content`, use `usage_metadata`
# to get separate input and output token counts
# (`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 )

影片或音訊檔案

音訊和影片會以下列固定費率轉換為符記:

  • 影片:每秒 263 個符記
  • 音訊:每秒 32 個符號

如果您使用文字和影片/音訊輸入內容呼叫 count_tokens,系統會針對僅輸入的文字和影片/音訊檔案,傳回文字和影片/音訊檔案的組合符記數 (total_tokens)。您可以在呼叫 generate_content 之前發出這項呼叫,以便檢查要求的大小。您也可以選擇分別對文字和檔案呼叫 count_tokens

另一個選項是呼叫 generate_content,然後使用 response 物件的 usage_metadata 屬性,取得以下內容:

  • 輸入 (prompt_token_count) 和輸出 (candidates_token_count) 的個別符號計數
  • 輸入和輸出中分詞的總數 (total_token_count)
import google.generativeai as genai

import time

model = genai.GenerativeModel("models/gemini-1.5-flash")

prompt = "Tell me about this video"
your_file = genai.upload_file(path=media / "Big_Buck_Bunny.mp4")

# Videos need to be processed before you can use them.
while your_file.state.name == "PROCESSING":
    print("processing video...")
    time.sleep(5)
    your_file = genai.get_file(your_file.name)

# Call `count_tokens` to get the input token count
# of the combined text and video/audio file (`total_tokens`).
# A video or audio file is converted to tokens at a fixed rate of tokens per second.
# Optionally, you can call `count_tokens` for the text and file separately.
print(model.count_tokens([prompt, your_file]))
# ( total_tokens: 300 )

response = model.generate_content([prompt, your_file])

# On the response for `generate_content`, use `usage_metadata`
# to get separate input and output token counts
# (`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: 301, candidates_token_count: 60, total_token_count: 361 )

系統指令和工具

系統指令和工具也會計入輸入內容的符記總數。

如果您使用系統指令,total_tokens 計數會增加,以反映 system_instruction 的新增數量。

import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash")

prompt = "The quick brown fox jumps over the lazy dog."

print(model.count_tokens(prompt))
# total_tokens: 10

model = genai.GenerativeModel(
    model_name="gemini-1.5-flash", system_instruction="You are a cat. Your name is Neko."
)

# The total token count includes everything sent to the `generate_content` request.
# When you use system instructions, the total token count increases.
print(model.count_tokens(prompt))
# ( total_tokens: 21 )

如果您使用函式呼叫,total_tokens 計數就會增加,以反映 tools 的增加。

import google.generativeai as genai

model = genai.GenerativeModel(model_name="gemini-1.5-flash")

prompt = "I have 57 cats, each owns 44 mittens, how many mittens is that in total?"

print(model.count_tokens(prompt))
# ( total_tokens: 22 )

def add(a: float, b: float):
    """returns a + b."""
    return a + b

def subtract(a: float, b: float):
    """returns a - b."""
    return a - b

def multiply(a: float, b: float):
    """returns a * b."""
    return a * b

def divide(a: float, b: float):
    """returns a / b."""
    return a / b

model = genai.GenerativeModel(
    "models/gemini-1.5-flash-001", tools=[add, subtract, multiply, divide]
)

# The total token count includes everything sent to the `generate_content` request.
# When you use tools (like function calling), the total token count increases.
print(model.count_tokens(prompt))
# ( total_tokens: 206 )