העמקה

כל המודלים של Gemini משתמשים ב "תהליך חשיבה" פנימי שמשפר משמעותית את יכולות החשיבה והתכנון הרב-שלבי שלהם, והופך אותם ליעילים מאוד במשימות מורכבות כמו תכנות, מתמטיקה מתקדמת וניתוח נתונים.

המודלים החושבים מציעים את ההגדרות והאפשרויות הבאות:

  • שליטה בכמות החשיבה
    אתם יכולים להגדיר רמות חשיבה כדי לקבוע כמה "חשיבה" המודל יכול לבצע. ההגדרה הזו חשובה במיוחד אם אתם רוצים לצמצם את זמן האחזור או את העלות. כדאי גם לעיין בהשוואה בין רמות הקושי של המשימות כדי להחליט עד כמה המודל צריך את יכולת החשיבה שלו.

  • קבלת סיכומי תהליך החשיבה
    אתם יכולים להפעיל את סיכומי תהליך החשיבה כדי לכלול אותם בתשובה שנוצרת. הסיכומים האלה הם גרסאות מסונתזות של המחשבות הגולמיות של המודל, והם מספקים תובנות לגבי תהליך החשיבה הפנימי של המודל.

  • טיפול בחתימות של מחשבות
    ‫Firebase AI Logic SDKs מטפלים אוטומטית בחתימות של מחשבות בשבילכם, וכך מוודאים שלמודל יש גישה להקשר של המחשבות מהתורות הקודמות, במיוחד כשמשתמשים בקריאה להפעלת פונקציות.

חשוב לקרוא את השיטות המומלצות וההנחיות לכתיבת הנחיות לשימוש במודלים של חשיבה.



שיטות מומלצות והנחיות לכתיבת הנחיות לשימוש במודלים של חשיבה

מומלץ לבדוק את ההנחיה ב-Google AI Studio או ב-Agent  Studio, שבהם אפשר לראות את תהליך החשיבה המלא. אתם יכולים לזהות תחומים שבהם המודל סטה מהמסלול, ולשפר את ההנחיות כדי לקבל תשובות עקביות ומדויקות יותר.

מתחילים בהנחיה כללית שמתארת את התוצאה הרצויה, ובוחנים את המחשבות הראשוניות של המודל לגבי האופן שבו הוא קובע את התשובה שלו. אם התשובה לא תואמת לציפיות, אפשר להשתמש בטכניקות הבאות לכתיבת הנחיות כדי לעזור למודל ליצור תשובה טובה יותר:

  • הוראות מפורטות
  • תנו כמה דוגמאות של זוגות קלט-פלט
  • הנחיות לניסוח ולעיצוב של הפלט והתשובות
  • לספק שלבי אימות ספציפיים

בנוסף להנדסת פרומפטים, כדאי להשתמש בהמלצות הבאות:

  • הגדרת הוראות מערכת, שהן כמו "הוראות מקדימות" שמוסיפים לפני שהמודל נחשף להוראות נוספות מהפרומפט או ממשתמש הקצה. הן מאפשרות לכם לכוון את ההתנהגות של המודל בהתאם לצרכים ולתרחישי השימוש הספציפיים שלכם.

  • הגדרת רמת חשיבה מאפשרת לשלוט בהיקף החשיבה שהמודל יכול לבצע. אם מגדירים רמה גבוהה, המודל יכול לחשוב יותר, אם יש צורך בכך. אם מגדירים ערך נמוך יותר, המודל לא "יחשוב יותר מדי" על התשובה שלו, וגם ישמור יותר ממגבלת הפלט הכוללת של האסימונים לתשובה בפועל, מה שיכול לעזור להפחית את זמן האחזור והעלות.

  • מפעילים את המעקב אחרי שימוש בתכונות AI במסוף Firebase כדי לעקוב אחרי מספר טוקנים של חשיבה והחביון של הבקשות שהופעלה בהן חשיבה. אם הפעלתם את סיכומי המחשבות, הם יוצגו במסוף, שם תוכלו לבדוק את תהליך החשיבה המפורט של המודל כדי לנפות באגים ולשפר את ההנחיות.



שליטה בכמות החשיבה

אתם יכולים להגדיר רמות חשיבה כדי לקבוע כמה "חשיבה" והסקת מסקנות המודל יכול לבצע לפני שהוא מחזיר תשובה. ההגדרה הזו חשובה במיוחד אם אתם רוצים לצמצם את זמן האחזור או את העלות.

