Dalam setiap panggilan ke model, Anda dapat mengirimkan konfigurasi model untuk mengontrol cara
model menghasilkan respons. Setiap model mendukung opsi konfigurasi yang berbeda, seperti menyetel maxOutputTokens atau konfigurasi khusus untuk penalaran, modalitas respons, gambar, ucapan, atau deteksi aktivitas suara (VAD).
Untuk sebagian besar kasus penggunaan saat mengakses model Gemini, Anda
mengonfigurasi model menggunakan GenerationConfig. Namun, jika Anda mengonfigurasi model Gemini Live API, gunakan LiveGenerationConfig.
Halaman ini menunjukkan cara menyiapkan konfigurasi untuk model Gemini dan menyediakan tabel parameter yang tersedia.
Langsung ke konfigurasi Gemini Langsung ke konfigurasi Gemini Live API
GenerationConfig untuk model Gemini
|
Klik penyedia Gemini API untuk melihat konten dan kode khusus penyedia di halaman ini. |
Tetapkan GenerationConfig untuk sebagian besar model generatif Gemini, termasuk model penggunaan umum, model pembuat gambar (Nano Banana), dan model text-to-speech (TTS).
Konfigurasi dipertahankan selama masa aktif instance GenerativeModel. Jika Anda ingin menggunakan konfigurasi yang berbeda, buat dan gunakan instance baru dengan konfigurasi yang berbeda.
Contoh berikut menunjukkan konfigurasi model yang sangat dasar. Untuk opsi tambahan (terutama untuk model khusus), lihat tabel parameter yang tersedia nanti di halaman ini.
Swift
Tetapkan nilai parameter dalam
GenerationConfig
sebagai bagian dari pembuatan instance 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
Tetapkan nilai parameter dalam
GenerationConfig
sebagai bagian dari pembuatan instance 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
Tetapkan nilai parameter dalam
GenerationConfig
sebagai bagian dari pembuatan instance 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
Tetapkan nilai parameter dalam
GenerationConfig
sebagai bagian dari pembuatan instance 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
Tetapkan nilai parameter dalam
GenerationConfig
sebagai bagian dari pembuatan instance 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',
generationConfig: generationConfig,
);
// ...
Unity
Tetapkan nilai parameter dalam
GenerationConfig
sebagai bagian dari pembuatan instance 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
);
LiveGenerationConfig untuk model Gemini Live API
|
Klik penyedia Gemini API untuk melihat konten dan kode khusus penyedia di halaman ini. |
Tetapkan LiveGenerationConfig hanya saat menggunakan model Gemini Live API.
Konfigurasi dipertahankan selama masa aktif instance LiveModel. Jika Anda ingin menggunakan konfigurasi yang berbeda, buat dan gunakan instance baru dengan konfigurasi yang berbeda.
Contoh berikut menunjukkan konfigurasi model yang sangat dasar. Untuk opsi tambahan (seperti mengonfigurasi deteksi aktivitas suara (VAD)), lihat tabel parameter yang tersedia di bagian lain halaman ini.
Swift
Tetapkan nilai parameter di
liveGenerationConfig
selama inisialisasi instance 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
Tetapkan nilai parameter dalam
LiveGenerationConfig
sebagai bagian dari pembuatan instance LiveModel.
// ...
// Set parameter values in a `LiveGenerationConfig` (example values shown here).
val config = liveGenerationConfig {
maxOutputTokens = 200
responseModality = ResponseModality.AUDIO
speechConfig = SpeechConfig(voice = Voice("FENRIR"))
}
// Specify the config as part of creating the `LiveModel` instance.
val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
modelName = "GEMINI_LIVE_MODEL_NAME",
generationConfig = config
)
// ...
Java
Tetapkan nilai parameter dalam
LiveGenerationConfig
sebagai bagian dari pembuatan instance 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(new Voice("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
Tetapkan nilai parameter di
LiveGenerationConfig
selama inisialisasi instance 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
Tetapkan nilai parameter dalam LiveGenerationConfig sebagai bagian dari pembuatan instance 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
Tetapkan nilai parameter dalam
LiveGenerationConfig
sebagai bagian dari pembuatan instance 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
);
// ...
Parameter model yang tersedia
Tabel berikut adalah ringkasan umum parameter yang tersedia untuk konfigurasi model. Beberapa parameter hanya berlaku untuk model tertentu.
| Parameter | Deskripsi | Keterbatasan | Nilai default |
|---|---|---|---|
Stempel waktu audioaudioTimestamp
|
Mengaktifkan pemahaman stempel waktu untuk file input khusus audio. |
Boolean
Hanya berlaku saat menggunakan konfigurasi |
false
|
Token output maksimummaxOutputTokens
|
Menentukan jumlah maksimum token yang dapat dibuat dalam respons.
Untuk |
--- | --- |
Hentikan urutanstopSequences
|
Menentukan daftar string yang memberi tahu model untuk berhenti menghasilkan konten jika salah satu string ditemukan dalam respons. |
Hanya berlaku saat menggunakan konfigurasi Tidak berlaku untuk model Nano Banana atau TTS. |
--- |
| Konfigurasi khusus | |||
BerpikirthinkingConfig
|
Mengontrol "proses berpikir" untuk model saat menghasilkan respons.
|
Nilai yang didukung: Lihat dokumentasi penalaran Tidak berlaku untuk model TTS.
Firebase AI Logic belum mendukung penetapan
|
Bergantung pada model |
Modalitas responsresponseModalities
|
Menentukan jenis output (seperti teks, audio, atau gambar). | Hanya berlaku (dan diperlukan) saat menggunakan model Nano Banana, TTS, dan Live API. | --- |
Karakteristik gambarimageConfig
|
Menentukan rasio aspek dan resolusi gambar yang dihasilkan. |
Nilai yang didukung: Lihat Mengonfigurasi pembuatan gambar Hanya berlaku saat menggunakan model Nano Banana. |
Rasio aspek 1:1 (persegi) Resolusi 1024x1024 |
Ucapan (suara)speechConfig
|
Menentukan suara yang digunakan untuk output audio. |
Hanya berlaku saat menggunakan model TTS dan model Live API. Diperlukan untuk model TTS. |
Puck
|
Deteksi aktivitas suara (VAD)realtimeInputConfig
|
Mengonfigurasi deteksi aktivitas suara (VAD), penanganan interupsi, dan cakupan konteks giliran, atau menonaktifkan VAD otomatis. |
Nilai yang didukung: Lihat Mengonfigurasi VAD Hanya berlaku saat menggunakan model Live API. |
VAD diaktifkan Lihat nilai default untuk masing-masing setelan. |
Opsi lain untuk mengontrol pembuatan konten
- Pelajari lebih lanjut desain perintah agar Anda dapat memengaruhi model untuk menghasilkan output yang sesuai dengan kebutuhan Anda.
- Gunakan setelan keamanan untuk menyesuaikan kemungkinan mendapatkan respons yang mungkin dianggap berbahaya, termasuk ujaran kebencian dan konten seksual vulgar.
- Menetapkan petunjuk sistem untuk mengarahkan perilaku model. Fitur ini seperti preamble yang Anda tambahkan sebelum model diekspos ke petunjuk lebih lanjut dari pengguna akhir.
- Teruskan skema respons bersama dengan perintah untuk menentukan skema output tertentu. Fitur ini paling umum digunakan saat membuat output JSON, tetapi juga dapat digunakan untuk tugas klasifikasi (seperti saat Anda ingin model menggunakan label atau tag tertentu).