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


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

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

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

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

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

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

ดูใน ai.google.dev เรียกใช้ใน Google Colab ดูแหล่งที่มาใน GitHub

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

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

ในตัวอย่างต่อไปนี้ คุณจะเห็นว่าโมเดล gemini-1.0-pro-001 มี มีขีดจำกัดอินพุตประมาณ 30,000 โทเค็น และขีดจำกัดเอาต์พุตประมาณ 2,000 โทเค็น หมายถึงหน้าต่างบริบทที่มีโทเค็น ประมาณ 32,000 โทเค็น

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 คุณจะเห็นหน้าต่างบริบทขนาด 2 ล้าน

นับโทเค็น

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