टोकन को समझें और उनकी गिनती करें


Gemini और अन्य जनरेटिव एआई मॉडल, इनपुट और आउटपुट को बारीकी से प्रोसेस करते हैं टोकन कहते हैं.

यह गाइड बताती है कि अलग-अलग मॉडल की कॉन्टेक्स्ट विंडो के साथ-साथ, टेक्स्ट इनपुट, चैट, और मल्टीमोडल जैसे इस्तेमाल के उदाहरणों के लिए, काउंट टोकन और सिस्टम के लिए निर्देश, और टूल हैं.

टोकन के बारे में जानकारी

टोकन, एक वर्ण हो सकते हैं, जैसे कि z या पूरे शब्द, जैसे कि cat. लंबे शब्द उन्हें कई टोकन में बांटा जाता है. मॉडल में इस्तेमाल किए गए सभी टोकन का सेट है जिसे शब्दावली कहते हैं और टेक्स्ट को टोकन में विभाजित करने की प्रक्रिया टोकनाइज़ेशन के दायरे में आता है.

Gemini मॉडल के लिए, एक टोकन, करीब चार वर्णों के बराबर होता है. 100 टोकन, करीब 60 से 80 अंग्रेज़ी शब्दों के बराबर होते हैं.

बिलिंग की सुविधा चालू होने पर, Gemini API को कॉल करने का शुल्क कितना होगा इनपुट और आउटपुट टोकन की संख्या के हिसाब से तय होता है. इसलिए, यह जानना ज़रूरी है कि की संख्या से जुड़े टोकन मददगार साबित हो सकते हैं.

ai.google.dev पर देखें Google Colab में चलाएं GitHub पर सोर्स देखें

कॉन्टेक्स्ट विंडो

Gemini API के ज़रिए उपलब्ध मॉडल में कॉन्टेक्स्ट विंडो होती है जिन्हें टोकन में मेज़र किया गया हो. कॉन्टेक्स्ट विंडो से पता चलता है कि कितना इनपुट दिया जा सकता है और मॉडल कितना आउटपुट जनरेट कर सकता है. आप यह पता लगा सकते हैं कि API का इस्तेमाल करके या मॉडल दस्तावेज़.

यहां दिए गए उदाहरण में देखा जा सकता है कि gemini-1.0-pro-001 मॉडल में करीब 30 हज़ार टोकन के इनपुट की सीमा और आउटपुट की सीमा करीब 2, 000 टोकन होती है. मतलब करीब 32 हज़ार टोकन की कॉन्टेक्स्ट विंडो है.

model_info = genai.get_model("models/gemini-1.0-pro-001")

# 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-1.5-flash-001, आपको दिखेगा कि इसमें 20 लाख कॉन्टेक्स्ट विंडो है.

टोकन गिनें

Gemini API के इनपुट और आउटपुट के तौर पर, टेक्स्ट और इमेज को टोकन के तौर पर इस्तेमाल किया जाता है और बिना टेक्स्ट वाले अन्य मॉडल.

टोकन की गिनती इन तरीकों से की जा सकती है:

टेक्स्ट टोकन गिनें

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 )

एक से ज़्यादा टर्न वाले (चैट) टोकन गिनना

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 को सभी इनपुट, टोकन के तौर पर दिए जाते हैं. इसमें टेक्स्ट, इमेज फ़ाइलें वगैरह शामिल हैं बिना टेक्स्ट वाले मोड. टोकनाइज़ेशन के बारे में यहां दी गई खास बातों का ध्यान रखें के मल्टीमोडल इनपुट इस्तेमाल करने के लिए:

  • इमेज को तय साइज़ माना जाता है. इसलिए, वे तय की गई साइज़ की टोकन (फ़िलहाल 258 टोकन), उनके डिसप्ले या फ़ाइल साइज़ पर ध्यान दिए बिना.

  • वीडियो और ऑडियो फ़ाइलों को यहां तय की गई दरों पर टोकन में बदला जाता है: 263 टोकन प्रति सेकंड और ऑडियो के लिए 32 टोकन प्रति सेकंड.

इमेज फ़ाइलें

प्रोसेस होने के दौरान, Gemini API इमेज को तय साइज़ के हिसाब से मानता है. इसलिए, टोकन की तय संख्या (फ़िलहाल, 258 टोकन) का इस्तेमाल करते हैं. भले ही, उनकी डिसप्ले या फ़ाइल साइज़.

File API से अपलोड की गई इमेज का इस्तेमाल करने वाला उदाहरण:

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

prompt = "Tell me about this image"
your_image_file = genai.upload_file(path="image.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 PIL.Image

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

prompt = "Tell me about this image"
your_image_file = PIL.Image.open("image.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 टोकन
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 को जोड़ा गया.

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 जोड़.

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 )