Trong mỗi lệnh gọi đến một mô hình, bạn có thể gửi cùng một cấu hình mô hình để kiểm soát cách mô hình tạo ra phản hồi. Mỗi mô hình hỗ trợ các lựa chọn cấu hình khác nhau, chẳng hạn như thiết lập maxOutputTokens hoặc các cấu hình chuyên biệt cho việc suy nghĩ, phương thức phản hồi, hình ảnh hoặc lời nói.
Đối với phần lớn các trường hợp sử dụng khi truy cập vào một mô hình Gemini, bạn sẽ định cấu hình mô hình bằng GenerationConfig. Tuy nhiên, nếu đang định cấu hình mô hình Gemini Live API, thì bạn sẽ dùng LiveGenerationConfig.
Trang này cho biết cách thiết lập cấu hình cho các mô hình Gemini và cung cấp nội dung mô tả về từng tham số.
Chuyển đến cấu hình Gemini Chuyển đến cấu hình Gemini Live API
GenerationConfig cho các mẫu Gemini
|
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 GenerationConfig cho phần lớn các mô hình Gemini, bao gồm cả mô hình sử dụng chung, mô hình tạo hình ảnh ("mô hình Nano Banana") và mô hình chuyển văn bản thành lời nói (TTS).
Cấu hình này được duy trì trong suốt thời gian tồn tại của thực thể GenerativeModel. Nếu bạn muốn sử dụng một cấu hình khác, hãy tạo và sử dụng một phiên bản mới với cấu hình khác.
Swift
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể GenerativeModel.
import FirebaseAILogic
// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
let config = GenerationConfig(
maxOutputTokens: 200,
stopSequences: ["red"]
)
// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: config
)
// ...
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 parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
val config = generationConfig {
maxOutputTokens = 200
stopSequences = listOf("red")
}
// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
generationConfig = config
)
// ...
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 parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.maxOutputTokens = 200;
configBuilder.stopSequences = List.of("red");
GenerationConfig config = configBuilder.build();
// Specify the config as part of creating the `GenerativeModel` instance.
GenerativeModelFutures model = GenerativeModelFutures.from(
FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
"GEMINI_MODEL_NAME",
config
);
);
// ...
Web
Đặt giá trị của các tham số trong GenerationConfig trong quá trình tạo một thực thể GenerativeModel.
// ...
// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
const generationConfig = {
maxOutputTokens: 200,
stopSequences: ["red"],
};
// 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 parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
final generationConfig = GenerationConfig(
maxOutputTokens: 200,
stopSequences: ["red"],
);
// Initialize the Gemini Developer API backend service.
// 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 parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// For the latest general-use Gemini models, the following parameters are now unsupported:
// temperature, top-K, top-P, frequency penalty, presence penalty, and candidate count
var generationConfig = new GenerationConfig(
maxOutputTokens: 200,
stopSequences: new string[] { "red" }
);
// Initialize the Gemini Developer API backend service.
// Specify the config as part of creating the `GenerativeModel` instance.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
var model = ai.GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
generationConfig: generationConfig
);
Bạn có thể xem nội dung mô tả về từng tham số trong phần tiếp theo của trang này.
Bạn có thể thử nghiệm các câu lệnh và cấu hình mô hình bằng cách sử dụng Google AI Studio.LiveGenerationConfig cho các mẫu Gemini Live API
|
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. |
Chỉ đặt LiveGenerationConfig khi sử dụng các mô hình Gemini Live API.
Cấu hình này được duy trì trong suốt thời gian tồn tại của thực thể LiveModel. Nếu bạn muốn sử dụng một cấu hình khác, hãy tạo và sử dụng một phiên bản mới với cấu hình khác.
Swift
Đặt giá trị của các tham số trong liveGenerationConfig trong quá trình khởi tạo thực thể LiveModel:
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
let config = LiveGenerationConfig(
maxOutputTokens: 200,
responseModalities: [.audio],
speech: SpeechConfig(voiceName: "Fenrir"),
)
// Specify the config as part of creating the `liveModel` instance.
let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
modelName: "GEMINI_LIVE_MODEL_NAME",
generationConfig: config
)
// ...
Kotlin
Đặt giá trị của các tham số trong LiveGenerationConfig trong quá trình tạo một thực thể LiveModel.
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
val config = liveGenerationConfig {
maxOutputTokens = 200
responseModality = ResponseModality.AUDIO
speechConfig = SpeechConfig(voice = Voices.FENRIR)
}
// Specify the config as part of creating the `LiveModel` instance.
val liveModel = Firebase.ai(backend = GenerativeBackend.agentPlatform()).liveModel(
modelName = "GEMINI_LIVE_MODEL_NAME",
generationConfig = config
)
// ...
Java
Đặt giá trị của các tham số trong LiveGenerationConfig trong quá trình tạo một thực thể LiveModel.
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
LiveGenerationConfig.Builder configBuilder = new LiveGenerationConfig.Builder();
configBuilder.setMaxOutputTokens(200);
configBuilder.setResponseModality(ResponseModality.AUDIO);
configBuilder.setSpeechConfig(new SpeechConfig(Voices.FENRIR));
LiveGenerationConfig config = configBuilder.build();
// Specify the config as part of creating the `LiveModel` instance.
LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
"GEMINI_LIVE_MODEL_NAME",
config
);
// ...
Web
Đặt giá trị của các tham số trong LiveGenerationConfig trong quá trình khởi tạo thực thể LiveGenerativeModel:
// ...
// Initialize the Gemini Developer API backend service.
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
const liveGenerationConfig = {
maxOutputTokens: 200,
responseModalities: [ResponseModality.AUDIO],
speechConfig: {
voiceConfig: {
prebuiltVoiceConfig: { voiceName: "Fenrir" },
},
},
};
// Specify the config as part of creating the `LiveGenerativeModel` instance.
const liveModel = getLiveGenerativeModel(ai, {
model: "GEMINI_LIVE_MODEL_NAME",
liveGenerationConfig,
});
// ...
Dart
Đặt giá trị của các tham số trong LiveGenerationConfig trong quá trình tạo một thực thể LiveGenerativeModel.
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
final config = LiveGenerationConfig(
maxOutputTokens: 200,
responseModalities: [ResponseModalities.audio],
speechConfig: SpeechConfig(voiceName: 'Fenrir'),
);
// Specify the config as part of creating the `liveGenerativeModel` instance.
final liveModel = FirebaseAI.googleAI().liveGenerativeModel(
model: 'GEMINI_LIVE_MODEL_NAME',
liveGenerationConfig: config,
);
// ...
Unity
Đặt giá trị của các tham số trong LiveGenerationConfig trong quá trình tạo một thực thể LiveModel.
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
var config = new LiveGenerationConfig(
maxOutputTokens: 200,
responseModalities: new[] { ResponseModality.Audio },
speechConfig: SpeechConfig.UsePrebuiltVoice("Fenrir")
);
// Specify the config as part of creating the `LiveModel` instance.
var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
modelName: "GEMINI_LIVE_MODEL_NAME",
liveGenerationConfig: config
);
// ...
Bạn có thể xem nội dung mô tả về từng tham số trong phần tiếp theo của trang này.
Bạn có thể thử nghiệm các câu lệnh và cấu hình mô hình bằng cách sử dụng Google AI Studio.Nội dung mô tả về các tham số
Bảng sau đây là thông tin tổng quan cấp cao về các tham số hiện có. Một số tham số chỉ áp dụng cho một số mẫu nhất định.
| Tham số | Mô tả | Hạn chế | Giá trị mặc định |
|---|---|---|---|
Dấu thời gian âm thanhaudioTimestamp
|
Cho phép hiểu dấu thời gian cho các tệp đầu vào chỉ có âm thanh. |
Boolean
Chỉ áp dụng khi sử dụng cấu hình |
false
|
Số mã thông báo đầu ra tối đamaxOutputTokens
|
Chỉ định số lượng mã thông báo tối đa có thể được tạo trong phản hồi. | --- | --- |
Dừng chuỗistopSequences
|
Chỉ định một danh sách các chuỗi để yêu cầu mô hình ngừng tạo nội dung nếu một trong các chuỗi đó xuất hiện trong câu trả lời. |
Chỉ áp dụng khi sử dụng cấu hình Không áp dụng cho các mô hình hình ảnh ("Nano Banana") hoặc các mô hình TTS. |
--- |
| Cấu hình chuyên biệt | |||
Đang suy nghĩthinkingConfig
|
Kiểm soát "tiến trình tư duy" của các mô hình khi chúng tạo ra một câu trả lời.
|
Giá trị được hỗ trợ: Xem tài liệu về suy nghĩ Không áp dụng cho các mô hình TTS.
Firebase AI Logic chưa hỗ trợ việc đặt |
Tuỳ thuộc vào mô hình |
Phương thức phản hồiresponseModalities
|
Chỉ định loại đầu ra (chẳng hạn như văn bản, âm thanh hoặc hình ảnh). | Chỉ áp dụng (và bắt buộc) khi sử dụng Gemini Mô hình hình ảnh ("Nano Banana"), mô hình TTS và mô hình Live API. | --- |
Đặc điểm của hình ảnhimageConfig
|
Chỉ định tỷ lệ khung hình và độ phân giải của hình ảnh được tạo. |
Giá trị được hỗ trợ: Xem phần Định cấu hình tính năng tạo hình ảnh Chỉ áp dụng khi sử dụng Gemini Mô hình hình ảnh (mô hình "Nano Banana"). |
Tỷ lệ khung hình 1:1 (hình vuông) Độ phân giải 1024x1024 |
Lời nói (giọng nói)speechConfig
|
Chỉ định giọng nói dùng cho đầu ra âm thanh. |
Chỉ áp dụng khi sử dụng các mô hình TTS và mô hình Live API. Bắt buộc đối với các mô hình TTS. |
Puck
|
Các lựa chọn khác để kiểm soát việc tạo nội dung
- Tìm hiểu thêm về cách thiết kế câu lệnh để bạn có thể tác động đến mô hình nhằm tạo ra kết quả phù hợp với nhu cầu của mình.
- Sử dụng chế độ cài đặt an toàn để điều chỉnh khả năng nhận được những câu trả lời có thể bị coi là gây hại, bao gồm cả lời nói hận thù và nội dung khiêu dâm.
- Đặt chỉ dẫn hệ thống để định hướng hành vi của mô hình. Tính năng này giống như một phầ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 của người dùng cuối.
- Truyền một giản đồ phản hồi cùng với câu lệnh để chỉ định một giản đồ đầu ra cụ thể. Tính năng này thường được dùng nhất khi tạo đầu ra JSON, nhưng cũng có thể dùng cho các tác vụ phân loại (chẳng hạn như khi bạn muốn mô hình sử dụng các nhãn hoặc thẻ cụ thể).