فهم الرموز المميّزة وعدّها


تعمل نماذج Gemini وغيرها من نماذج الذكاء الاصطناعي التوليدي على معالجة المدخلات والمخرجات بدقة. تسمى الرمز المميز.

يشرح هذا الدليل كيفية الحصول على النوافذ السياقية لنماذج محددة، بالإضافة إلى كيفية عدد الرموز المميّزة لحالات الاستخدام، مثل إدخال النص والمحادثة والوسائط المتعددة الوسائط والإدخال وتعليمات النظام وأدواته.

لمحة عن الرموز المميّزة

يمكن أن تتألف الرموز المميّزة من أحرف فردية مثل z أو كلمات كاملة مثل cat. كلمات طويلة يتم تقسيمها إلى عدة رموز مميزة. مجموعة كل الرموز المميزة التي يستخدمها النموذج هي المفردات، وعملية تقسيم النص إلى رموز مميزة إنشاء الرموز المميّزة.

في نماذج Gemini، يعادل الرمز المميّز 4 أحرف تقريبًا. 100 رمز مميّز يساوي حوالي 60-80 كلمة إنجليزية.

عندما تكون الفوترة مفعَّلة، تكون تكلفة الاتصال بواجهة Gemini API هي محددة جزئيًا من خلال عدد رموز المدخلات والمخرجات، لذا فإن معرفة كيفية عدد الرموز المميزة يمكن أن تكون مفيدة.

العرض على ai.google.dev التنفيذ في Google Colab عرض المصدر على GitHub

نوافذ السياق

للنماذج المتوفّرة من خلال Gemini API نوافذ سياقية: وتُقاس بالرموز المميزة. تحدِّد نافذة السياق مقدار المعلومات التي يمكنك تقديمها. وحجم المخرجات التي يمكن أن ينتجها النموذج. يمكنك تحديد حجم سياق باستخدام API أو من خلال البحث في مستندات models.

في المثال التالي، يمكنك ملاحظة أنّ النموذج gemini-1.0-pro-001 له سمة حد إدخال يبلغ حوالي 30 ألف رمز مميز وحد إخراج يبلغ حوالي ألفي رمز، ويعني نافذة سياق لحوالي 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، ستلاحظ أنّ الفيديو يحتوي على مليونَي سياق سياق.

عدد الرموز المميّزة

يتم تحويل جميع الإدخالات والمخرجات من 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 )