כדאי לעיין בהשוואה בין רמות הקושי של המשימות כדי להחליט כמה יכולת חשיבה המודל צריך. הנה כמה הנחיות כלליות:

  • כדאי להגדיר ערך חשיבה נמוך יותר למשימות פחות מורכבות, או אם חשוב לכם להקטין את זמן האחזור או את העלות.
  • כדאי להגדיר ערך חשיבה גבוה יותר למשימות מורכבות יותר.

רמות ההעמקה

כדי לקבוע כמה חשיבה מודל יכול להקדיש כדי ליצור את התשובה, אפשר לציין רמת חשיבה עבור מספר טוקנים של חשיבה שמותר לו להשתמש בהם.

הגדרת רמת ההעמקה

כדי לראות תוכן וקוד שספציפיים לספק, לוחצים על הספק Gemini API בדף הזה.

מגדירים את רמת החשיבה ב-GenerationConfig כחלק מתהליך היצירה של מופע GenerativeModel. ההגדרה נשמרת לאורך כל משך החיים של המכונה. אם אתם רוצים להשתמש ברמות חשיבה שונות לבקשות שונות, אתם יכולים ליצור מכונות GenerativeModel שמוגדרות לכל רמה.

בהמשך הקטע הזה מפורטים הערכים הנתמכים של רמת העמקה.

Swift

מגדירים את רמת החשיבה ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(thinkingLevel: .low)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
)

// ...

Kotlin

מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      thinkingLevel = ThinkingLevel.LOW
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_3.x_MODEL_NAME",
  generationConfig,
)

// ...

Java

מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setThinkingLevel(ThinkingLevel.LOW)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_3.x_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// ...

Web

מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
const generationConfig = {
  thinkingConfig: {
    thinkingLevel: ThinkingLevel.LOW
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_3.x_MODEL_NAME", generationConfig });

// ...

Dart

מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מכונה של GenerativeModel.


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
final thinkingConfig = ThinkingConfig.withThinkingLevel(ThinkingLevel.low);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_3.x_MODEL_NAME',
  config: generationConfig,
);

// ...

Unity

מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Use a thinking level value appropriate for your model (example value shown here).
var thinkingConfig = new ThinkingConfig(thinkingLevel: ThinkingLevel.Low);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_3.x_MODEL_NAME",
  generationConfig: generationConfig
);

// ...

ערכים נתמכים של רמת ההעמקה

בטבלה הבאה מפורטים הערכים של רמת החשיבה שאפשר להגדיר לכל מודל על ידי הגדרת thinkingLevel של המודל.

MINIMAL LOW MEDIUM HIGH

המודל משתמש בכמה שפחות אסימונים, כמעט בלי חשיבה

משימות עם מורכבות נמוכה

המודל משתמש בפחות אסימונים, ומצמצם את זמן האחזור והעלות

משימות פשוטות ומשימות עם נפח נתונים גבוה

המודל משתמש בגישה מאוזנת

משימות ברמת מורכבות בינונית

המודל משתמש בטוקנים עד לרמה המקסימלית שלו

הנחיות מורכבות שדורשות חשיבה מעמיקה

Gemini 4 Argon
gemini-4-argon
1 ‫ (ברירת מחדל)
Gemini 3.x Pro
gemini-3.1-pro-preview
‫ (ברירת מחדל)
Gemini 3.x Flash
gemini-3.8-flash
gemini-3.7-flash
gemini-3.6-flash
gemini-3.5-flash

not supported by gemini-3.8-flash or gemini-3.7-flash 1
‫ (ברירת מחדל)
Gemini 3.x Flash‑Lite
gemini-3.5-flash-lite
gemini-3.1-flash-lite
‫ (ברירת מחדל)
Gemini 3.x Pro Image
gemini-3-pro-image
("Nano Banana Pro")
‫ (ברירת המחדל)
Gemini 3.x Flash Image
gemini-3.1-flash-image
("Nano Banana 2")
‫ (ברירת מחדל)
Gemini 3.x Flash‑Lite Image
gemini-3.1-flash-lite-image
("Nano Banana 2 Lite")
‫ (ברירת מחדל)

