Các mô hình 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 hiệu quả cao đố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:
Ngân sách suy nghĩ: Bạn có thể định cấu hình mức độ "suy nghĩ" mà một mô hình có thể thực hiện bằng cách sử dụng ngân sách suy nghĩ. Cấu hình này đặc biệt quan trọng nếu bạn ưu tiên 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.
Tóm tắt ý tưởng: Bạn có thể bật tính năng tóm tắt ý tưởng để đư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.
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 kỹ 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 (khởi chạy trình cung cấp Gemini API mà bạn đã chọn, tạo một thực thể GenerativeModel
, v.v.).
Bạn có thể dùng các mô hình này cho những tác vụ tạo văn bản hoặc mã, chẳng hạn như tạo đầu ra có cấu trúc hoặc phân tích dữ liệu đầu vào đa phương thức (chẳng hạn như hình ảnh, video, âm thanh hoặc tệp PDF).
Bạn thậm chí có thể sử dụng các mô hình tư duy khi truyền trực tuyến đầu ra.
Các mô hình hỗ trợ tính năng này
Chỉ các mô hình Gemini 2.5 mới hỗ trợ chức năng này.
gemini-2.5-pro
gemini-2.5-flash
gemini-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 để 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 một trong các kỹ thuật tạo câu lệnh 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 ngân sách suy nghĩ để định cấu hình mức độ suy nghĩ mà mô hình có thể thực hiện. Nếu bạn đặt ngân sách thấp, thì mô hình sẽ không "suy nghĩ quá nhiều" về câu trả lời của mình. Nếu bạn đặt ngân sách cao, thì mô hình có thể suy nghĩ thêm nếu cần. Việc đặt ngân sách suy nghĩ cũng giúp dành nhiều hơn hạn mức tổng số mã thông báo đầu ra cho phản hồi thực tế.
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 đang 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 ngân sách suy nghĩ
Để kiểm soát mức độ suy nghĩ mà mô hình có thể thực hiện để tạo câu trả lời, bạn có thể chỉ định số lượng mã thông báo ngân sách suy nghĩ mà mô hình được phép sử dụng.
Bạn có thể đặt ngân sách suy nghĩ theo cách thủ công trong những trường hợp bạn có thể cần nhiều hoặc ít mã thông báo hơn ngân sách suy nghĩ mặc định. Bạn có thể xem hướng dẫn chi tiết hơn về độ phức tạp của nhiệm vụ và ngân sách đề xuất ở phần sau của mục này. Sau đây là một số hướng dẫn chung:
- Đặt ngân sách suy nghĩ thấp nếu độ trễ là yếu tố quan trọng hoặc đối với các tác vụ ít phức tạp
- Đặt ngân sách tư duy cao cho những công việc phức tạp hơn
Đặ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 quá trình tạo thực thể GenerativeModel
. Cấu hình này đượ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ị ngân sách tư duy được hỗ trợ ở phần sau của phần này.
Swift
Đặt ngân sách suy nghĩ 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)
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
Đặ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_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_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_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(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
Đặ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_MODEL_NAME",
generationConfig: generationConfig
);
// ...
Các giá trị ngân sách đượ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.
Mô hình | Giá trị mặc định | Phạm vi có thể sử dụng để lập ngân sách |
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 tắt được | -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 |
Tắt tiến trình tư duy
Đối với một số tác vụ dễ dàng hơn, khả năng tư duy là không cần thiết và suy luận truyền thống là đủ. Hoặc nếu việc giảm độ trễ là ưu tiên, bạn có thể không muốn mô hình mất thêm thời gian không cần thiết để tạo câu trả lời.
Trong những tình huống này, bạn có thể tắt (hoặc ngừng) suy nghĩ:
- Gemini 2.5 Pro: không thể tắt tính năng suy nghĩ
- Gemini 2.5 Flash: đặt
thinkingBudget
thành0
mã thông báo - Gemini 2.5 Flash‑Lite: tính năng suy nghĩ bị tắt theo mặc định
Bật tư duy linh hoạt
Bạn có thể cho phép mô hình quyết định thời điểm và mức độ suy nghĩ (gọi là suy nghĩ linh hoạt) bằng cách đặt thinkingBudget
thành -1
. Mô hình có thể sử dụng nhiều mã thông báo tuỳ ý, tối đa bằng giá trị mã thông báo tối đa được liệt kê ở trên.
Độ phức tạp của việc cần làm
Việc dễ dàng – có thể tắt tính năng suy nghĩ
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ần ngân sách mặc định hoặc một số ngân sách suy nghĩ bổ sung
Các yêu cầu phổ biến được hưởng lợi từ mức độ xử lý từng bước hoặc 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 ngân sách 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 tác 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ố nguyên b > 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ể."
Đưa thông tin tóm tắt về suy nghĩ vào câu trả lời
Tóm tắt suy nghĩ là phiên bản tổng hợp của suy nghĩ thô 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 ý tưởng 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ố lưu ý quan trọng 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ĩ.
Bản 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 luồng suy nghĩ
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 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;
}
}
Tìm hiểu về chữ ký tư duy
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 đơ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
Thinking token sử dụng cùng một mức giá như text-output token. Nếu bạn bật tính năng tóm tắt suy nghĩ, thì những nội dung này đượ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ể nhận được tổng số mã thông báo suy nghĩ 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 lượt truy cập.