Sử dụng cấu hình mô hình để kiểm soát phản hồi

Trong mỗi lệnh gọi đến một mô hình, bạn có thể gửi kèm một cấu hình mô hình để kiểm soát cách mô hình tạo phản hồi. Mỗi mô hình hỗ trợ các tuỳ chọn cấu hình khác nhau, chẳng hạn như đặt maxOutputTokens hoặc cấu hình chuyên biệt cho tư duy, 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ột mô hình Gemini Live API, thì bạn sẽ 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 Geminicấu hình Chuyển đến Gemini Live APIcấu hình

GenerationConfig cho các mô hình Gemini

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.

Đặ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 sang 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ô hình Gemini Live API

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.

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 chạy 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 = 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

Đặ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(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

Đặt giá trị của các tham số trong LiveGenerationConfig trong quá trình khởi chạy 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 Google AI Studio.



Nội dung mô tả về các tham số

Bảng sau đây trình bày 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 của âm thanh
audioTimestamp
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 GenerativeModel và loại đầu vào là tệp chỉ có âm thanh.

false
Số mã thông báo đầu ra tối đa
maxOutputTokens

Chỉ định số lượng mã thông báo tối đa có thể được tạo trong phản hồi.

Đối với gemini-4-argon, giới hạn mã thông báo đầu ra mặc định và tối đa là 32,768 mã thông báo cho các yêu cầu không truyền trực tuyến và 65,536 mã thông báo cho các yêu cầu truyền trực tuyến. Xin lưu ý rằng Firebase AI Logic chưa hỗ trợ tính năng giải mã dài, nhưng tính năng đó sẽ sớm ra mắt!

--- ---
Dừng trình tự
stopSequences
Chỉ định danh sách các chuỗi yêu cầu mô hình ngừng tạo nội dung nếu gặp một trong các chuỗi đó trong phản hồi.

Chỉ áp dụng khi sử dụng cấu hình GenerativeModel.

Không áp dụng cho Mô hình hình ảnh ("Nano Banana") hoặ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 câu trả lời.

  • Kiểm soát mức độ tư duy mà mô hình có thể thực hiện bằng cách sử dụng cấp độ tư duy.
  • Kiểm soát việc có đưa bản tóm tắt suy nghĩ vào hay không.

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 hiện chưa hỗ trợ việc đặt thinkingConfig cho các mô hình Live API.

Tuỳ thuộc vào mô hình
Phương thức phản hồi
responseModalities
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 (mô hình "Nano Banana"), mô hình TTS và Live API mô hình. ---
Đặc điểm của hình ảnh
imageConfig
Chỉ định tỷ lệ khung hình và độ phân giải của hình ảnh tạo sinh.

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 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ề 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 bạn.
  • 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 để điều chỉnh hành vi của mô hình. Tính năng này 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 của người dùng cuối.
  • Truyền 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 kết quả JSON, nhưng cũng có thể dùng cho các nhiệm 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ể).