Gemini 2.5 possono utilizzare un "processo di pensiero" interno che migliora significativamente le loro capacità di ragionamento e pianificazione in più passaggi, rendendoli altamente efficaci per attività complesse come la programmazione, la matematica avanzata e l'analisi dei dati.
I modelli di pensiero offrono le seguenti configurazioni e opzioni:
Budget di pensiero: puoi configurare la quantità di "pensiero" che un modello può fare utilizzando un budget di pensiero. Questa configurazione è particolarmente importante se la priorità è ridurre la latenza o i costi. Inoltre, consulta il confronto tra le difficoltà delle attività per decidere quanto un modello potrebbe aver bisogno della sua capacità di pensiero.
Riepiloghi dei pensieri: puoi attivare i riepiloghi dei pensieri da includere nella risposta generata. Questi riepiloghi sono versioni sintetizzate dei pensieri grezzi del modello e offrono informazioni sul processo di ragionamento interno del modello.
Firme del pensiero: gli SDK Firebase AI Logic gestiscono automaticamente le firme del pensiero per te, il che garantisce che il modello abbia accesso al contesto del pensiero dei turni precedenti, in particolare quando si utilizza la chiamata di funzioni.
Assicurati di leggere le best practice e le indicazioni per i prompt per l'utilizzo dei modelli di pensiero.
Utilizzare un modello di pensiero
Utilizza un modello di pensiero proprio come faresti con qualsiasi altro modello Gemini
(inizializza il provider Gemini API scelto, crea un'istanza GenerativeModel
e così via).
Questi modelli possono essere utilizzati per attività di generazione di testo o codice, ad esempio
generare output strutturati
o analizzare input multimodali (come
immagini,
video,
audio,
o
PDF).
Puoi anche utilizzare modelli di pensiero durante lo streaming dell'output.
Modelli che supportano questa funzionalità
Solo i modelli Gemini 2.5 supportano questa funzionalità.
gemini-2.5-pro
gemini-2.5-flash
gemini-2.5-flash-lite
Best practice e indicazioni per l'utilizzo dei modelli di pensiero
Ti consigliamo di testare il prompt in Google AI Studio o Vertex AI Studio dove puoi visualizzare l'intero processo di pensiero. Puoi identificare le aree in cui il modello potrebbe aver sbagliato, in modo da poter perfezionare i prompt per ottenere risposte più coerenti e accurate.
Inizia con un prompt generale che descriva il risultato desiderato e osserva i pensieri iniziali del modello su come determina la sua risposta. Se la risposta non è quella che ti aspettavi, aiuta il modello a generare una risposta migliore utilizzando una delle tecniche di prompting seguenti:
- Fornire istruzioni passo passo
- Fornisci diversi esempi di coppie input-output
- Fornisci indicazioni su come devono essere formulate e formattate l'output e le risposte.
- Fornisci passaggi di verifica specifici
Oltre ai prompt, valuta la possibilità di utilizzare questi consigli:
Imposta le istruzioni di sistema, che sono come un "preambolo" che aggiungi prima che il modello venga esposto a ulteriori istruzioni dal prompt o dall'utente finale. Ti consentono di indirizzare il comportamento del modello in base alle tue esigenze e ai tuoi casi d'uso specifici.
Imposta un budget di pensiero per configurare la quantità di pensiero che il modello può fare. Se imposti un budget basso, il modello non "penserà troppo" alla sua risposta. Se imposti un budget elevato, il modello può pensare di più, se necessario. L'impostazione di un budget per il pensiero riserva anche una parte maggiore del limite totale di token di output per la risposta effettiva.
Abilita AI Monitoring nella console Firebase per monitorare il conteggio dei token di pensiero e la latenza delle richieste per cui è abilitata la funzionalità di pensiero. Se hai attivato i riepiloghi dei pensieri, questi verranno visualizzati nella console, dove potrai esaminare il ragionamento dettagliato del modello per eseguire il debug e perfezionare i prompt.
Controllare il budget di pensiero
Per controllare la quantità di pensiero che il modello può utilizzare per generare la risposta, puoi specificare il numero di token del budget di pensiero che può utilizzare.
Puoi impostare manualmente il budget di pensiero nelle situazioni in cui potresti aver bisogno di più o meno token rispetto al budget di pensiero predefinito. Più avanti in questa sezione troverai indicazioni più dettagliate sulla complessità delle attività e sui budget suggeriti. Ecco alcune indicazioni di alto livello:
- Imposta un budget di pensiero basso se la latenza è importante o per attività meno complesse
- Imposta un budget di pensiero elevato per le attività più complesse
Impostare il budget di pensiero
Fai clic sul tuo fornitore Gemini API per visualizzare i contenuti e il codice specifici del fornitore in questa pagina. |
Imposta il budget di pensiero in un GenerationConfig
durante la creazione dell'istanza
GenerativeModel
. La configurazione viene mantenuta per l'intera durata dell'istanza. Se vuoi utilizzare budget di pensiero diversi per richieste diverse, crea istanze GenerativeModel
configurate con ciascun budget.
Scopri di più sui valori del budget di pianificazione supportati più avanti in questa sezione.
Swift
Imposta il budget di pensiero in un
GenerationConfig
durante la creazione di un'istanza 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
Imposta i valori dei parametri in un
GenerationConfig
durante la creazione di un'istanza 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
Imposta i valori dei parametri in un
GenerationConfig
durante la creazione di un'istanza 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
Imposta i valori dei parametri in un
GenerationConfig
durante la creazione di un'istanza 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
Imposta i valori dei parametri in un
GenerationConfig
durante la creazione di un'istanza 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
Imposta i valori dei parametri in un
GenerationConfig
durante la creazione di un'istanza 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
);
// ...
Valori del budget di pensiero supportati
La tabella seguente elenca i valori del budget di pensiero che puoi impostare per ogni modello configurando il thinkingBudget
del modello.
Modello | Valore predefinito | Intervallo disponibile per il budget di pensiero |
Valore per disattivare il pensiero |
Valore per favorire il pensiero dinamico |
|
---|---|---|---|---|---|
Valore minimo | Valore massimo | ||||
Gemini 2.5 Pro | 8,192 |
128 |
32,768 |
non può essere disattivato | -1 |
Gemini 2.5 Flash | 8,192 |
1 |
24,576 |
0 |
-1 |
Gemini 2.5 Flash‑Lite | 0 (il pensiero è disattivato per impostazione predefinita) |
512 |
24,576 |
0 (o non configurare affatto il budget di pensiero) |
-1 |
Disattiva il processo elaborativo
Per alcune attività più semplici, la capacità di ragionamento non è necessaria e l'inferenza tradizionale è sufficiente. Oppure, se la riduzione della latenza è una priorità, potresti non volere che il modello impieghi più tempo del necessario per generare una risposta.
In queste situazioni, puoi disattivare (o disabilitare) il pensiero:
- Gemini 2.5 Pro: il pensiero non può essere disattivato
- Gemini 2.5 Flash: imposta
thinkingBudget
su0
token - Gemini 2.5 Flash‑Lite: la funzionalità di pensiero è disattivata per impostazione predefinita
Attivare il pensiero dinamico
Puoi lasciare che il modello decida quando e quanto pensare (funzionalità chiamata
pensiero dinamico) impostando thinkingBudget
su -1
. Il modello può utilizzare
tutti i token che ritiene opportuno, fino al valore massimo di token
elencato sopra.
Complessità dell'attività
Attività semplici: il pensiero potrebbe essere disattivato
Richieste semplici in cui non è necessario un ragionamento complesso, ad esempio il recupero o la classificazione di fatti. Esempi:- "Dove è stata fondata DeepMind?"
- "Questa email richiede una riunione o fornisce solo informazioni?"
Attività medie: budget predefinito o budget aggiuntivo per la riflessione necessario
Richieste comuni che traggono vantaggio da un'elaborazione passo passo o da una comprensione più approfondita. Esempi:- "Crea un'analogia tra la fotosintesi e la crescita."
- "Confronta e contrapponi auto elettriche e auto ibride".
Attività difficili: potrebbe essere necessario il budget di pensiero massimo
Sfide davvero complesse, come la risoluzione di problemi di matematica complessi o attività di programmazione. Questi tipi di attività richiedono al modello di utilizzare tutte le sue capacità di ragionamento e pianificazione, spesso coinvolgendo molti passaggi interni prima di fornire una risposta. Esempi:- "Risolvi il problema 1 dell'AIME 2025: trova la somma di tutte le basi intere b > 9 per le quali 17b è un divisore di 97b."
- "Scrivi codice Python per un'applicazione web che visualizzi dati in tempo reale del mercato azionario, inclusa l'autenticazione utente. Rendilo il più efficiente possibile".
Includere i riepiloghi del pensiero nelle risposte
I riepiloghi del pensiero sono versioni sintetizzate dei pensieri grezzi del modello e offrono approfondimenti sul processo di ragionamento interno del modello.
Ecco alcuni motivi per includere i riepiloghi del pensiero nelle risposte:
Puoi visualizzare il riepilogo dei pensieri nell'interfaccia utente della tua app o renderli accessibili ai tuoi utenti. Il riepilogo del pensiero viene restituito come parte separata nella risposta, in modo da avere maggiore controllo sul suo utilizzo nell'app.
Se attivi anche il monitoraggio dell'AI nella console Firebase, i riepiloghi dei pensieri vengono visualizzati nella console, dove puoi esaminare il ragionamento dettagliato del modello per eseguire il debug e perfezionare i prompt.
Ecco alcuni punti chiave sui riepiloghi dei pensieri:
I riepiloghi dei pensieri non sono controllati dai budget di pensiero (i budget si applicano solo ai pensieri grezzi del modello). Tuttavia, se l'opzione Pensa è disattivata, il modello non restituirà un riepilogo del pensiero.
I riepiloghi del pensiero sono considerati parte della normale risposta di testo generato del modello e vengono conteggiati come token di output.
Attivare i riepiloghi dei pensieri
Fai clic sul tuo fornitore Gemini API per visualizzare i contenuti e il codice specifici del fornitore in questa pagina. |
Puoi attivare i riepiloghi dei pensieri impostando includeThoughts
su true nella configurazione del modello. Puoi quindi accedere al riepilogo controllando il campo
thoughtSummary
della risposta.
Ecco un esempio che mostra come attivare e recuperare i riepiloghi dei pensieri con la risposta:
Swift
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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}");
}
Riepiloghi dei pensieri dello stream
Puoi anche visualizzare i riepiloghi dei pensieri se scegli di riprodurre in streaming una risposta utilizzando
generateContentStream
. In questo modo, durante la generazione della risposta verranno restituiti riepiloghi incrementali e continui.
Swift
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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
Attiva i riepiloghi dei pensieri in
GenerationConfig
durante la creazione di un'istanza 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;
}
}
Informazioni sulle firme dei pensieri
Quando utilizza il pensiero nelle interazioni a più turni, il modello non ha accesso al contesto del pensiero dei turni precedenti. Tuttavia, se utilizzi la chiamata di funzione, puoi sfruttare le firme del pensiero per mantenere il contesto del pensiero tra i turni. Le firme del pensiero sono rappresentazioni criptate del processo di pensiero interno del modello e sono disponibili quando si utilizza la chiamata di funzione e thinking. Nello specifico, le firme dei pensieri vengono generate quando:
- La funzionalità di pensiero è abilitata e i pensieri vengono generati.
- La richiesta include dichiarazioni di funzione.
Per sfruttare le firme del pensiero, utilizza la chiamata di funzione normalmente.
Gli SDK Firebase AI Logic semplificano il processo gestendo lo stato e gestendo automaticamente le firme del pensiero per te. Gli SDK trasmettono automaticamente
tutte le firme di pensieri generate tra le chiamate sendMessage
o
sendMessageStream
successive in una sessione Chat
.
Prezzi e conteggio dei token di pensiero
I token di pensiero utilizzano gli stessi prezzi dei token di output di testo. Se attivi i riepiloghi del pensiero, questi vengono considerati token di pensiero e vengono prezzati di conseguenza.
Puoi attivare AI Monitoring nella console Firebase per monitorare il conteggio dei token di pensiero per le richieste per cui è attivata la funzionalità di pensiero.
Puoi ottenere il numero totale di token di pensiero dal campo thoughtsTokenCount
nell'attributo usageMetadata
della risposta:
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}");
}
Scopri di più sui token nella guida al conteggio dei token.