Модели Gemini 2.5 могут использовать внутренний «мыслительный процесс», который значительно улучшает их способности к рассуждению и многоэтапному планированию, что делает их крайне эффективными для решения таких сложных задач, как кодирование, высшая математика и анализ данных.
Модели мышления предлагают следующие конфигурации и опции:
Бюджет мышления : вы можете настроить объём «мышления», который может выполнить модель, используя бюджет мышления . Эта настройка особенно важна, если приоритетом является сокращение задержки или стоимости. Также просмотрите сравнение сложности задач , чтобы определить, насколько необходимый объём мыслительных способностей модели.
Краткое содержание мыслей : вы можете включить краткое содержание мыслей в сгенерированный ответ. Эти краткое содержание представляют собой синтезированные версии исходных мыслей модели и дают представление о её внутреннем процессе рассуждений.
Сигнатуры мыслей : Пакеты Firebase AI Logic SDK автоматически обрабатывают сигнатуры мыслей , что гарантирует модели доступ к контексту мыслей из предыдущих ходов, особенно при использовании вызова функций.
Обязательно ознакомьтесь с лучшими практиками и рекомендациями по использованию моделей мышления.
Используйте модель мышления
Используйте модель мышления так же, как и любую другую модель Gemini (инициализируйте выбранного поставщика API Gemini , создайте экземпляр 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 Flash : установите
thinkingBudget
на0
токенов - Gemini 2.5 Flash‑Lite : мышление отключено по умолчанию
Включить динамическое мышление
Вы можете позволить модели самостоятельно решать, когда и сколько думать (это называется динамическим мышлением ), установив значение thinkingBudget
равным -1
. Модель может использовать столько токенов, сколько сочтет нужным, вплоть до максимального значения токена, указанного выше.
Сложность задачи
Легкие задачи — мышление можно отключить
Простые запросы, не требующие сложных рассуждений, например, поиск фактов или классификация. Примеры:- «Где была основана компания DeepMind?»
- «Это электронное письмо с просьбой о встрече или просто с информацией?»
Средние задачи — требуется бюджет по умолчанию или дополнительный бюджет на обдумывание
Распространенные запросы, которые требуют пошаговой обработки или более глубокого понимания. Примеры:- «Проведите аналогию между фотосинтезом и взрослением».
- «Сравните и сопоставьте электромобили и гибридные автомобили».
Сложные задачи — может потребоваться максимальный бюджет для размышлений
По-настоящему сложные задачи, такие как решение сложных математических задач или задач программирования. Такие задачи требуют от модели полного задействования её возможностей рассуждения и планирования, часто требуя множества внутренних этапов, прежде чем будет выдан ответ. Примеры:- Решите задачу 1 в AIME 2025: найдите сумму всех целых чисел с основаниями b > 9, для которых 17b является делителем 97b.
- «Напишите код Python для веб-приложения, которое визуализирует данные фондового рынка в режиме реального времени, включая аутентификацию пользователей. Сделайте его максимально эффективным».
Включайте краткое изложение мыслей в ответы
Конспекты мыслей представляют собой синтезированные версии исходных мыслей модели и дают представление о внутреннем процессе рассуждений модели.
Вот несколько причин включать в ответы краткое изложение мыслей:
Вы можете отобразить краткое содержание мыслей в пользовательском интерфейсе вашего приложения или сделать его доступным для пользователей. Краткое содержание мыслей возвращается в виде отдельной части ответа, что позволяет вам лучше контролировать его использование в вашем приложении.
Если вы также включите мониторинг ИИ в консоли 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}");
}
# Example Response:
# Okay, let's solve the quadratic equation x² + 4x + 4 = 0.
# ...
# **Answer:**
# The solution to the equation x² + 4x + 4 = 0 is x = -2. This is a repeated root (or a root with multiplicity 2).
# Example Thought Summary:
# **My Thought Process for Solving the Quadratic Equation**
#
# Alright, let's break down this quadratic, x² + 4x + 4 = 0. First things first:
# it's a quadratic; the x² term gives it away, and we know the general form is
# ax² + bx + c = 0.
#
# So, let's identify the coefficients: a = 1, b = 4, and c = 4. Now, what's the
# most efficient path to the solution? My gut tells me to try factoring; it's
# often the fastest route if it works. If that fails, I'll default to the quadratic
# formula, which is foolproof. Completing the square? It's good for deriving the
# formula or when factoring is difficult, but not usually my first choice for
# direct solving, but it can't hurt to keep it as an option.
#
# Factoring, then. I need to find two numbers that multiply to 'c' (4) and add
# up to 'b' (4). Let's see... 1 and 4 don't work (add up to 5). 2 and 2? Bingo!
# They multiply to 4 and add up to 4. This means I can rewrite the equation as
# (x + 2)(x + 2) = 0, or more concisely, (x + 2)² = 0. Solving for x is now
# trivial: x + 2 = 0, thus x = -2.
#
# Okay, just to be absolutely certain, I'll run the quadratic formula just to
# double-check. x = [-b ± √(b² - 4ac)] / 2a. Plugging in the values, x = [-4 ±
# √(4² - 4 * 1 * 4)] / (2 * 1). That simplifies to x = [-4 ± √0] / 2. So, x =
# -2 again - a repeated root. Nice.
#
# Now, let's check via completing the square. Starting from the same equation,
# (x² + 4x) = -4. Take half of the b-value (4/2 = 2), square it (2² = 4), and
# add it to both sides, so x² + 4x + 4 = -4 + 4. Which simplifies into (x + 2)²
# = 0. The square root on both sides gives us x + 2 = 0, therefore x = -2, as
# expected.
#
# Always, *always* confirm! Let's substitute x = -2 back into the original
# equation: (-2)² + 4(-2) + 4 = 0. That's 4 - 8 + 4 = 0. It checks out.
#
# Conclusion: the solution is x = -2. Confirmed.
Конспекты мыслей в прямом эфире
Вы также можете просматривать краткие изложения мыслей, если выберете потоковую передачу ответа с помощью 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}");
}
Дополнительную информацию о токенах можно найти в руководстве по подсчету токенов .