فکر کردن

مدل‌های Gemini 2.5 می‌توانند از یک «فرایند تفکر» داخلی استفاده کنند که به‌طور قابل‌توجهی توانایی‌های استدلال و برنامه‌ریزی چند مرحله‌ای آن‌ها را بهبود می‌بخشد و آن‌ها را برای کارهای پیچیده‌ای مانند کدنویسی، ریاضیات پیشرفته و تجزیه و تحلیل داده‌ها بسیار مؤثر می‌سازد.

مدل های تفکر تنظیمات و گزینه های زیر را ارائه می دهند:

  • بودجه فکری : می‌توانید با استفاده از یک بودجه فکری ، مقدار «تفکر» را که یک مدل می‌تواند انجام دهد، پیکربندی کنید. اگر کاهش تأخیر یا هزینه در اولویت باشد، این پیکربندی بسیار مهم است. همچنین، مقایسه دشواری های کار را مرور کنید تا تصمیم بگیرید که یک مدل چقدر ممکن است به توانایی تفکر خود نیاز داشته باشد.

  • خلاصه افکار : می‌توانید خلاصه‌های فکری را فعال کنید تا در پاسخ ایجاد شده گنجانده شوند. این خلاصه ها نسخه های ترکیب شده ای از افکار خام مدل هستند و بینش هایی را در مورد فرآیند استدلال درونی مدل ارائه می دهند.

  • امضاهای فکری : Firebase AI Logic SDK به طور خودکار امضاهای فکری را برای شما مدیریت می کند، که تضمین می کند که مدل به طور خاص در هنگام استفاده از فراخوانی تابع، به زمینه فکری از چرخش های قبلی دسترسی دارد.

مطمئن شوید که بهترین شیوه ها و راهنمایی های ضروری برای استفاده از مدل های تفکر را مرور کرده اید.

از یک مدل تفکر استفاده کنید

از یک مدل تفکر درست مانند سایر مدل های Gemini استفاده کنید (ارائه دهنده Gemini API انتخابی خود را راه اندازی کنید، یک نمونه GenerativeModel ایجاد کنید، و غیره). این مدل‌ها را می‌توان برای وظایف تولید متن یا کد، مانند تولید خروجی ساختاریافته یا تجزیه و تحلیل ورودی‌های چندوجهی (مانند تصاویر ، ویدئو ، صدا یا فایل‌های PDF ) استفاده کرد. هنگام پخش جریانی خروجی، حتی می توانید از مدل های تفکر استفاده کنید.

مدل هایی که از این قابلیت پشتیبانی می کنند

تنها مدل های Gemini 2.5 از این قابلیت پشتیبانی می کنند.

  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite

بهترین شیوه ها و راهنمایی های ضروری برای استفاده از مدل های تفکر

توصیه می‌کنیم درخواست خود را در Google AI Studio یا Vertex AI Studio آزمایش کنید تا بتوانید فرآیند تفکر کامل را مشاهده کنید. می‌توانید مناطقی را که ممکن است مدل به اشتباه رفته را شناسایی کنید تا بتوانید درخواست‌های خود را برای دریافت پاسخ‌های منسجم‌تر و دقیق‌تر اصلاح کنید.

با یک اعلان کلی که نتیجه مورد نظر را توصیف می کند، شروع کنید و افکار اولیه مدل را در مورد چگونگی تعیین پاسخ خود مشاهده کنید. اگر پاسخ آنطور که انتظار می رود نیست، با استفاده از هر یک از تکنیک های تحریک زیر به مدل کمک کنید تا پاسخ بهتری ایجاد کند:

  • دستورالعمل های گام به گام را ارائه دهید
  • چندین مثال از جفت ورودی-خروجی ارائه دهید
  • برای نحوه بیان و قالب بندی خروجی ها و پاسخ ها راهنمایی ارائه دهید
  • مراحل تأیید مشخصی را ارائه دهید

علاوه بر تلقین، استفاده از این توصیه ها را در نظر بگیرید:

  • دستورالعمل‌های سیستم را تنظیم کنید، که مانند یک «مقدمه» هستند که قبل از اینکه مدل در معرض هر دستورالعمل دیگری از طرف درخواست یا کاربر نهایی قرار گیرد، اضافه می‌کنید. آنها به شما اجازه می دهند رفتار مدل را بر اساس نیازهای خاص و موارد استفاده خود هدایت کنید.

  • یک بودجه فکری تنظیم کنید تا میزان تفکر مدل را پیکربندی کنید. اگر بودجه پایینی تعیین کنید، مدل به پاسخ خود "بیش از حد" فکر نمی کند. اگر بودجه بالایی تعیین می کنید، در صورت نیاز مدل می تواند بیشتر فکر کند. تنظیم بودجه فکری همچنین مقدار بیشتری از حد خروجی توکن را برای پاسخ واقعی ذخیره می کند.

  • مانیتورینگ هوش مصنوعی را در کنسول Firebase فعال کنید تا بر تعداد نشانه‌های تفکر و تأخیر درخواست‌هایتان که تفکر فعال است نظارت کنید. و اگر خلاصه‌های فکری را فعال کرده‌اید، در کنسول نمایش داده می‌شوند که می‌توانید استدلال دقیق مدل را بررسی کنید تا به شما در رفع اشکال و اصلاح درخواست‌هایتان کمک کند.

