يمكن لنماذج Gemini 2.5 استخدام "عملية تفكير" داخلية تحسّن بشكل كبير قدراتها على الاستدلال والتخطيط المتعدّد الخطوات، ما يجعلها فعّالة للغاية في المهام المعقّدة، مثل الترميز والرياضيات المتقدّمة وتحليل البيانات.
تقدّم نماذج التفكير الإعدادات والخيارات التالية:
مدة الاستدلال: يمكنك ضبط مدة الاستدلال التي يمكن أن يستغرقها النموذج باستخدام مدة الاستدلال. ويكون هذا الإعداد مهمًا بشكل خاص إذا كانت الأولوية لتقليل وقت الاستجابة أو التكلفة. راجِع أيضًا مقارنة صعوبات المهام لتحديد مدى حاجة النموذج إلى قدرته على التفكير.
ملخّصات الأفكار: يمكنك تفعيل ملخّصات الأفكار لتضمينها في الردّ الذي تم إنشاؤه. هذه الملخّصات هي إصدارات مركّبة من أفكار النموذج الأولية، وتقدّم معلومات عن عملية الاستدلال الداخلية التي يتّبعها النموذج.
توقيعات الأفكار: تتولّى حِزم تطوير البرامج (SDK) في Firebase AI Logic معالجة توقيعات الأفكار تلقائيًا، ما يضمن وصول النموذج إلى سياق الفكرة من الجولات السابقة، خاصةً عند استخدام ميزة "استدعاء الدوال".
احرص على مراجعة أفضل الممارسات وإرشادات الطلبات لاستخدام نماذج التفكير.
استخدام نموذج تفكير
استخدِم نموذجًا للتفكير تمامًا كما تستخدم أي نموذج 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 console لمراقبة عدد الرموز المميزة التي يتم إنشاؤها أثناء التفكير ووقت الاستجابة لطلباتك التي تم تفعيل ميزة "التفكير" فيها. وفي حال تفعيل ملخّصات الأفكار، ستظهر في وحدة التحكّم حيث يمكنك الاطّلاع على تفاصيل أسباب النموذج لمساعدتك في تصحيح الأخطاء وتحسين طلباتك.
التحكّم في ميزانية التفكير
للتحكّم في مقدار التفكير الذي يمكن أن تقوم به النماذج لإنشاء ردودها، يمكنك تحديد عدد الرموز المميزة لميزانية التفكير التي يُسمح لها باستخدامها.
يمكنك ضبط مدة الاستدلال يدويًا في الحالات التي قد تحتاج فيها إلى عدد أكبر أو أقل من الرموز المميزة مقارنةً بمدة الاستدلال التلقائية. يمكنك الاطّلاع على إرشادات أكثر تفصيلاً حول مدى تعقيد المهام والميزانيات المقترَحة لاحقًا في هذا القسم. في ما يلي بعض الإرشادات العامة:
- اضبط ميزانية تفكير منخفضة إذا كان وقت الاستجابة مهمًا أو للمهام الأقل تعقيدًا
- تحديد ميزانية تفكير عالية للمهام الأكثر تعقيدًا
ضبط ميزانية التفكير
انقر على مزوّد 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
. يمكن للنموذج استخدام أكبر عدد ممكن من الرموز المميزة يراه مناسبًا، وذلك بما لا يتجاوز الحد الأقصى لقيمة الرموز المميزة المذكورة أعلاه.
تعقيد المهمة
المهام السهلة: يمكن إيقاف ميزة "التفكير"
الطلبات المباشرة التي لا تتطلّب تحليلًا معقّدًا، مثل استرجاع المعلومات أو التصنيف أمثلة:- "أين تأسّست شركة DeepMind؟"
- "هل تطلب هذه الرسالة الإلكترونية عقد اجتماع أم أنّها تقدّم معلومات فقط؟"
المهام المتوسطة: الميزانية التلقائية أو بعض ميزانية التفكير الإضافية مطلوبة
طلبات شائعة تستفيد من درجة من المعالجة خطوة بخطوة أو فهم أعمق. أمثلة:- "أريد تشبيهًا بين عملية التمثيل الضوئي ومرحلة النمو".
- "قارِن بين السيارات الكهربائية والسيارات الهجينة".
المهام الصعبة التي قد تتطلّب أقصى مدة استدلال
التحديات المعقّدة حقًا، مثل حلّ مسائل رياضية معقّدة أو مهام تتعلّق بالبرمجة تتطلّب هذه الأنواع من المهام أن يستفيد النموذج من جميع قدراته في التفكير والتخطيط، وغالبًا ما تتضمّن العديد من الخطوات الداخلية قبل تقديم إجابة. أمثلة:- "حلّ المسألة 1 في مسابقة AIME لعام 2025: أوجد مجموع كل الأعداد الصحيحة الأساسية b > 9 التي يكون فيها 17b قاسمًا لـ 97b".
- "أريد كتابة رمز Python لتطبيق ويب يعرض بيانات سوق الأسهم في الوقت الفعلي، بما في ذلك مصادقة المستخدم. اجعلها فعّالة قدر الإمكان".
تضمين ملخّصات الأفكار في الردود
ملخّصات الأفكار هي نُسخ مركّزة من الأفكار الأولية التي يطرحها النموذج، وتقدّم إحصاءات حول عملية الاستدلال الداخلية التي يتّبعها النموذج.
في ما يلي بعض الأسباب التي تدفع إلى تضمين ملخّصات الأفكار في الردود:
يمكنك عرض ملخّصات الأفكار في واجهة مستخدم تطبيقك أو إتاحتها للمستخدمين. يتم عرض ملخّص الفكرة كجزء منفصل في الردّ، ما يتيح لك التحكّم بشكل أكبر في طريقة استخدامه في تطبيقك.
إذا فعّلت أيضًا ميزة مراقبة الذكاء الاصطناعي في Firebase console، ستظهر ملخّصات الأفكار في وحدة التحكّم حيث يمكنك فحص الاستدلال المفصّل للنموذج لمساعدتك في تصحيح الأخطاء وتحسين طلباتك.
في ما يلي بعض الملاحظات الرئيسية حول ملخّصات الأفكار:
لا يتم التحكّم في ملخّصات الأفكار من خلال ميزانيات التفكير (تنطبق الميزانيات فقط على الأفكار الأولية التي يقدّمها النموذج). ومع ذلك، إذا كانت ميزة التفكير غير مفعّلة، لن يعرض النموذج ملخّصًا للأفكار.
تُعتبر ملخّصات الأفكار جزءًا من الردود العادية التي ينشئها النموذج، ويتم احتسابها كرموز مميّزة للناتج.
تفعيل ملخّصات الأفكار
انقر على مزوّد Gemini API لعرض المحتوى والرمز الخاصين بالمزوّد على هذه الصفحة. |
يمكنك تفعيل ملخّصات الأفكار من خلال ضبط includeThoughts
على "صحيح" في إعدادات النموذج. يمكنك بعد ذلك الوصول إلى الملخّص من خلال التحقّق من الحقل 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
.
التسعير واحتساب الرموز المميزة للتفكير
تستخدم الرموز المميزة الخاصة بالتفكير التسعير نفسه المستخدَم في الرموز المميزة الخاصة بإخراج النص. في حال تفعيل ملخّصات الأفكار، يتم اعتبارها رموزًا مميّزة للتفكير ويتم تحديد سعرها وفقًا لذلك.
يمكنك تفعيل ميزة مراقبة الذكاء الاصطناعي في Firebase Console لمراقبة عدد الرموز المميزة الخاصة بالتفكير للطلبات التي تم تفعيل ميزة التفكير فيها.
يمكنك الحصول على إجمالي عدد الرموز المميزة للتفكير من الحقل 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}");
}
يمكنك الاطّلاع على مزيد من المعلومات حول الرموز المميزة في دليل الرموز المميزة الخاصة بالعدد.