Tất cả các mô hình Gemini đều sử dụng một "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.
Mô hình tư duy có các cấu hình và lựa chọn sau:
Kiểm soát mức độ suy nghĩ
Bạn có thể định cấu hình mức độ suy nghĩ để kiểm soát mức độ "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 giảm độ trễ hoặc chi phí. Ngoài ra, hãy xem phần so sánh độ khó của nhiệm vụ để quyết định mức độ cần thiết của khả năng tư duy đối với một mô hình.Nhận bản 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. Những bản tóm tắt này 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.Xử lý chữ ký suy nghĩ
Firebase AI Logic SDK tự động xử lý chữ ký suy nghĩ cho bạn, đảm bảo rằng mô hình có quyền truy cập vào bối 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.
Hãy 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.
Các phương pháp hay nhất và hướng dẫn viết câu lệnh khi sử dụng mô hình tư duy
Bạn nên thử nghiệm câu lệnh trong Google AI Studio hoặc Agent Studio để có thể xem toàn bộ quy trình suy nghĩ. Bạn có thể xác định mọi đ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 phù hợp hơn bằng cách sử dụng bất kỳ kỹ thuật viết 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ặp đầu vào-đầu ra
- Đưa ra hướng dẫn về cách diễn đạt và định dạng kết quả cũng như câu trả lời
- Cung cấp các bước xác minh cụ thể
Ngoài việc nhắc 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 "chỉ dẫn mở đầu" mà bạn thêm vào 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 hướng dẫn này giú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 bạn.
Đặt mức độ tư duy để kiểm soát mức độ tư duy mà mô hình có thể thực hiện. Nếu bạn đặt mức độ tư duy cao, mô hình có thể tư duy nhiều hơn (nếu cần). Nếu bạn đặt giá trị thấp hơn, thì mô hình sẽ không "suy nghĩ quá nhiều" về câu trả lời và cũng dành nhiều hơn trong tổng giới hạn mã thông báo đầu ra cho câu trả lời thực tế, đồng thời có thể giúp giảm độ trễ và chi phí.
Bật tính năng giám sát bằng AI trong bảng điều khiển Firebase để giám sát số lượng token tư duy và độ trễ của các yêu cầu đã bật tính năng tư duy. 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ể thiết lập các cấp độ tư duy để kiểm soát mức độ "tư duy" 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 giảm độ trễ hoặc chi phí.
Hãy nhớ xem phần so sánh độ khó của nhiệm vụ để quyết định mức độ cần thiết của khả năng tư duy đối với một mô hình. Sau đây là một số hướng dẫn chung:
- Hãy đặt giá trị tư duy thấp hơn cho các nhiệm vụ ít phức tạp hơn 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 các nhiệm vụ phức tạp hơn.
Cấp độ tư duy
Để kiểm soát mức độ tư duy mà mô hình có thể thực hiện để tạo câu trả lời, bạn có thể chỉ định một cấp độ tư duy cho số lượng token tư duy mà mô hình được phép sử dụng.
Thiết lập 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 trong quá trình tạo thực thể GenerativeModel. Cấu hình được duy trì trong suốt thời gian hoạt động của thực thể. 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 mục 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.x_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.x_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.x_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.x_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.x_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.x_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 độ suy nghĩ 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 |
|
|---|---|---|---|---|
|
Mô hình sử dụng ít mã thông báo nhất có thể; gần như không suy nghĩ Việc có độ phức tạp thấp |
Mô hình sử dụng ít mã thông báo hơn; giảm thiểu độ trễ và chi phí Tác vụ đơn giản và tác vụ có công suất cao |
Mô hình sử dụng một cách tiếp cận cân bằng Nhiệm vụ có độ phức tạp trung bình |
Mô hình sử dụng token đến mức tối đa Câu lệnh phức tạp đòi hỏi khả năng suy luận sâu sắc |
|
Gemini 4 Argongemini-4-argon
|
1 | (mặc định) | ||
Gemini 3.x Progemini-3.1-pro-preview
|
(mặc định) | |||
Gemini 3.x Flashgemini-3.8-flashgemini-3.7-flashgemini-3.6-flashgemini-3.5-flash
|
không được gemini-3.8-flashgemini-3.7-flash |
(mặc định) | ||
Gemini 3.x Flash‑Litegemini-3.5-flash-litegemini-3.1-flash-lite
|
(mặc định) | |||
Gemini 3.x Pro Imagegemini-3-pro-image("Nano Banana Pro") |
(mặc định) | |||
Gemini 3.x Flash Imagegemini-3.1-flash-image("Nano Banana 2") |
(mặc định) | |||
Gemini 3.x Flash‑Lite Imagegemini-3.1-flash-lite-image("Nano Banana 2 Lite") |
(mặc định) |
1 gemini-4-argon, gemini-3.8-flash và gemini-3.7-flash không hỗ trợ cấp độ tư duy MINIMAL. Nếu bạn đặt cấp độ tư duy thành
MINIMAL, yêu cầu sẽ không thành công và trả về lỗi 400.
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 phản hồ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.
Độ phức tạp của nhiệm vụ đối với tất cả mô hình tư duy
Nhiệm vụ dễ dàng — không cần suy nghĩ nhiều
Những yêu cầu đơn giản không cần suy luận phức tạp, chẳng hạn như truy xuất thông tin thực tế hoặc phân loại. Ví dụ:- "DeepMind được thành lập ở đâu?"
- "Email này yêu cầu 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ĩ
Các yêu cầu thường gặp 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ụ:- "Hãy so sánh quá trình quang hợp với 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 suy nghĩ 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 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ố 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ả quy trình 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 nên đưa bản 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 suy nghĩ đượ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 đó trong ứng dụng của mình.
Nếu bạn cũng bật tính năng giám sát bằng AI trong bảng điều khiển Firebase, thì các 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 để giúp bạn 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ĩ:
Bản 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 bạn tắt tính năng suy nghĩ, 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 dạng văn bản thông thường do mô hình tạo ra 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 của bạn để 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 suy nghĩ bằng phản hồi:
Swift
Bật bản 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 bản 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 bản 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 bản 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 bản 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 ý tưởng 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 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 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 bản 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 bản 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ý tư duy
Khi sử dụng quy trình 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 bối 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ý suy nghĩ để duy trì ngữ cảnh suy nghĩ qua các lượt. Chữ ký suy nghĩ là bản trình bày đượ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 gọi hàm suy nghĩ và. Cụ thể, chữ ký tư duy được tạo ra khi:
- Tính năng tư duy được bật và các suy nghĩ được tạo.
- 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.
Firebase AI Logic SDK đơn giản hoá quy trình này bằng cách quản lý trạng thái và tự động xử lý chữ ký suy nghĩ cho bạn. SDK sẽ tự động
chuyển mọi chữ ký suy nghĩ được tạo giữa các lệnh gọi sendMessage hoặc
sendMessageStream tiếp theo trong một phiên Chat.
Định giá và tính số lượng token tư duy
Token tư duy có cùng mức giá với token đầu ra 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à token tư duy 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 token tư duy cho những yêu cầu đã bật chế độ tư duy.
Bạn có thể lấy tổng số token 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.