بودجه فکری را کنترل کنید

برای کنترل میزان تفکری که مدل می‌تواند برای ایجاد پاسخ خود انجام دهد، می‌توانید تعداد نشانه‌های بودجه فکری را که مجاز به استفاده از آن هستند را مشخص کنید.

شما می‌توانید به صورت دستی بودجه فکری را در شرایطی تنظیم کنید که ممکن است به توکن‌های بیشتر یا کمتری نسبت به بودجه پیش‌فرض نیاز داشته باشید. راهنمایی دقیق‌تر در مورد پیچیدگی کار و بودجه‌های پیشنهادی را بعداً در این بخش بیابید. در اینجا برخی از راهنمایی های سطح بالا آورده شده است:

  • اگر تأخیر مهم است یا برای کارهای کم‌پیچیده‌تر، بودجه فکری پایینی تعیین کنید
  • بودجه فکری بالایی برای کارهای پیچیده تر تعیین کنید

بودجه فکری را تنظیم کنید

برای مشاهده محتوا و کد ارائه دهنده خاص در این صفحه، روی ارائه دهنده API Gemini خود کلیک کنید.

بودجه فکری را در GenerationConfig به عنوان بخشی از ایجاد نمونه GenerativeModel تنظیم کنید. پیکربندی برای تمام طول عمر نمونه حفظ می شود. اگر می‌خواهید از بودجه‌های فکری متفاوت برای درخواست‌های مختلف استفاده کنید، نمونه‌های GenerativeModel را با هر بودجه پیکربندی کنید.

در ادامه در این بخش با مقادیر بودجه تفکر پشتیبانی شده آشنا شوید.

سویفت

بودجه فکری را در 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,
);

// ...

وحدت

مقادیر پارامترها را در 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 مدل تنظیم کنید، فهرست می کند.

مدل مقدار پیش فرض محدوده موجود برای بودجه فکری ارزش به
غیر فعال کردن تفکر
ارزش به
تفکر پویا را فعال کنید
حداقل ارزش حداکثر مقدار
جمینی 2.5 پرو 8,192 128 32,768 نمی توان خاموش کرد -1
فلش جمینی 2.5 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 : thinkingBudget روی 0 توکن تنظیم کنید
  • Gemini 2.5 Flash-Lite : تفکر به طور پیش فرض غیرفعال است

تفکر پویا را فعال کنید

می توانید با تنظیم thinkingBudget روی -1 به مدل اجازه دهید تصمیم بگیرد که چه زمانی و چقدر فکر می کند (به نام تفکر پویا ). مدل می‌تواند تا حداکثر مقدار توکن خود که در بالا ذکر شده است، از هر تعداد نشانه که مناسب است استفاده کند.

پیچیدگی کار

  • کارهای آسان - فکر کردن را می توان خاموش کرد
    درخواست‌های ساده در مواردی که نیاز به استدلال پیچیده نیست، مانند بازیابی یا طبقه‌بندی واقعیت. مثال ها:

    • "دیپ مایند کجا تاسیس شد؟"
    • "آیا این ایمیل درخواست یک جلسه است یا فقط ارائه اطلاعات؟"
  • وظایف متوسط ​​- بودجه پیش فرض یا مقداری بودجه فکری اضافی مورد نیاز است
    درخواست های رایجی که از درجه ای از پردازش گام به گام یا درک عمیق تر بهره می برند. مثال ها:

    • یک قیاس بین فتوسنتز و بزرگ شدن ایجاد کنید.
    • "مقایسه و مقایسه خودروهای الکتریکی و خودروهای هیبریدی."
  • کارهای سخت - حداکثر بودجه فکری ممکن است مورد نیاز باشد
    چالش‌های واقعاً پیچیده، مانند حل مسائل پیچیده ریاضی یا وظایف کدنویسی. این نوع وظایف مدل را ملزم می‌کند که توانایی‌های استدلال و برنامه‌ریزی کامل خود را درگیر کند، که اغلب شامل بسیاری از مراحل داخلی قبل از ارائه پاسخ است. مثال ها:

    • حل مسئله 1 در AIME 2025: مجموع پایه های اعداد صحیح b > 9 را پیدا کنید که 17b مقسوم علیه 97b است.
    • "کد پایتون را برای یک برنامه وب بنویسید که داده های بازار سهام را در زمان واقعی تجسم می کند، از جمله احراز هویت کاربر. آن را تا حد امکان کارآمد کنید."