‫1 המודלים gemini-4-argon,‏ gemini-3.8-flash ו-gemini-3.7-flash לא תומכים ברמת ההעמקה MINIMAL. אם תגדירו את רמת החשיבה ל-MINIMAL, הבקשה תיכשל ותופיע שגיאה 400.



תקציבי חשיבה (מודלים של Gemini 2.5)

כדי לקבוע כמה זמן מודל Gemini 2.5 יכול להקדיש לחשיבה לפני שהוא יוצר תשובה, אתם יכולים להגדיר תקציב חשיבה – מספר הטוקנים של חשיבה שהמודל יכול להשתמש בהם.



מורכבות המשימה לכל המודלים החושבים

  • משימות פשוטות – לא צריך לחשוב יותר מדי
    בקשות פשוטות שלא דורשות חשיבה מורכבת, כמו שליפת עובדות או סיווג. דוגמאות:

    • ‫"Where was DeepMind founded?‎"
    • "האם באימייל הזה מבקשים לקיים פגישה או רק מספקים מידע?"
  • משימות ברמת קושי בינונית – כנראה נדרשת מידה מסוימת של חשיבה
    בקשות נפוצות שדורשות עיבוד שלבים או הבנה מעמיקה יותר. דוגמאות:

    • "צור אנלוגיה בין פוטוסינתזה לבין תהליך ההתבגרות".
    • השווה והבדל בין מכוניות חשמליות לרכבים היברידיים.
  • משימות קשות – יכול להיות שיהיה צורך בחשיבה מקסימלית
    אתגרים מורכבים באמת, כמו פתרון בעיות מתמטיות מורכבות או משימות תכנות. כדי לבצע משימות כאלה, המודל צריך להשתמש בכל יכולות החשיבה והתכנון שלו, ולעיתים קרובות הוא מבצע הרבה שלבים פנימיים לפני שהוא מספק תשובה. דוגמאות:

    • "פתור את בעיה 1 ב-AIME 2025: מצא את סכום כל הבסיסים השלמים b > 9 שעבורם 17b הוא מחלק של 97b".
    • "תכתוב קוד Python לאפליקציית אינטרנט שמציגה נתונים של שוק המניות בזמן אמת, כולל אימות משתמשים. התשובה צריכה להיות יעילה ככל האפשר".



סיכומי מחשבות

סיכומי מחשבות הם גרסאות מסוכמות של המחשבות הגולמיות של המודל, ומספקים תובנות לגבי תהליך החשיבה הפנימי של המודל.

הנה כמה סיבות לכלול סיכומי תהליך חשיבה בתשובות:

  • אתם יכולים להציג את סיכום תהליך החשיבה בממשק המשתמש של האפליקציה או לאפשר למשתמשים לגשת אליו. סיכום המחשבה מוחזר כחלק נפרד בתשובה, כדי שתהיה לכם יותר שליטה באופן השימוש בו באפליקציה.

  • אם תפעילו גם את המעקב אחרי שימוש בתכונות AI במסוף Firebase, סיכומי שלבים בתהליך החשיבה יוצגו במסוף, ותוכלו לבדוק את החשיבה הרציונלית המפורטת של המודל כדי לנפות באגים ולשפר את הפרומפטים.

הנה כמה נקודות חשובות לגבי סיכומי תהליך החשיבה:

  • לא ניתן לשלוט בסיכומי המחשבות באמצעות תקציבי חשיבה (תקציבים חלים רק על המחשבות הגולמיות של המודל). עם זאת, אם החשיבה מושבתת, המודל לא יחזיר סיכום של תהליך החשיבה.

  • סיכומי המחשבות נחשבים לחלק מהתגובה הרגילה של המודל שנוצרת מטקסט, והם נספרים כאסימוני פלט.

הפעלת סיכומים של תהליך החשיבה

כדי לראות תוכן וקוד שספציפיים לספק, לוחצים על הספק Gemini API בדף הזה.

כדי להפעיל את סיכומי תהליך החשיבה, מגדירים את includeThoughts כ-true בהגדרות המודל. אחר כך תוכלו לגשת לסיכום על ידי בדיקת השדה thoughtSummary בתגובה.

בדוגמה הבאה אפשר לראות איך מפעילים את סיכומי המחשבות ומקבלים אותם עם התשובה:

Swift

מפעילים את סיכומי תהליך החשיבה בGenerationConfig כחלק מיצירת מכונה של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(includeThoughts: true)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
)

