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

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

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

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

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

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

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

กำหนด GenerationConfig สำหรับโมเดลส่วนใหญ่ของ Gemini ซึ่งรวมถึง โมเดลการใช้งานทั่วไป โมเดลการสร้างรูปภาพ ("โมเดล Nano Banana") และ โมเดล Text-to-Speech (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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
let config = GenerationConfig(
  candidateCount: 1,
  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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
val config = generationConfig {
    candidateCount = 1
    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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.candidateCount = 1;
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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
const generationConfig = {
  candidate_count: 1,
  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.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
final generationConfig = GenerationConfig(
  candidateCount: 1,
  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

ตั้งค่าพารามิเตอร์ใน GenerationConfig เป็นส่วนหนึ่งของการสร้างอินสแตนซ์ GenerativeModel


// ...

// Set parameter values in a `GenerationConfig`.
// IMPORTANT: Example values shown here. Make sure to update for your use case.
// Note that temperature, top-K, and top-P are deprecated and ignored by the latest Gemini models.
var generationConfig = new GenerationConfig(
  candidateCount: 1,
  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 หากต้องการใช้การกำหนดค่าอื่น ให้สร้างและใช้อินสแตนซ์ใหม่ ที่มีการกำหนดค่าอื่น

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 = 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

ตั้งค่าพารามิเตอร์ใน 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(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

ตั้งค่าพารามิเตอร์ใน 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



คำอธิบายพารามิเตอร์

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

โปรดทราบว่าอุณหภูมิ, top-K และ top-P เลิกใช้งานแล้วและโมเดล Gemini รุ่นล่าสุดจะไม่สนใจพารามิเตอร์เหล่านี้

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

บูลีน

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

false
จำนวนผู้สมัคร
candidateCount
ระบุจำนวนรูปแบบการตอบกลับที่จะแสดง สำหรับคำขอแต่ละรายการ ระบบจะเรียกเก็บเงินสำหรับโทเค็นเอาต์พุตของผู้สมัครรับเลือกทั้งหมด แต่จะเรียกเก็บเงิน สำหรับโทเค็นอินพุตเพียงครั้งเดียว

ค่าที่รองรับ: 1 - 8 (รวมทุกค่าในช่วง)

ใช้ได้เมื่อใช้ generateContent เท่านั้น

ไม่เกี่ยวข้องเมื่อใช้โมเดล generateContentStream, TTS หรือโมเดล Live API

1
โทเค็นเอาต์พุตสูงสุด
maxOutputTokens
ระบุจำนวนโทเค็นสูงสุดที่สร้างได้ใน การตอบกลับ --- ---
ค่าปรับตามความถี่
frequencyPenalty

ควบคุมความน่าจะเป็นของการรวมโทเค็นที่ปรากฏซ้ำๆ ใน คำตอบที่สร้างขึ้น

ค่าบวกจะลงโทษโทเค็นที่ปรากฏซ้ำๆ ใน เนื้อหาที่สร้างขึ้น ซึ่งจะลดความน่าจะเป็นของการทำเนื้อหาซ้ำ

ใช้ไม่ได้กับโมเดล TTS ---
การลงโทษเมื่อไม่เข้าร่วม
presencePenalty

ควบคุมความน่าจะเป็นของการรวมโทเค็นที่ปรากฏใน คำตอบที่สร้างขึ้นแล้ว

ค่าบวกจะลงโทษโทเค็นที่ปรากฏในเนื้อหาที่สร้างขึ้นแล้ว ซึ่งจะเพิ่มโอกาสในการสร้างเนื้อหาที่หลากหลายมากขึ้น

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

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

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

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

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

  • ควบคุมปริมาณการคิดที่โมเดลทำได้โดยใช้ระดับการคิด (โมเดล Gemini 3.x ขึ้นไป)
  • ควบคุมว่าจะรวมสรุปความคิดหรือไม่

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

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

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

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

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

ใช้ได้เฉพาะเมื่อใช้Geminiโมเดลรูปภาพ ("Nano Banana" )

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

ต้องระบุสำหรับโมเดล TTS
Puck





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

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