خلاصه های فکری را در پاسخ ها بگنجانید

خلاصه های فکری نسخه های ترکیبی از افکار خام مدل هستند و بینش هایی را در مورد فرآیند استدلال درونی مدل ارائه می دهند.

در اینجا چند دلیل برای گنجاندن خلاصه های فکری در پاسخ ها آورده شده است:

  • می توانید خلاصه افکار را در رابط کاربری برنامه خود نمایش دهید یا آنها را در دسترس کاربران خود قرار دهید. خلاصه فکر به عنوان یک بخش جداگانه در پاسخ برگردانده می شود تا کنترل بیشتری بر نحوه استفاده از آن در برنامه خود داشته باشید.

  • اگر نظارت هوش مصنوعی را در کنسول Firebase نیز فعال کنید، خلاصه‌های فکری در کنسول نمایش داده می‌شوند که در آن می‌توانید استدلال دقیق مدل را بررسی کنید تا به شما در رفع اشکال و اصلاح درخواست‌هایتان کمک کند.

در اینجا چند نکته کلیدی در مورد خلاصه افکار آورده شده است:

  • خلاصه های فکری توسط بودجه های فکری کنترل نمی شوند (بودجه ها فقط برای افکار خام مدل اعمال می شود). با این حال، اگر تفکر غیرفعال باشد ، مدل خلاصه فکری را برنمی‌گرداند.

  • خلاصه های فکری بخشی از پاسخ متنی تولید شده منظم مدل در نظر گرفته می شوند و به عنوان نشانه های خروجی به حساب می آیند.

فعال کردن خلاصه افکار

برای مشاهده محتوا و کد ارائه دهنده خاص در این صفحه، روی ارائه دهنده API Gemini خود کلیک کنید.

می‌توانید خلاصه‌های افکار را با تنظیم includeThoughts روی true در پیکربندی مدل خود فعال کنید. سپس می توانید با بررسی فیلد thoughtSummary از پاسخ، به خلاصه دسترسی پیدا کنید.

در اینجا مثالی وجود دارد که نحوه فعال کردن و بازیابی خلاصه های فکری را با پاسخ نشان می دهد:

سویفت

خلاصه‌های فکری را در 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}');
}

وحدت

خلاصه‌های فکری را در 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 پخش جریانی کنید، می توانید خلاصه افکار را مشاهده کنید. این خلاصه‌های متحرک و افزایشی را در طول تولید پاسخ برمی‌گرداند.

سویفت

خلاصه‌های فکری را در 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!;
  }
}

وحدت

خلاصه‌های فکری را در 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;
    }
}

امضاهای فکری را درک کنید

هنگام استفاده از تفکر در تعاملات چند نوبتی، مدل به بافت فکری از نوبت های قبلی دسترسی ندارد. با این حال، اگر از فراخوانی تابع استفاده می‌کنید، می‌توانید از امضاهای فکری برای حفظ بافت فکری در سراسر نوبت استفاده کنید. امضاهای فکر بازنمایی رمزگذاری شده ای از فرآیند فکر داخلی مدل هستند و هنگام استفاده از تفکر و فراخوانی تابع در دسترس هستند. به طور خاص، امضاهای فکری زمانی ایجاد می شوند که:

  • تفکر فعال می شود و افکار تولید می شوند.
  • درخواست شامل اعلان های عملکرد است.

برای استفاده از امضاهای فکری، از فراخوانی تابع به طور معمول استفاده کنید. Firebase AI Logic SDK فرآیند را با مدیریت وضعیت و مدیریت خودکار امضاهای فکری برای شما ساده می کند. SDK ها به طور خودکار هر گونه امضای فکری ایجاد شده را بین تماس های sendMessage یا sendMessageStream بعدی در یک جلسه Chat ارسال می کنند.

قیمت گذاری و شمارش نشانه های تفکر

توکن‌های تفکر از همان قیمت‌گذاری توکن‌های خروجی متن استفاده می‌کنند. اگر خلاصه‌های فکری را فعال کنید، آنها به عنوان نشانه‌های تفکر در نظر گرفته می‌شوند و بر اساس آن قیمت‌گذاری می‌شوند.

می‌توانید نظارت بر هوش مصنوعی را در کنسول Firebase فعال کنید تا بر تعداد توکن‌های فکری برای درخواست‌هایی که تفکر فعال است نظارت کند.

می‌توانید تعداد کل نشانه‌های فکری را از قسمت thoughtsTokenCount در ویژگی usageMetadata پاسخ دریافت کنید:

سویفت

// ...

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}");
}

وحدت

// ...

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

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

در راهنمای شمارش نشانه‌ها درباره نشانه‌ها بیشتر بیاموزید.