מודלים של Gemini 2.5 יכולים להשתמש ב "תהליך חשיבה" פנימי שמשפר באופן משמעותי את יכולות ההסקה והתכנון הרב-שלבי שלהם, ולכן הם יעילים מאוד למשימות מורכבות כמו תכנות, מתמטיקה מתקדמת וניתוח נתונים.
מודלים של חשיבה מציעים את ההגדרות והאפשרויות הבאות:
תקציב חשיבה: אפשר להגדיר כמה 'חשיבה' מודל יכול לבצע באמצעות תקציב חשיבה. ההגדרה הזו חשובה במיוחד אם העדיפות היא להפחית את זמן האחזור או את העלות. כדאי גם לעיין בהשוואה בין רמות הקושי של המשימות כדי להחליט כמה יכולת חשיבה המודל צריך.
סיכום התהליך המחשבתי: אתם יכולים להפעיל את האפשרות סיכום התהליך המחשבתי כדי שהתשובה שנוצרה תכלול אותו. הסיכומים האלה הם גרסאות מסונתזות של המחשבות הגולמיות של המודל, והם מספקים תובנות לגבי תהליך ההיגיון הפנימי של המודל.
חתימות של מחשבות: Firebase AI Logic ה-SDKs מטפלים אוטומטית בחתימות של מחשבות, וכך מוודאים שלמודל יש גישה להקשר של המחשבה מהתורות הקודמות, במיוחד כשמשתמשים בקריאה לפונקציה.
חשוב לעיין בשיטות המומלצות ובהנחיות לכתיבת הנחיות לשימוש במודלים של חשיבה.
שימוש במודל חשיבה
משתמשים במודל חושב בדיוק כמו בכל מודל אחר של Gemini
(מאתחלים את ספק Gemini API שבחרתם, יוצרים מופע של GenerativeModel וכו').
אפשר להשתמש במודלים האלה למשימות של יצירת טקסט או קוד, כמו יצירת פלט מובנה או ניתוח של קלט מולטימודאלי (למשל תמונות, סרטונים, אודיו או קובצי PDF).
אתם יכולים אפילו להשתמש במודלים של חשיבה כשאתם משדרים את הפלט בסטרימינג.
מודלים שתומכים ביכולת הזו
רק בדגמי Gemini 3 ו-Gemini 2.5 יש תמיכה באפשרות הזו.
gemini-3-pro-previewgemini-2.5-progemini-2.5-flashgemini-2.5-flash-lite
שיטות מומלצות והנחיות לכתיבת הנחיות לשימוש במודלים של חשיבה
מומלץ לבדוק את ההנחיה ב-Google AI Studio או ב-Vertex AI Studio, שבהם אפשר לראות את תהליך החשיבה המלא. תוכלו לזהות אזורים שבהם יכול להיות שהמודל טעה, כדי לשפר את ההנחיות ולקבל תשובות עקביות ומדויקות יותר.
מתחילים בהנחיה כללית שמתארת את התוצאה הרצויה, ומתבוננים במחשבות הראשוניות של המודל לגבי האופן שבו הוא קובע את התשובה. אם התשובה לא עונה על הציפיות, אפשר להשתמש בטכניקות הבאות לכתיבת הנחיות כדי לעזור למודל ליצור תשובה טובה יותר:
- לתת הוראות מפורטות
- תנו כמה דוגמאות של זוגות קלט-פלט
- הנחיות לגבי הניסוח והפורמט של התוצאות והתשובות
- ציון שלבי אימות ספציפיים
בנוסף להנחיות, כדאי להשתמש בהמלצות הבאות:
מגדירים הוראות למערכת, שהן כמו "הקדמה" שמוסיפים לפני שהמודל נחשף להוראות נוספות מההנחיה או ממשתמש הקצה. ההנחיות מאפשרות לכם לכוון את התנהגות המודל בהתאם לצרכים הספציפיים ולתרחישי השימוש שלכם.
מגדירים תקציב חשיבה כדי לקבוע כמה חשיבה המודל יכול לבצע. אם מגדירים תקציב נמוך, המודל לא יחשוב יותר מדי על התשובה שלו. אם מגדירים תקציב גבוה, המודל יכול לחשוב יותר אם צריך. הגדרת תקציב חשיבה גם שומרת יותר ממגבלת הפלט הכוללת של הטוקנים לתשובה בפועל.
מפעילים את המעקב מבוסס-AI במסוף Firebase כדי לעקוב אחרי מספר האסימונים של החשיבה וזמן האחזור של הבקשות שהופעלה בהן החשיבה. אם הפעלתם את סיכומי המחשבות, הם יוצגו במסוף, שם תוכלו לבדוק את ההיגיון המפורט של המודל כדי לעזור לכם לנפות באגים ולשפר את ההנחיות.
שליטה בתקציב החשיבה
כדי לקבוע כמה מחשבה המודל יכול להשקיע ביצירת התשובה, אפשר לציין את מספר הטוקנים של תקציב החשיבה שהוא יכול להשתמש בהם.
אתם יכולים להגדיר ידנית את תקציב החשיבה במצבים שבהם אתם צריכים יותר או פחות טוקנים מתקציב החשיבה שמוגדר כברירת מחדל. בהמשך הקטע הזה מופיע מידע מפורט יותר על מורכבות המשימה והתקציבים המומלצים. ריכזנו כאן כמה הנחיות כלליות:
- אם זמן האחזור חשוב או אם המשימות פחות מורכבות, כדאי להגדיר תקציב נמוך יותר לחשיבה
- להגדיר תקציב חשיבה גבוה למשימות מורכבות יותר
הגדרת תקציב החשיבה
|
לוחצים על הספק Gemini API כדי לראות בדף הזה תוכן וקוד שספציפיים לספק. |
מגדירים את תקציב החשיבה ב-GenerationConfig כחלק מיצירת מופע GenerativeModel. ההגדרה נשמרת למשך כל חיי המופע. אם רוצים להשתמש בתקציבי חשיבה שונים לבקשות שונות, צריך ליצור מופעים של GenerativeModel שמוגדרים עם כל תקציב.
בהמשך הסעיף הזה מפורט מידע על ערכי תקציב חשיבה נתמכים.
Swift
מגדירים את תקציב החשיבה ב-GenerationConfig במסגרת יצירת מופע GenerativeModel.
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
let generationConfig = GenerationConfig(
thinkingConfig: ThinkingConfig(thinkingBudget: 1024)
)
// Specify the config as part of creating the `GenerativeModel` instance
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
)
// ...
Kotlin
מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
val generationConfig = generationConfig {
thinkingConfig = thinkingConfig {
thinkingBudget = 1024
}
}
// Specify the config as part of creating the `GenerativeModel` instance
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
generationConfig,
)
// ...
Java
מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
ThinkingConfig thinkingConfig = new ThinkingConfig.Builder()
.setThinkingBudget(1024)
.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
);
);
// ...
Web
מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.
// ...
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
const generationConfig = {
thinkingConfig: {
thinkingBudget: 1024
}
};
// Specify the config as part of creating the `GenerativeModel` instance
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", generationConfig });
// ...
Dart
מגדירים את הערכים של הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
final thinkingConfig = ThinkingConfig(thinkingBudget: 1024);
final generationConfig = GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
config: generationConfig,
);
// ...
Unity
מגדירים את ערכי הפרמטרים ב-GenerationConfig כחלק מיצירת מופע של GenerativeModel.
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
var thinkingConfig = new ThinkingConfig(thinkingBudget: 1024);
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
);
// ...
ערכי תקציב נתמכים
בטבלה הבאה מפורטים ערכי התקציב לתהליך החשיבה שאפשר להגדיר לכל מודל על ידי הגדרת thinkingBudget של המודל.
| דגם | ערך ברירת מחדל | הטווח הזמין לתקציב החשיבה |
הערך ש משבית את החשיבה |
ערך ל הפעלת חשיבה דינמית |
|
|---|---|---|---|---|---|
| ערך מינימלי | ערך מקסימלי | ||||
| Gemini 2.5 Pro | 8,192 |
128 |
32,768 |
אי אפשר להשבית את האפשרות הזו | -1 |
| Gemini 2.5 Flash | 8,192 |
1 |
24,576 |
0 |
-1 |
| Gemini 2.5 Flash‑Lite | 0(החשיבה מושבתת כברירת מחדל) |
512 |
24,576 |
0(או לא להגדיר תקציב חשיבה בכלל) |
-1 |
השבתת תהליך החשיבה
למשימות פשוטות יותר, יכולת החשיבה לא נחוצה, והסקת מסקנות מסורתית מספיקה. או אם חשוב לכם להקטין את זמן האחזור, יכול להיות שלא תרצו שהמודל יקדיש יותר זמן מהנדרש ליצירת תשובה.
במקרים כאלה, אפשר להשבית את התכונה 'חשיבה':
- Gemini 2.5 Pro: חשיבה לא ניתנת להשבתה
- Gemini 2.5 Flash: הגדרת
thinkingBudgetל-0טוקנים - Gemini 2.5 Flash‑Lite: החשיבה מושבתת כברירת מחדל
הפעלת חשיבה דינמית
כדי לאפשר למודל להחליט מתי וכמה לחשוב (מה שנקרא חשיבה דינמית), צריך להגדיר את thinkingBudget לערך -1. המודל יכול להשתמש בכמה טוקנים שהוא מחליט שמתאים, עד הערך המקסימלי של הטוקנים שמופיע למעלה.
מורכבות המשימה
משימות פשוטות – אפשר להשבית את החשיבה
בקשות פשוטות שלא דורשות נימוק מורכב, כמו אחזור עובדות או סיווג. דוגמאות:- "Where was DeepMind founded?"
- "האם האימייל הזה הוא הזמנה לפגישה או שהוא רק מספק מידע?"
משימות בינוניות – נדרש תקציב ברירת המחדל או תקציב נוסף למחשבה
בקשות נפוצות שמומלץ להשתמש בהן בעיבוד שלב אחר שלב או בהבנה מעמיקה יותר. דוגמאות:- "צור אנלוגיה בין פוטוסינתזה לבין התבגרות".
- "השווה והבדל בין מכוניות חשמליות למכוניות היברידיות".
משימות קשות – יכול להיות שיידרש תקציב חשיבה מקסימלי
אתגרים מורכבים באמת, כמו פתרון בעיות מתמטיות מורכבות או משימות תכנות. סוגי המשימות האלה דורשים מהמודל להשתמש בכל יכולות ההסקה והתכנון שלו, ולעיתים קרובות כוללים הרבה שלבים פנימיים לפני מתן תשובה. דוגמאות:- "Solve problem 1 in AIME 2025: Find the sum of all integer bases b > 9 for which 17b is a divisor of 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}");
}
מידע נוסף על טוקנים זמין במדריך לספירת טוקנים.