let response = try await model.generateContent("solve x^2 + 4x + 4 = 0")

// Handle the response that includes thought summaries.
if let thoughtSummary = response.thoughtSummary {
  print("Thought Summary: \(thoughtSummary)")
}
guard let text = response.text else {
  fatalError("No text in response.")
}
print("Answer: \(text)")

Kotlin

מפעילים את סיכומי החשיבה ב-GenerationConfig במהלך יצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      includeThoughts = true
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_MODEL_NAME",
  generationConfig,
)

val response = model.generateContent("solve x^2 + 4x + 4 = 0")

// Handle the response that includes thought summaries.
response.thoughtSummary?.let {
    println("Thought Summary: $it")
}
response.text?.let {
    println("Answer: $it")
}

Java

מפעילים את סיכומי תהליך החשיבה בGenerationConfig כחלק מיצירת מכונה של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setIncludeThoughts(true)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// Handle the response that includes thought summaries.
ListenableFuture responseFuture = model.generateContent("solve x^2 + 4x + 4 = 0");
Futures.addCallback(responseFuture, new FutureCallback() {
    @Override
    public void onSuccess(GenerateContentResponse response) {
        if (response.getThoughtSummary() != null) {
            System.out.println("Thought Summary: " + response.getThoughtSummary());
        }
        if (response.getText() != null) {
            System.out.println("Answer: " + response.getText());
        }
    }

    @Override
    public void onFailure(Throwable t) {
        // Handle error
    }
}, MoreExecutors.directExecutor());

Web

מפעילים את סיכומי החשיבה ב-GenerationConfig במהלך יצירת מופע של GenerativeModel.


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
const generationConfig = {
  thinkingConfig: {
    includeThoughts: true
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });

const result = await model.generateContent("solve x^2 + 4x + 4 = 0");
const response = result.response;

// Handle the response that includes thought summaries.
if (response.thoughtSummary()) {
    console.log(`Thought Summary: ${response.thoughtSummary()}`);
}
const text = response.text();
console.log(`Answer: ${text}`);

Dart

מפעילים את סיכומי המחשבות ב-GenerationConfig במהלך יצירת מופע GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
final thinkingConfig = ThinkingConfig(includeThoughts: true);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  generationConfig: generationConfig,
);

final response = await model.generateContent('solve x^2 + 4x + 4 = 0');

// Handle the response that includes thought summaries.
if (response.thoughtSummary != null) {
  print('Thought Summary: ${response.thoughtSummary}');
}
if (response.text != null) {
  print('Answer: ${response.text}');
}

Unity

מפעילים את סיכומי החשיבה ב-GenerationConfig במהלך יצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
var thinkingConfig = new ThinkingConfig(includeThoughts: true);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

var response = await model.GenerateContentAsync("solve x^2 + 4x + 4 = 0");

// Handle the response that includes thought summaries.
if (response.ThoughtSummary != null) {
    Debug.Log($"Thought Summary: {response.ThoughtSummary}");
}
if (response.Text != null) {
    Debug.Log($"Answer: {response.Text}");
}

סיכום תהליך החשיבה בזמן אמת

אפשר גם לראות סיכומים של תהליך החשיבה אם בוחרים להזרים תשובה באמצעות generateContentStream. הפעולה הזו תחזיר סיכומים מצטברים ומתגלגלים במהלך יצירת התשובה.

Swift

מפעילים את סיכומי החשיבה ב-GenerationConfig במהלך יצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
let generationConfig = GenerationConfig(
  thinkingConfig: ThinkingConfig(includeThoughts: true)
)

// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
)

let stream = try model.generateContentStream("solve x^2 + 4x + 4 = 0")

// Handle the streamed response that includes thought summaries.
var thoughts = ""
var answer = ""
for try await response in stream {
  if let thought = response.thoughtSummary {
    if thoughts.isEmpty {
      print("--- Thoughts Summary ---")
    }
    print(thought)
    thoughts += thought
  }

  if let text = response.text {
    if answer.isEmpty {
      print("--- Answer ---")
    }
    print(text)
    answer += text
  }
}

Kotlin

מפעילים את סיכומי החשיבה ב-GenerationConfig במהלך יצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
val generationConfig = generationConfig {
  thinkingConfig = thinkingConfig {
      includeThoughts = true
  }
}

// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
  modelName = "GEMINI_MODEL_NAME",
  generationConfig,
)

// Handle the streamed response that includes thought summaries.
var thoughts = ""
var answer = ""
model.generateContentStream("solve x^2 + 4x + 4 = 0").collect { response ->
    response.thoughtSummary?.let {
        if (thoughts.isEmpty()) {
            println("--- Thoughts Summary ---")
        }
        print(it)
        thoughts += it
    }
    response.text?.let {
        if (answer.isEmpty()) {
            println("--- Answer ---")
        }
        print(it)
        answer += it
    }
}

Java

מפעילים את סיכומי תהליך החשיבה בGenerationConfig כחלק מיצירת מכונה של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
    .setIncludeThoughts(true)
    .build();

GenerationConfig generationConfig = GenerationConfig.builder()
    .setThinkingConfig(thinkingConfig)
    .build();

// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
        FirebaseAI.getInstance(GenerativeBackend.googleAI())
                .generativeModel(
                  /* modelName */ "GEMINI_MODEL_NAME",
                  /* generationConfig */ generationConfig
                );
);

// Streaming with Java is complex and depends on the async library used.
// This is a conceptual example using a reactive stream.
Flowable responseStream = model.generateContentStream("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries
StringBuilder thoughts = new StringBuilder();
StringBuilder answer = new StringBuilder();

responseStream.subscribe(response -> {
    if (response.getThoughtSummary() != null) {
        if (thoughts.length() == 0) {
            System.out.println("--- Thoughts Summary ---");
        }
        System.out.print(response.getThoughtSummary());
        thoughts.append(response.getThoughtSummary());
    }
    if (response.getText() != null) {
        if (answer.length() == 0) {
            System.out.println("--- Answer ---");
        }
        System.out.print(response.getText());
        answer.append(response.getText());
    }
}, throwable -> {
    // Handle error
});

Web

מפעילים את סיכומי תהליך החשיבה בGenerationConfig כחלק מיצירת מכונה של GenerativeModel.


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
const generationConfig = {
  thinkingConfig: {
    includeThoughts: true
  }
};

// Specify the config as part of creating the `GenerativeModel` instance.
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });

