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


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

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

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

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

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

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

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

ดูที่ ai.google.dev ลองใช้ Colab Notebook ดูสมุดบันทึกใน GitHub

หน้าต่างบริบท

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

ในตัวอย่างต่อไปนี้ คุณจะเห็นว่าgemini-1.5-flashโมเดลมี ขีดจำกัดอินพุตประมาณ 1,000,000 โทเค็นและขีดจำกัดเอาต์พุตประมาณ 8,000 โทเค็น ซึ่งหมายความว่าหน้าต่างบริบทคือ 1,000,000 โทเค็น

from google import genai

client = genai.Client()
model_info = client.models.get(model="gemini-2.0-flash")
print(f"{model_info.input_token_limit=}")
print(f"{model_info.output_token_limit=}")
# ( e.g., input_token_limit=30720, output_token_limit=2048 )

นับโทเค็น

อินพุตและเอาต์พุตทั้งหมดจาก Gemini API จะได้รับการโทเค็น รวมถึงข้อความ ไฟล์รูปภาพ และรูปแบบอื่นๆ ที่ไม่ใช่ข้อความ

คุณนับโทเค็นได้ด้วยวิธีต่อไปนี้

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

from google import genai

client = genai.Client()
prompt = "The quick brown fox jumps over the lazy dog."

# Count tokens using the new client method.
total_tokens = client.models.count_tokens(
    model="gemini-2.0-flash", contents=prompt
)
print("total_tokens: ", total_tokens)
# ( e.g., total_tokens: 10 )

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=prompt
)

# The usage_metadata provides detailed token counts.
print(response.usage_metadata)
# ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )

นับโทเค็นแบบหลายรอบ (แชท)

from google import genai
from google.genai import types

client = genai.Client()

chat = client.chats.create(
    model="gemini-2.0-flash",
    history=[
        types.Content(
            role="user", parts=[types.Part(text="Hi my name is Bob")]
        ),
        types.Content(role="model", parts=[types.Part(text="Hi Bob!")]),
    ],
)
# Count tokens for the chat history.
print(
    client.models.count_tokens(
        model="gemini-2.0-flash", contents=chat.get_history()
    )
)
# ( e.g., total_tokens: 10 )

response = chat.send_message(
    message="In one sentence, explain how a computer works to a young child."
)
print(response.usage_metadata)
# ( e.g., prompt_token_count: 25, candidates_token_count: 21, total_token_count: 46 )

# You can count tokens for the combined history and a new message.
extra = types.UserContent(
    parts=[
        types.Part(
            text="What is the meaning of life?",
        )
    ]
)
history = chat.get_history()
history.append(extra)
print(client.models.count_tokens(model="gemini-2.0-flash", contents=history))
# ( e.g., total_tokens: 56 )

นับโทเค็นหลายรูปแบบ

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

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

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

ไฟล์ภาพ

ตัวอย่างที่ใช้รูปภาพที่อัปโหลดจาก File API

from google import genai

client = genai.Client()
prompt = "Tell me about this image"
your_image_file = client.files.upload(file=media / "organ.jpg")

print(
    client.models.count_tokens(
        model="gemini-2.0-flash", contents=[prompt, your_image_file]
    )
)
# ( e.g., total_tokens: 263 )

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=[prompt, your_image_file]
)
print(response.usage_metadata)
# ( e.g., prompt_token_count: 264, candidates_token_count: 80, total_token_count: 345 )

ตัวอย่างที่แสดงรูปภาพเป็นข้อมูลแบบอินไลน์

from google import genai
import PIL.Image

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

# Count tokens for combined text and inline image.
print(
    client.models.count_tokens(
        model="gemini-2.0-flash", contents=[prompt, your_image_file]
    )
)
# ( e.g., total_tokens: 263 )

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=[prompt, your_image_file]
)
print(response.usage_metadata)
# ( e.g., prompt_token_count: 264, candidates_token_count: 80, total_token_count: 345 )

ไฟล์วิดีโอหรือเสียง

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

  • วิดีโอ: 263 โทเค็นต่อวินาที
  • เสียง: 32 โทเค็นต่อวินาที
from google import genai
import time

client = genai.Client()
prompt = "Tell me about this video"
your_file = client.files.upload(file=media / "Big_Buck_Bunny.mp4")

# Poll until the video file is completely processed (state becomes ACTIVE).
while not your_file.state or your_file.state.name != "ACTIVE":
    print("Processing video...")
    print("File state:", your_file.state)
    time.sleep(5)
    your_file = client.files.get(name=your_file.name)

print(
    client.models.count_tokens(
        model="gemini-2.0-flash", contents=[prompt, your_file]
    )
)
# ( e.g., total_tokens: 300 )

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=[prompt, your_file]
)
print(response.usage_metadata)
# ( e.g., prompt_token_count: 301, candidates_token_count: 60, total_token_count: 361 )

คำสั่งและเครื่องมือของระบบ

คำสั่งและเครื่องมือของระบบจะนับรวมในจำนวนโทเค็นทั้งหมดสำหรับ อินพุตด้วย

หากคุณใช้คำสั่งของระบบ จำนวน total_tokens จะเพิ่มขึ้นเพื่อแสดงการเพิ่ม system_instruction

หากใช้การเรียกใช้ฟังก์ชัน จำนวน total_tokens จะเพิ่มขึ้นเพื่อแสดงถึงการเพิ่ม tools