המודל של 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
ומזינים את הבקשה.
הפונקציה הזו מחזירה את המספר הכולל של האסימונים בקלט בלבד. אפשר לבצע את הקריאה הזו לפני שליחת הקלט למודל כדי לבדוק את הגודל של הבקשות.משתמשים במאפיין
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, קלט של תמונות עם שני המימדים קטנים מ-384 פיקסלים נספר כ-258 אסימונים. תמונות גדולות יותר במידות אחת או בשתיהן חתוכות ומשוקללות לפי הצורך, כך שיתאימו לתמונות בגודל 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 )