ใช้การกําหนดค่าโมเดลเพื่อควบคุมคําตอบ

ในการเรียกใช้โมเดลแต่ละครั้ง คุณสามารถส่งการกำหนดค่าโมเดลเพื่อควบคุมวิธีที่โมเดลสร้างคำตอบได้ แต่ละโมเดลรองรับตัวเลือกการกำหนดค่าที่แตกต่างกัน เช่น การตั้งค่า maxOutputTokens หรือการกำหนดค่าเฉพาะสำหรับการคิด รูปแบบการตอบสนอง รูปภาพ คำพูด หรือการตรวจจับกิจกรรมเสียงพูด (VAD)

สำหรับ Use Case ส่วนใหญ่เมื่อเข้าถึงโมเดล Gemini คุณจะกำหนดค่าโมเดลโดยใช้ GenerationConfig อย่างไรก็ตาม หากคุณกำลังกำหนดค่าโมเดล Gemini Live API คุณจะต้องใช้ LiveGenerationConfig

หน้านี้แสดงวิธีตั้งค่าการกำหนดค่าสำหรับโมเดล Gemini และมีตารางพารามิเตอร์ที่ใช้ได้

ข้ามไปที่การกำหนดค่า Gemini ข้ามไปที่การกำหนดค่า Gemini Live API

GenerationConfig สำหรับโมเดล Gemini

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการในหน้านี้

ตั้งค่า GenerationConfig สำหรับโมเดล Gemini ส่วนใหญ่ ซึ่งรวมถึงโมเดลการใช้งานทั่วไป โมเดลการสร้างรูปภาพ (Nano Banana) และโมเดลการอ่านออกเสียงข้อความ (TTS)

ระบบจะคงการกำหนดค่าไว้ตลอดอายุการใช้งานของอินสแตนซ์ GenerativeModel หากต้องการใช้การกำหนดค่าอื่น ให้สร้างและใช้อินสแตนซ์ใหม่ที่มีการกำหนดค่าอื่น

ตัวอย่างต่อไปนี้แสดงการกำหนดค่าโมเดลพื้นฐาน สำหรับตัวเลือกเพิ่มเติม (โดยเฉพาะสำหรับโมเดลเฉพาะทาง) โปรดดูตารางพารามิเตอร์ที่ใช้ได้ในหน้านี้

Swift

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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
);

คุณสามารถทดลองใช้พรอมต์และการกำหนดค่าโมเดลได้โดยใช้ Google AI Studio

LiveGenerationConfig สำหรับโมเดล Gemini Live API

คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการในหน้านี้

ตั้งค่า LiveGenerationConfig เฉพาะเมื่อใช้โมเดล Gemini Live API

ระบบจะคงการกำหนดค่าไว้ตลอดอายุการใช้งานของอินสแตนซ์ LiveModel หากต้องการใช้การกำหนดค่าอื่น ให้สร้างและใช้อินสแตนซ์ใหม่ที่มีการกำหนดค่าอื่น

ตัวอย่างต่อไปนี้แสดงการกำหนดค่าโมเดลพื้นฐาน สำหรับตัวเลือกเพิ่มเติม (เช่น การกำหนดค่าการตรวจจับกิจกรรมเสียงพูด (VAD)) โปรดดูตารางพารามิเตอร์ที่ใช้ได้ในหน้านี้

Swift

ตั้งค่าพารามิเตอร์ใน liveGenerationConfig ระหว่างการเริ่มต้นอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน LiveGenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน LiveGenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน LiveGenerationConfig ระหว่างการเริ่มต้นอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน LiveGenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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

ตั้งค่าพารามิเตอร์ใน LiveGenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ 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
);

// ...

คุณสามารถทดลองใช้พรอมต์และการกำหนดค่าโมเดลโดยใช้ Google AI Studio



พารามิเตอร์โมเดลที่พร้อมใช้งาน

ตารางต่อไปนี้เป็นภาพรวมระดับสูงของพารามิเตอร์ที่ใช้ได้สำหรับการกำหนดค่าโมเดล พารามิเตอร์บางรายการใช้ได้กับบางโมเดลเท่านั้น

พารามิเตอร์ คำอธิบาย ข้อจำกัด ค่าเริ่มต้น
การประทับเวลาของเสียง
audioTimestamp
เปิดใช้การทำความเข้าใจการประทับเวลาสำหรับไฟล์อินพุตแบบเสียงเท่านั้น

บูลีน

ใช้ได้เฉพาะเมื่อใช้การกำหนดค่า GenerativeModel และประเภทอินพุตเป็นไฟล์เสียงเท่านั้น

false
โทเค็นเอาต์พุตสูงสุด
maxOutputTokens

ระบุจำนวนโทเค็นสูงสุดที่สร้างได้ในการตอบกลับ

