ทําความเข้าใจและนับโทเค็น


Gemini และโมเดล Generative AI อื่นๆ จะประมวลผลอินพุตและเอาต์พุตในระดับรายละเอียดที่เรียกว่า โทเค็น

เกี่ยวกับโทเค็น

โดยโทเค็นอาจเป็นอักขระเดี่ยว เช่น z หรือทั้งคำ เช่น cat คํายาวๆ จะแบ่งออกเป็นโทเค็นหลายรายการ ชุดโทเค็นทั้งหมดที่โมเดลใช้เรียกว่าคําศัพท์ และกระบวนการแยกข้อความออกเป็นโทเค็นเรียกว่าการแยกโทเค็น

สำหรับโมเดล Gemini โทเค็นจะเท่ากับประมาณ 4 อักขระ โทเค็น 100 รายการเท่ากับคำภาษาอังกฤษประมาณ 60-80 คำ

เมื่อเปิดใช้การเรียกเก็บเงิน ต้นทุนของการเรียกใช้ Gemini API จะพิจารณาจากจำนวนโทเค็นอินพุตและเอาต์พุตส่วนหนึ่ง ดังนั้นการทราบวิธีนับโทเค็นจึงมีประโยชน์

ลองนับโทเค็นใน Colab

คุณลองนับโทเค็นได้โดยใช้ Colab

กรอบเวลาบริบท

โมเดลที่ใช้ได้ผ่าน Gemini API จะมีกรอบเวลาบริบทที่วัดเป็นโทเค็น กรอบเวลาบริบทจะกําหนดจํานวนอินพุตที่คุณให้ได้ และจํานวนเอาต์พุตที่โมเดลสร้างได้ คุณกำหนดขนาดของกรอบบริบทได้โดยเรียกใช้ปลายทาง getModel หรือดูในเอกสารประกอบเกี่ยวกับโมเดล

ในตัวอย่างต่อไปนี้ คุณจะเห็นได้ว่าโมเดล 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 พร้อมข้อมูลคำขอ
    การดำเนินการนี้จะแสดงผลจํานวนโทเค็นทั้งหมดในอินพุตเท่านั้น คุณเรียกใช้การเรียกนี้ก่อนส่งอินพุตไปยังโมเดลเพื่อตรวจสอบขนาดของคำขอได้

  • ใช้แอตทริบิวต์ usage_metadata ในออบเจ็กต์ response หลังจากเรียกใช้ generate_content
    การดำเนินการนี้จะแสดงจํานวนโทเค็นทั้งหมดในทั้งอินพุตและเอาต์พุต ดังนี้ total_token_count
    นอกจากนี้ยังแสดงผลจํานวนโทเค็นของอินพุตและเอาต์พุตแยกกัน ดังนี้ prompt_token_count (โทเค็นอินพุต) และ candidates_token_count (โทเค็นเอาต์พุต)

นับโทเค็นข้อความ

หากคุณเรียกใช้ count_tokens ด้วยอินพุตที่เป็นข้อความเท่านั้น ระบบจะแสดงผลจํานวนโทเค็นของข้อความในอินพุตเท่านั้น (total_tokens) คุณสามารถเรียกใช้ฟังก์ชันนี้ก่อนเรียกใช้ generate_content เพื่อตรวจสอบขนาดของคําขอ

อีกทางเลือกหนึ่งคือการเรียกใช้ generate_content แล้วใช้แอตทริบิวต์ usage_metadata ในออบเจ็กต์ response เพื่อรับผลลัพธ์ต่อไปนี้

  • จํานวนโทเค็นแยกต่างหากของอินพุต (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 แล้วใช้แอตทริบิวต์ usage_metadata ในออบเจ็กต์ response เพื่อรับผลลัพธ์ต่อไปนี้

  • จํานวนโทเค็นแยกต่างหากของอินพุต (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 ระบบจะนับอินพุตรูปภาพที่มีทั้ง 2 มิติไม่เกิน 384 พิกเซลเป็นโทเค็น 258 รายการ ระบบจะครอบตัดและปรับขนาดรูปภาพที่มีขนาดใหญ่กว่าในมิติข้อมูลอย่างน้อย 1 หรือทั้ง 2 มิติเป็นไทล์ขนาด 768x768 พิกเซลตามที่จำเป็น โดยแต่ละรูปภาพจะนับเป็นโทเค็น 258 รายการ ก่อน Gemini 2.0 รูปภาพใช้โทเค็น 258 รายการแบบคงที่

  • ระบบจะแปลงไฟล์วิดีโอและไฟล์เสียงเป็นโทเค็นในอัตราคงที่ต่อไปนี้ วิดีโอที่ 263 โทเค็นต่อวินาทีและเสียงที่ 32 โทเค็นต่อวินาที

ไฟล์ภาพ

หากคุณเรียกใช้ count_tokens ด้วยอินพุตข้อความและรูปภาพ ระบบจะแสดงผลจำนวนโทเค็นรวมของข้อความและรูปภาพในอินพุตเท่านั้น (total_tokens) คุณสามารถเรียกใช้การเรียกนี้ก่อนเรียกใช้ generate_content เพื่อตรวจสอบขนาดของคำขอ นอกจากนี้ คุณยังเรียกใช้ count_tokens ในข้อความและไฟล์แยกกันได้

อีกทางเลือกหนึ่งคือการเรียกใช้ generate_content แล้วใช้แอตทริบิวต์ usage_metadata ในออบเจ็กต์ response เพื่อรับผลลัพธ์ต่อไปนี้

  • จํานวนโทเค็นแยกต่างหากของอินพุต (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 แล้วใช้แอตทริบิวต์ usage_metadata ในออบเจ็กต์ response เพื่อรับผลลัพธ์ต่อไปนี้

  • จํานวนโทเค็นแยกต่างหากของอินพุต (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 )