Các mô hình Gemini 3 và Gemini 2.5 có thể sử dụng "quy trình tư duy" nội bộ giúp cải thiện đáng kể khả năng lập luận và lập kế hoạch nhiều bước, khiến chúng trở nên rất hiệu quả đối với các tác vụ phức tạp như lập trình, toán học nâng cao và phân tích dữ liệu.
Các mô hình tư duy cung cấp những cấu hình và lựa chọn sau:
Kiểm soát lượng suy nghĩ
Bạn có thể định cấu hình lượng "suy nghĩ" mà một mô hình có thể thực hiện. Cấu hình này đặc biệt quan trọng nếu bạn ưu tiên việc giảm độ trễ hoặc chi phí. Ngoài ra, hãy xem so sánh độ khó của các nhiệm vụ để quyết định mức độ cần thiết của khả năng tư duy của mô hình.Kiểm soát cấu hình này bằng các cấp độ tư duy (Gemini phiên bản 3 trở lên) hoặc bằng ngân sách tư duy (Gemini phiên bản 2.5).
Nhận thông tin tóm tắt về suy nghĩ
Bạn có thể bật tính năng thông tin tóm tắt về suy nghĩ để đưa vào câu trả lời được tạo. Đây là phiên bản tổng hợp của những suy nghĩ thô sơ của mô hình và cung cấp thông tin chi tiết về quy trình suy luận nội bộ của mô hình.Xử lý chữ ký suy nghĩ
Các SDK Firebase AI Logic sẽ tự động xử lý chữ ký suy nghĩ cho bạn. Điều này đảm bảo rằng mô hình có quyền truy cập vào ngữ cảnh suy nghĩ từ các lượt trước, đặc biệt là khi sử dụng tính năng gọi hàm.
Nhớ xem các phương pháp hay nhất và hướng dẫn đưa ra câu lệnh để sử dụng các mô hình tư duy.
Sử dụng mô hình tư duy
Sử dụng mô hình tư duy giống như cách bạn sử dụng bất kỳ mô hình Gemini nào khác.
Để khai thác tối đa các mô hình tư duy, hãy xem Các phương pháp hay nhất và hướng dẫn đưa ra câu lệnh để sử dụng mô hình tư duy ở phần sau của trang này.
Các mô hình hỗ trợ tính năng này
Chỉ các mẫu Gemini 3 và Gemini 2.5 hỗ trợ tính năng này.
gemini-3-pro-previewgemini-3-pro-image-preview(còn gọi là "nano banana pro")gemini-3-flash-previewgemini-2.5-progemini-2.5-flashgemini-2.5-flash-lite
Các phương pháp hay nhất và hướng dẫn đưa ra câu lệnh để sử dụng các mô hình tư duy
Bạn nên thử nghiệm câu lệnh của mình trong Google AI Studio hoặc Vertex AI Studio. Tại đây, bạn có thể xem toàn bộ quy trình suy nghĩ. Bạn có thể xác định những điểm mà mô hình có thể đã đi sai hướng để tinh chỉnh câu lệnh nhằm nhận được câu trả lời nhất quán và chính xác hơn.
Bắt đầu bằng một câu lệnh chung mô tả kết quả mong muốn và quan sát suy nghĩ ban đầu của mô hình về cách xác định câu trả lời. Nếu câu trả lời không như mong đợi, hãy giúp mô hình tạo ra câu trả lời hay hơn bằng cách sử dụng bất kỳ kỹ thuật tạo câu lệnh nào sau đây:
- Cung cấp hướng dẫn từng bước
- Cung cấp một số ví dụ về các cặp đầu vào-đầu ra
- Đưa ra hướng dẫn về cách diễn đạt và định dạng đầu ra cũng như câu trả lời
- Cung cấp các bước xác minh cụ thể
Ngoài việc đưa ra câu lệnh, hãy cân nhắc sử dụng các đề xuất sau:
Đặt hướng dẫn hệ thống, giống như một "lời nói đầu" mà bạn thêm trước khi mô hình tiếp xúc với bất kỳ hướng dẫn nào khác từ câu lệnh hoặc người dùng cuối. Các tham số này cho phép bạn điều chỉnh hành vi của mô hình dựa trên nhu cầu và trường hợp sử dụng cụ thể của mình.
Đặt cấp độ tư duy (hoặc ngân sách tư duy cho các mô hình Gemini 2.5) để kiểm soát mức độ tư duy mà mô hình có thể thực hiện. Nếu bạn đặt cấp độ này ở mức cao, thì mô hình có thể tư duy nhiều hơn nếu cần. Nếu bạn đặt giá trị này thấp hơn, thì mô hình sẽ không "suy nghĩ quá nhiều" về câu trả lời của mình, đồng thời dành nhiều hơn tổng hạn mức đầu ra của mã thông báo cho câu trả lời thực tế và có thể giúp giảm độ trễ cũng như chi phí.
Bật tính năng giám sát AI trong bảng điều khiển Firebase để theo dõi số lượng mã thông báo suy nghĩ và độ trễ của các yêu cầu đã bật tính năng suy nghĩ. Và nếu bạn đã bật tính năng tóm tắt suy nghĩ, thì tính năng này sẽ xuất hiện trong bảng điều khiển, nơi bạn có thể kiểm tra lập luận chi tiết của mô hình để giúp bạn gỡ lỗi và tinh chỉnh câu lệnh.
Kiểm soát mức độ suy nghĩ
Bạn có thể định cấu hình mức độ "suy nghĩ" và suy luận mà một mô hình có thể thực hiện trước khi trả về câu trả lời. Cấu hình này đặc biệt quan trọng nếu bạn ưu tiên việc giảm độ trễ hoặc chi phí.
Hãy nhớ xem xét so sánh độ khó của các nhiệm vụ để quyết định mức độ cần thiết của khả năng tư duy của một mô hình. Sau đây là một số hướng dẫn chung:
- Đặt giá trị suy nghĩ thấp hơn cho các tác vụ ít phức tạp hoặc nếu bạn ưu tiên giảm độ trễ hoặc chi phí.
- Đặt giá trị tư duy cao hơn cho những công việc phức tạp hơn.
Kiểm soát cấu hình này bằng các cấp độ tư duy (Gemini phiên bản 3 trở lên) hoặc bằng ngân sách tư duy (Gemini phiên bản 2.5).
Các cấp độ tư duy (mô hình Gemini 3)
Để kiểm soát mức độ suy nghĩ mà mô hình Gemini 3 có thể thực hiện để tạo câu trả lời, bạn có thể chỉ định một mức độ suy nghĩ cho số lượng mã thông báo suy nghĩ mà mô hình được phép sử dụng.
Đặt cấp độ tư duy
|
Nhấp vào nhà cung cấp Gemini API để xem nội dung và mã dành riêng cho nhà cung cấp trên trang này. |
Đặt cấp độ tư duy trong GenerationConfig như một phần của việc tạo thực thể GenerativeModel cho mô hình Gemini 3. Cấu hình được duy trì trong suốt thời gian tồn tại của phiên bản. Nếu bạn muốn sử dụng các cấp độ tư duy khác nhau cho các yêu cầu khác nhau, hãy tạo các thực thể GenerativeModel được định cấu hình với từng cấp độ.
Tìm hiểu về các giá trị được hỗ trợ cho cấp độ tư duy ở phần sau của phần này.
Swift
Đặt cấp độ tư duy trong GenerationConfig trong quá trình tạo một thực thể 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_MODEL_NAME",
generationConfig: generationConfig
)
// ...
Kotlin
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_MODEL_NAME",
generationConfig,
)
// ...
Java
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_MODEL_NAME",
/* generationConfig */ generationConfig
);
);
// ...
Web
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_MODEL_NAME", generationConfig });
// ...
Dart
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_MODEL_NAME',
config: generationConfig,
);
// ...
Unity
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_MODEL_NAME",
generationConfig: generationConfig
);
// ...
Các giá trị được hỗ trợ cho cấp độ tư duy
Bảng sau đây liệt kê các giá trị cấp độ tư duy mà bạn có thể đặt cho từng mô hình bằng cách định cấu hình thinkingLevel của mô hình.
MINIMAL |
LOW |
MEDIUM |
HIGH |
|
|---|---|---|---|---|
|
|
|
|
|
| Gemini 3 Pro | (mặc định) | |||
| Gemini 3 Pro Image ("nano banana pro") | (mặc định) | |||
| Gemini 3 Flash | (mặc định) |
Ngân sách tư duy (Gemini 2.5 mô hình)
Để kiểm soát mức độ suy nghĩ mà mô hình Gemini 2.5 có thể thực hiện để tạo câu trả lời, bạn có thể chỉ định ngân sách suy nghĩ cho số lượng mã thông báo suy nghĩ mà mô hình được phép sử dụng.
Đặt ngân sách suy nghĩ
|
Nhấp vào nhà cung cấp Gemini API để xem nội dung và mã dành riêng cho nhà cung cấp trên trang này. |
Đặt ngân sách suy nghĩ trong GenerationConfig như một phần của việc tạo thực thể GenerativeModel cho mô hình Gemini 2.5. Cấu hình được duy trì trong suốt thời gian tồn tại của phiên bản. Nếu bạn muốn sử dụng các ngân sách suy nghĩ khác nhau cho các yêu cầu khác nhau, hãy tạo các thực thể GenerativeModel được định cấu hình với từng ngân sách.
Tìm hiểu về các giá trị được hỗ trợ cho ngân sách suy nghĩ ở phần sau của phần này.
Swift
Đặt ngân sách suy nghĩ trong GenerationConfig như một phần của việc tạo một phiên bản 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_2.5_MODEL_NAME",
generationConfig: generationConfig
)
// ...
Kotlin
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_2.5_MODEL_NAME",
generationConfig,
)
// ...
Java
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_2.5_MODEL_NAME",
/* generationConfig */ generationConfig
);
);
// ...
Web
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_2.5_MODEL_NAME", generationConfig });
// ...
Dart
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể GenerativeModel.
// ...
// Set the thinking configuration
// Use a thinking budget value appropriate for your model (example value shown here)
final thinkingConfig = ThinkingConfig.withThinkingBudget(1024);
final generationConfig = GenerationConfig(
thinkingConfig: thinkingConfig
);
// Specify the config as part of creating the `GenerativeModel` instance
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_2.5_MODEL_NAME',
config: generationConfig,
);
// ...
Unity
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể 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_2.5_MODEL_NAME",
generationConfig: generationConfig
);
// ...
Các giá trị ngân sách tư duy được hỗ trợ
Bảng sau đây liệt kê các giá trị ngân sách suy nghĩ mà bạn có thể đặt cho từng mô hình bằng cách định cấu hình thinkingBudget của mô hình.
| Kiểu máy | Giá trị mặc định | Phạm vi ngân sách có thể sử dụng |
Giá trị để tắt tính năng suy nghĩ |
Giá trị của việc khuyến khích tư duy linh hoạt |
|
|---|---|---|---|---|---|
| Giá trị tối thiểu | Giá trị tối đa | ||||
| Gemini 2.5 Pro | 8,192 |
128 |
32,768 |
Không thể tắt | -1 |
| Gemini 2.5 Flash | 8,192 |
1 |
24,576 |
0 |
-1 |
| Gemini 2.5 Flash‑Lite | 0(theo mặc định, tính năng suy nghĩ sẽ bị tắt) |
512 |
24,576 |
0(hoặc không định cấu hình ngân sách suy nghĩ) |
-1 |
Độ phức tạp của nhiệm vụ đối với tất cả các mô hình tư duy
Việc cần làm đơn giản – không cần tư duy nhiều
Những yêu cầu đơn giản không đòi hỏi suy luận phức tạp, chẳng hạn như truy xuất hoặc phân loại thông tin. Ví dụ:- "DeepMind được thành lập ở đâu?"
- "Email này có yêu cầu tổ chức cuộc họp hay chỉ cung cấp thông tin?"
Công việc ở mức trung bình – có thể cần suy nghĩ
Những yêu cầu phổ biến cần được xử lý từng bước hoặc cần hiểu biết sâu sắc hơn. Ví dụ:- "Tạo một phép loại suy giữa quá trình quang hợp và quá trình trưởng thành."
- "So sánh và đối chiếu xe điện và xe lai điện."
Nhiệm vụ khó – có thể cần tư duy tối đa
Những thử thách thực sự phức tạp, chẳng hạn như giải các bài toán phức tạp hoặc các nhiệm vụ lập trình. Những loại nhiệm vụ này đòi hỏi mô hình phải sử dụng toàn bộ khả năng lập luận và lập kế hoạch, thường liên quan đến nhiều bước nội bộ trước khi đưa ra câu trả lời. Ví dụ:- "Giải bài toán 1 trong AIME 2025: Tìm tổng của tất cả các cơ số b là số nguyên > 9 sao cho 17b là ước số của 97b."
- "Viết mã Python cho một ứng dụng web trực quan hoá dữ liệu thị trường chứng khoán theo thời gian thực, bao gồm cả xác thực người dùng. Hãy làm cho nó hiệu quả nhất có thể."
Tóm tắt suy nghĩ
Bản tóm tắt suy nghĩ là phiên bản tổng hợp của những suy nghĩ thô sơ của mô hình và cung cấp thông tin chi tiết về quy trình suy luận nội bộ của mô hình.
Sau đây là một số lý do bạn nên đưa tóm tắt suy nghĩ vào câu trả lời:
Bạn có thể hiển thị bản tóm tắt suy nghĩ trong giao diện người dùng của ứng dụng hoặc cung cấp cho người dùng. Bản tóm tắt ý tưởng được trả về dưới dạng một phần riêng biệt trong phản hồi để bạn có nhiều quyền kiểm soát hơn đối với cách sử dụng bản tóm tắt này trong ứng dụng của mình.
Nếu bạn cũng bật tính năng giám sát AI trong bảng điều khiển Firebase, thì bản tóm tắt suy nghĩ sẽ xuất hiện trong bảng điều khiển. Tại đây, bạn có thể kiểm tra lý do chi tiết của mô hình để gỡ lỗi và tinh chỉnh câu lệnh.
Sau đây là một số điểm chính về bản tóm tắt suy nghĩ:
Tóm tắt suy nghĩ không chịu sự kiểm soát của ngân sách suy nghĩ (ngân sách chỉ áp dụng cho suy nghĩ thô của mô hình). Tuy nhiên, nếu tính năng suy nghĩ bị tắt, thì mô hình sẽ không trả về bản tóm tắt suy nghĩ.
Tóm tắt suy nghĩ được coi là một phần của phản hồi văn bản thông thường do mô hình tạo và được tính là mã thông báo đầu ra.
Bật tính năng tóm tắt suy nghĩ
|
Nhấp vào nhà cung cấp Gemini API để xem nội dung và mã dành riêng cho nhà cung cấp trên trang này. |
Bạn có thể bật tính năng tóm tắt suy nghĩ bằng cách đặt includeThoughts thành true trong cấu hình mô hình. Sau đó, bạn có thể truy cập vào bản tóm tắt bằng cách kiểm tra trường thoughtSummary trong phản hồi.
Sau đây là ví dụ minh hoạ cách bật và truy xuất bản tóm tắt ý tưởng bằng câu trả lời:
Swift
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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}");
}
Tóm tắt suy nghĩ theo thời gian thực
Bạn cũng có thể xem bản tóm tắt suy nghĩ nếu chọn truyền trực tuyến một câu trả lời bằng generateContentStream. Thao tác này sẽ trả về các bản tóm tắt tăng dần theo thời gian thực trong quá trình tạo phản hồi.
Swift
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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
Bật tính năng tóm tắt suy nghĩ trong GenerationConfig trong quá trình tạo một thực thể 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;
}
}
Chữ ký của suy nghĩ
Khi sử dụng tính năng suy nghĩ trong các lượt tương tác nhiều lượt, mô hình không có quyền truy cập vào ngữ cảnh suy nghĩ từ các lượt trước. Tuy nhiên, nếu đang sử dụng tính năng gọi hàm, bạn có thể tận dụng chữ ký ý tưởng để duy trì ngữ cảnh ý tưởng trong các lượt tương tác. Chữ ký suy nghĩ là biểu thị được mã hoá của quy trình suy nghĩ nội bộ của mô hình và có sẵn khi sử dụng tính năng suy nghĩ và gọi hàm. Cụ thể, chữ ký tư duy được tạo ra khi:
- Đã bật tính năng suy nghĩ và hệ thống đang tạo suy nghĩ.
- Yêu cầu này bao gồm các nội dung khai báo hàm.
Để tận dụng chữ ký suy nghĩ, hãy sử dụng tính năng gọi hàm như bình thường.
Các SDK Firebase AI Logic giúp đơn giản hoá quy trình bằng cách quản lý trạng thái và tự động xử lý chữ ký tư duy cho bạn. Các SDK sẽ tự động truyền mọi chữ ký tư duy đã tạo giữa các lệnh gọi sendMessage hoặc sendMessageStream tiếp theo trong một phiên Chat.
Giá và cách tính mã thông báo
Mô hình Tư duy sử dụng cùng một mức giá như các mã thông báo đầu ra dạng văn bản. Nếu bạn bật tính năng tóm tắt suy nghĩ, thì những nội dung này sẽ được coi là mã thông báo suy nghĩ và được tính giá tương ứng.
Bạn có thể bật tính năng giám sát bằng AI trong bảng điều khiển Firebase để theo dõi số lượng mã thông báo đang suy nghĩ cho các yêu cầu đã bật tính năng suy nghĩ.
Bạn có thể lấy tổng số mã thông báo tư duy từ trường thoughtsTokenCount trong thuộc tính usageMetadata của phản hồi:
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}");
}
Tìm hiểu thêm về mã thông báo trong hướng dẫn về mã thông báo số lượng.