สำหรับ gemini-4-argon ขีดจำกัดโทเค็นเอาต์พุตเริ่มต้นและสูงสุดคือ 32,768 โทเค็นสำหรับคำขอที่ไม่ใช่สตรีมมิง และ 65,536 โทเค็นสำหรับคำขอสตรีมมิง โปรดทราบว่า Firebase AI Logic ยังไม่รองรับการถอดรหัสแบบยาว แต่ฟีเจอร์ดังกล่าวจะพร้อมให้บริการเร็วๆ นี้

--- ---
หยุดลำดับ
stopSequences
ระบุรายการสตริงที่บอกให้โมเดลหยุดสร้างเนื้อหาหากพบสตริงใดสตริงหนึ่งในคำตอบ

ใช้ได้เฉพาะเมื่อใช้การกำหนดค่า GenerativeModel

ไม่เกี่ยวข้องกับโมเดล Nano Banana หรือ TTS

---
การกำหนดค่าเฉพาะทาง
กำลังคิด
thinkingConfig

ควบคุม "กระบวนการคิด" ของโมเดลเมื่อสร้างคำตอบ

  • ควบคุมปริมาณการคิดที่โมเดลสามารถทำได้โดยใช้ระดับการคิด
  • ควบคุมว่าจะรวมสรุปความคิดหรือไม่

ค่าที่รองรับ: ดูเอกสารประกอบเกี่ยวกับการคิด

ใช้ไม่ได้กับโมเดล TTS

Firebase AI Logic ยังไม่รองรับการตั้งค่า thinkingConfig สำหรับโมเดล Live API

ขึ้นอยู่กับรุ่น
รูปแบบการตอบสนอง
responseModalities
ระบุประเภทเอาต์พุต (เช่น ข้อความ เสียง หรือรูปภาพ) ใช้ได้ (และจำเป็น) เมื่อใช้โมเดล Nano Banana, TTS และ Live API เท่านั้น ---
ลักษณะของรูปภาพ
imageConfig
ระบุสัดส่วนภาพและความละเอียดของรูปภาพที่สร้างขึ้น

ค่าที่รองรับ: ดูหัวข้อกำหนดค่าการสร้างรูปภาพ

ใช้ได้เฉพาะเมื่อใช้โมเดล Nano Banana เท่านั้น

สัดส่วนภาพ 1:1 (สี่เหลี่ยมจัตุรัส)
ความละเอียด 1024x1024
คำพูด (เสียง)
speechConfig
ระบุเสียงที่ใช้สำหรับเอาต์พุตเสียง ใช้ได้เฉพาะเมื่อใช้โมเดล TTS และโมเดล Live API

ต้องระบุสำหรับโมเดล TTS
Puck
การตรวจจับกิจกรรมเสียงพูด (VAD)
realtimeInputConfig
กำหนดค่าการตรวจจับกิจกรรมเสียงพูด (VAD), การจัดการการขัดจังหวะ และความครอบคลุมของบริบทเทิร์น หรือปิดใช้ VAD อัตโนมัติ

ค่าที่รองรับ: ดูหัวข้อกำหนดค่า VAD

ใช้ได้เฉพาะเมื่อใช้โมเดล Live API เท่านั้น

เปิดใช้ VAD แล้ว

ดูค่าเริ่มต้นสำหรับการตั้งค่าแต่ละรายการ



ตัวเลือกอื่นๆ ในการควบคุมการสร้างเนื้อหา

  • ดูข้อมูลเพิ่มเติมเกี่ยวกับการออกแบบพรอมต์เพื่อให้คุณสามารถกำหนดให้โมเดลสร้างเอาต์พุตที่ตรงกับความต้องการของคุณได้
  • ใช้การตั้งค่าความปลอดภัยเพื่อปรับโอกาสในการได้รับคำตอบที่อาจถือว่าเป็นอันตราย ซึ่งรวมถึงวาจาสร้างความเกลียดชังและเนื้อหาเกี่ยวกับเรื่องเพศอย่างโจ่งแจ้ง
  • ตั้งค่าคำสั่งของระบบเพื่อควบคุมลักษณะการทำงานของโมเดล ฟีเจอร์นี้เปรียบเสมือนข้อความนำที่คุณเพิ่มก่อนที่โมเดลจะได้รับคำสั่งเพิ่มเติมจากผู้ใช้ปลายทาง
  • ส่งสคีมาการตอบกลับพร้อมกับพรอมต์เพื่อระบุสคีมาเอาต์พุตที่เฉพาะเจาะจง ฟีเจอร์นี้ใช้กันโดยทั่วไปเมื่อสร้างเอาต์พุต JSON แต่ก็ใช้สำหรับงานการแยกประเภทได้เช่นกัน (เช่น เมื่อต้องการให้โมเดลใช้ป้ายกำกับหรือแท็กที่เฉพาะเจาะจง)