const result = await model.generateContentStream("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries.
let thoughts = "";
let answer = "";
for await (const chunk of result.stream) {
  if (chunk.thoughtSummary()) {
    if (thoughts === "") {
      console.log("--- Thoughts Summary ---");
    }
    // In Node.js, process.stdout.write(chunk.thoughtSummary()) could be used
    // to avoid extra newlines.
    console.log(chunk.thoughtSummary());
    thoughts += chunk.thoughtSummary();
  }

  const text = chunk.text();
  if (text) {
    if (answer === "") {
      console.log("--- Answer ---");
    }
    // In Node.js, process.stdout.write(text) could be used.
    console.log(text);
    answer += text;
  }
}

Dart

מפעילים את סיכומי החשיבה ב-GenerationConfig כחלק מיצירת מכונה של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
final thinkingConfig = ThinkingConfig(includeThoughts: true);

final generationConfig = GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
final model = FirebaseAI.googleAI().generativeModel(
  model: 'GEMINI_MODEL_NAME',
  generationConfig: generationConfig,
);

final responses = model.generateContentStream('solve x^2 + 4x + 4 = 0');

// Handle the streamed response that includes thought summaries.
var thoughts = '';
var answer = '';
await for (final response in responses) {
  if (response.thoughtSummary != null) {
    if (thoughts.isEmpty) {
      print('--- Thoughts Summary ---');
    }
    thoughts += response.thoughtSummary!;
  }
  if (response.text != null) {
    if (answer.isEmpty) {
      print('--- Answer ---');
    }
    answer += response.text!;
  }
}

Unity

מפעילים את סיכומי החשיבה ב-GenerationConfig במהלך יצירת מופע של GenerativeModel.


// ...

// Set the thinking configuration.
// Optionally enable thought summaries in the generated response (default is false).
var thinkingConfig = new ThinkingConfig(includeThoughts: true);

var generationConfig = new GenerationConfig(
  thinkingConfig: thinkingConfig
);

// Specify the config as part of creating the `GenerativeModel` instance.
var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetGenerativeModel(
  modelName: "GEMINI_MODEL_NAME",
  generationConfig: generationConfig
);

var stream = model.GenerateContentStreamAsync("solve x^2 + 4x + 4 = 0");

// Handle the streamed response that includes thought summaries.
var thoughts = "";
var answer = "";
await foreach (var response in stream)
{
    if (response.ThoughtSummary != null)
    {
        if (string.IsNullOrEmpty(thoughts))
        {
            Debug.Log("--- Thoughts Summary ---");
        }
        Debug.Log(response.ThoughtSummary);
        thoughts += response.ThoughtSummary;
    }
    if (response.Text != null)
    {
        if (string.IsNullOrEmpty(answer))
        {
            Debug.Log("--- Answer ---");
        }
        Debug.Log(response.Text);
        answer += response.Text;
    }
}



חתימות של מחשבות

כשמשתמשים בחשיבה באינטראקציות מרובות תפניות, למודל אין גישה להקשר של המחשבה מתפניות קודמות. עם זאת, אם אתם משתמשים בקריאה להפעלת פונקציות, אתם יכולים להשתמש בחתימות של שלבים בתהליך החשיבה כדי לשמור על הקשר של שלבים בתהליך החשיבה לאורך השיחה. חתימות החשיבה הן ייצוגים מוצפנים של תהליך החשיבה הפנימי של המודל, והן זמינות כשמשתמשים בחשיבה וגם בקריאה להפעלת פונקציות. באופן ספציפי, חתימות מחשבה נוצרות כש:

  • החשיבה מופעלת והמסקנות נוצרות.
  • הבקשה כוללת הצהרות על פונקציות.

כדי ליהנות מהיתרונות של חתימות מחשבה, משתמשים בקריאה להפעלת פונקציות כרגיל. ערכות ה-SDK של Firebase AI Logic מפשטות את התהליך על ידי ניהול המצב וטיפול אוטומטי בחתימות המחשבה. ערכות ה-SDK מעבירות באופן אוטומטי את כל חתימות המחשבה שנוצרו בין קריאות עוקבות של sendMessage או sendMessageStream בסשן Chat.



תמחור וספירה של טוקנים של חשיבה

התמחור של טוקנים של חשיבה זהה לתמחור של טוקנים של פלט טקסט. אם מפעילים סיכומי חשיבה, הם נחשבים לטוקנים של חשיבה והמחיר שלהם נקבע בהתאם.

אפשר להפעיל מעקב אחרי שימוש בתכונות AI במסוף Firebase כדי לעקוב אחרי מספר הטוקנים של החשיבה בבקשות שבהן החשיבה מופעלת.

אפשר לקבל את המספר הכולל של טוקנים של חשיבה מהשדה thoughtsTokenCount במאפיין usageMetadata של התשובה:

Swift

// ...

let response = try await model.generateContent("Why is the sky blue?")

if let usageMetadata = response.usageMetadata {
  print("Thoughts Token Count: \(usageMetadata.thoughtsTokenCount)")
}

Kotlin

// ...

val response = model.generateContent("Why is the sky blue?")

response.usageMetadata?.let { usageMetadata ->
    println("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}")
}

Java

// ...

ListenableFuture<GenerateContentResponse> response =
    model.generateContent("Why is the sky blue?");

Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
    @Override
    public void onSuccess(GenerateContentResponse result) {
        String usageMetadata = result.getUsageMetadata();
        if (usageMetadata != null) {
            System.out.println("Thoughts Token Count: " +
                usageMetadata.getThoughtsTokenCount());
        }
    }

    @Override
    public void onFailure(Throwable t) {
        t.printStackTrace();
    }
}, executor);

Web

// ...

const response = await model.generateContent("Why is the sky blue?");

if (response?.usageMetadata?.thoughtsTokenCount != null) {
    console.log(`Thoughts Token Count: ${response.usageMetadata.thoughtsTokenCount}`);
}

Dart

// ...

final response = await model.generateContent(
  Content.text("Why is the sky blue?"),
]);

if (response?.usageMetadata case final usageMetadata?) {
  print("Thoughts Token Count: ${usageMetadata.thoughtsTokenCount}");
}

Unity

// ...

var response = await model.GenerateContentAsync("Why is the sky blue?");

if (response.UsageMetadata != null)
{
    UnityEngine.Debug.Log($"Thoughts Token Count: {response.UsageMetadata?.ThoughtsTokenCount}");
}

מידע נוסף על טוקנים זמין במדריך לספירת טוקנים.