همه مدلهای Gemini از یک «فرایند تفکر» داخلی استفاده میکنند که تواناییهای استدلال و برنامهریزی چندمرحلهای آنها را بهطور قابلتوجهی بهبود میبخشد و آنها را برای کارهای پیچیده مانند کدنویسی، ریاضیات پیشرفته، و تحلیل دادهها بسیار مؤثر میسازد.
مدلهای اندیشگر پیکربندیها و گزینههای زیر را ارائه میدهند:
کنترل کردن میزان تفکر
میتوانید سطوح تفکر را پیکربندی کنید تا میزان «تفکر» مدل را کنترل کنید. این پیکربندی بهویژه زمانی مهم است که کاهش تأخیر یا هزینه در اولویت باشد. همچنین، مقایسه دشواری تکلیف را مرور کنید تا تصمیم بگیرید که مدل چقدر ممکن است به قابلیت تفکر خود نیاز داشته باشد.دریافت خلاصه افکار
میتوانید خلاصه افکار را فعال کنید تا در پاسخ تولیدشده گنجانده شود. این خلاصهها نسخههای ترکیبی از افکار خام مدل هستند و بینشهایی درباره فرایند استدلال داخلی مدل ارائه میدهند.مدیریت امضاهای فکری
Firebase AI Logic کیتهای توسعه نرمافزار بهطور خودکار امضاهای فکری را برای شما مدیریت میکنند، که تضمین میکند مدل به بافتار فکری از نوبتهای قبلی بهویژه هنگام استفاده از فراخوانی تابع دسترسی دارد.
حتماً روالهای مطلوب و راهنمای پیامواره را برای استفاده از مدلهای استدلالی مرور کنید.
روالهای مطلوب و راهنمای پیامواره برای استفاده از مدلهای استدلالی
توصیه میکنیم پیاموارهتان را در Google AI Studio یا Agent Studio آزمایش کنید تا بتوانید فرایند کامل تفکر را مشاهده کنید. میتوانید بخشهایی را که ممکن است مدل در آنها اشتباه کرده باشد شناسایی کنید تا بتوانید پیاموارههایتان را برای دریافت پاسخهای دقیقتر و منسجمتر پالایش کنید.
با پیاموارهای کلی شروع کنید که نتیجه مطلوب را توصیف میکند و افکار اولیه مدل را درباره نحوه تعیین پاسخ آن مشاهده کنید. اگر پاسخ مطابق انتظار نیست، بااستفاده از هرکدام از فنهای پیامواره زیر به مدل کمک کنید پاسخ بهتری تولید کند:
- دستورالعملهای گامبهگام ارائه دهید
- ارائه چند نمونه از جفتهای ورودی-خروجی
- راهنماییهایی درباره نحوه عبارتبندی و قالببندی برونداد و پاسخها ارائه دهید
- مراحل درستیسنجی خاصی را ارائه دهید
علاوهبر پیامواره، استفاده از این توصیهها را نیز درنظر بگیرید:
دستورالعملهای سیستم را تنظیم کنید که مانند «مقدمهای» است که قبلاز اینکه مدل درمعرض دستورالعملهای بیشتر ازسوی پیامواره یا کاربر نهایی قرار گیرد، اضافه میکنید. این دستورالعملها به شما امکان میدهند رفتار مدل را براساس نیازها و موارد استفاده خاص خود هدایت کنید.
سطح اندیشیدن را تنظیم کنید تا میزان اندیشیدن مدل را کنترل کنید. اگر آن را روی سطح بالا تنظیم کنید، مدل میتواند درصورت نیاز بیشتر فکر کند. اگر آن را کمتر تنظیم کنید، مدل درباره پاسخش «بیشازحد فکر نمیکند» و همچنین بخش بیشتری از حد برونداد کل کد را برای پاسخ واقعی نگه میدارد و میتواند به کاهش تأخیر و هزینه کمک کند.
پایش هوش مصنوعی را در کنسول 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',
generationConfig: 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 Argongemini-4-argon
|
۱ | (پیشفرض) | ||
Gemini 3.x Flashgemini-3.8-flashgemini-3.7-flashgemini-3.6-flashgemini-3.5-flash
|
gemini-3.8-flashgemini-3.7-flash |
(پیشفرض) | ||
Gemini 3.x Flash‑Litegemini-3.5-flash-litegemini-3.1-flash-lite
|
(پیشفرض) | |||
Gemini Nano Banana 2.1gemini-nano-banana-2.1
|
(پیشفرض) | |||
Gemini Nano Banana 2gemini-3.1-flash-image |
(پیشفرض) | |||
Gemini Nano Banana 2 Litegemini-3.1-flash-lite-image |
(پیشفرض) | |||
Gemini Nano Banana Progemini-3-pro-image
|
(پیشفرض) |
۱ gemini-4-argon، gemini-3.8-flash،
و gemini-3.7-flash از سطح اندیشگر
MINIMAL پشتیبانی نمیکنند. اگر سطح «اندیشگر» را روی
MINIMAL تنظیم کنید، درخواست با خطای ۴۰۰ ناموفق خواهد بود.
بودجههای تفکر (مدلهای Gemini 2.5)
برای کنترل کردن میزان اندیشیدن مدل Gemini 2.5 برای تولید پاسخ، میتوانید بودجه اندیشیدن را برای مقدار دادهواحدهای اندیشیدن که مجاز است استفاده کند مشخص کنید.
پیچیدگی تکلیف برای همه مدلهای اندیشگر
تکالیف آسان — تفکر چندان ضروری نیست
درخواستهای ساده که به استدلال پیچیده نیاز ندارند، مانند بازیابی واقعیت یا طبقهبندی. مثالها:- «DeepMind کجا تأسیس شد؟»
- «آیا این ایمیل درخواست جلسه میکند یا فقط اطلاعات ارائه میدهد؟»
تکالیف متوسط — احتمالاً به مقداری تفکر نیاز است
درخواستهای رایجی که از سطحی از پردازش گامبهگام یا درک عمیقتر بهره میبرند. مثالها:- «بین فتوسنتز و بزرگ شدن قیاس ایجاد کن.»
- «خودروهای برقی و خودروهای ترکیبی را مقایسه و همسنجی کن.»
تکالیف دشوار — ممکن است به حداکثر تفکر نیاز باشد
چالشهای واقعاً پیچیده، مانند حل مسائل پیچیده ریاضی یا تکالیف کدنویسی. این نوع تکالیف مستلزم آن است که مدل از تمام قابلیتهای استدلال و برنامهریزی خود استفاده کند، که اغلب شامل مراحل داخلی زیادی قبل از ارائه پاسخ میشود. مثالها:- «مسئله ۱ در AIME 2025 را حل کن: مجموع همه پایههای عدد صحیح b > 9 را که برای آنها 17b مقسومعلیه 97b است پیدا کن.»
- «کد پایتون برای برنامه وبی بنویس که دادههای بازار سهام را بهصورت همزمان تجسم کند، ازجمله اصالتسنجی کاربر. تا حد امکان کارآمد باشد.»
خلاصه افکار
خلاصههای فکری نسخههای ترکیبی از افکار خام مدل هستند و اطلاعات آماری درباره فرایند استدلال داخلی مدل ارائه میدهند.
در اینجا چند دلیل برای گنجاندن خلاصه افکار در پاسخها آورده شده است:
میتوانید خلاصه فکر را در میانای کاربری برنامهتان نمایش دهید یا آن را برای کاربران دردسترس قرار دهید. خلاصه فکر بهعنوان بخش جداگانهای در پاسخ برگردانده میشود تا کنترل بیشتری بر نحوه استفاده از آن در برنامهتان داشته باشید.
اگر پایش هوشوارهای را در کنسول 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([Content.text('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([Content.text('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;
}
}
امضاهای فکری
هنگام استفاده از تفکر در تعاملات چندنوبتی، مدل به بافت فکری نوبتهای قبلی دسترسی ندارد. بااینحال، اگر از فراخوانی تابع استفاده میکنید، میتوانید از امضاهای فکری برای حفظ بافت فکری درطول نوبتها بهره ببرید. امضاهای فکری نمایشهای رمزگذاریشده از فرایند فکری داخلی مدل هستند و هنگام استفاده از فراخوانی تابع و اندیشگر دردسترس هستند. بهطور خاص، امضاهای فکری زمانی تولید میشوند که:
- اندیشیدن فعال است و افکار تولید میشوند.
- درخواست شامل اعلانهای تابع است.
برای بهرهمندی از امضاهای فکری، از فراخوانی تابع بهصورت عادی استفاده کنید.
Firebase AI Logic کیتهای توسعه نرمافزار با مدیریت کردن وضعیت
و رسیدگی خودکار به امضاهای فکری، فرایند را برای شما ساده میکنند. کیتهای توسعه نرمافزار بهطور خودکار
هرگونه امضای فکری تولیدشده را بین تماسهای بعدی sendMessage یا
sendMessageStream در جلسه Chat منتقل میکنند.
قیمتگذاری و شمارش دادهواحدهای اندیشیدن
دادهواحدهای اندیشیدن از همان قیمتگذاری دادهواحدهای برونداد نوشتاری استفاده میکنند. اگر خلاصههای اندیشیدن را فعال کنید، این خلاصهها بهعنوان دادهواحد اندیشیدن درنظر گرفته میشوند و قیمتگذاری آنها براساس آن انجام میشود.
میتوانید پایش هوشوارهای را در کنسول 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}");
}
در راهنمای شمارش توکنها درباره توکنها بیشتر بدانید.