ตัวเลือกการกำหนดค่าสำหรับ Live API


แม้จะใช้การติดตั้งใช้งานพื้นฐานสำหรับ Live API คุณก็สร้างการโต้ตอบที่น่าสนใจและมีประสิทธิภาพสำหรับผู้ใช้ได้ คุณสามารถปรับแต่งประสบการณ์การใช้งานให้ดียิ่งขึ้นได้โดยใช้ตัวเลือกการกำหนดค่าต่อไปนี้



เสียงและภาษาของคำตอบ

คุณสามารถทำให้โมเดลตอบกลับด้วยเสียงที่เฉพาะเจาะจงและกำหนดให้โมเดลตอบกลับในภาษาต่างๆ ได้

ระบุเสียงตอบกลับ

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

Live API รองรับเสียง HD สังเคราะห์ที่แตกต่างกัน 30 เสียง โดยแต่ละเสียงมีลักษณะเฉพาะที่แตกต่างกัน คุณสามารถดูรายการตัวเลือกเสียงตอบกลับและฟังตัวอย่างของแต่ละเสียงได้โดยขยายส่วนด้านล่าง

หากคุณไม่ได้ระบุเสียงตอบกลับ ระบบจะใช้เสียง Puck เป็นค่าเริ่มต้น

หากต้องการระบุเสียงตอบกลับ ให้ตั้งชื่อเสียงภายในออบเจ็กต์ speechConfig เป็นส่วนหนึ่งของการกำหนดค่าโมเดล

Swift


// ...

let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_API_MODEL_NAME",
  // Configure the model to use a specific voice for its audio response.
  generationConfig: LiveGenerationConfig(
    responseModalities: [.audio],
    speech: SpeechConfig(voiceName: "VOICE_NAME")
  )
)

// ...

Kotlin


// ...

val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_API_MODEL_NAME",
    // Configure the model to use a specific voice for its audio response.
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO
        speechConfig = SpeechConfig(voice = Voice("VOICE_NAME"))
    }
)

// ...

Java


// ...

LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
    "GEMINI_LIVE_API_MODEL_NAME",
    // Configure the model to use a specific voice for its audio response.
    new LiveGenerationConfig.Builder()
        .setResponseModality(ResponseModality.AUDIO)
        .setSpeechConfig(new SpeechConfig(new Voice("VOICE_NAME")))
        .build()
);

// ...

Web


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

const liveModel = getLiveGenerativeModel(ai, {
  model: "GEMINI_LIVE_API_MODEL_NAME",
  // Configure the model to use a specific voice for its audio response.
  generationConfig: {
    responseModalities: [ResponseModality.AUDIO],
    speechConfig: {
      voiceConfig: {
        prebuiltVoiceConfig: { voiceName: "VOICE_NAME" },
      },
    },
  },
});

// ...

Dart


// ...

final _liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_LIVE_API_MODEL_NAME',
  // Configure the model to use a specific voice for its audio response.
  liveGenerationConfig: LiveGenerationConfig(
    responseModalities: [ResponseModalities.audio],
    speechConfig: SpeechConfig(voiceName: 'VOICE_NAME'),
  ),
);

// ...

Unity


// ...

var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
    modelName: "GEMINI_LIVE_API_MODEL_NAME",
    // Configure the model to use a specific voice for its audio response
    liveGenerationConfig: new LiveGenerationConfig(
        responseModalities: new[] { ResponseModality.Audio },
        speechConfig: SpeechConfig.UsePrebuiltVoice("VOICE_NAME")
    )
);

// ...

กำหนดภาษาของคำตอบ

โมเดล Live API จะเลือกภาษาที่เหมาะสมสำหรับคำตอบโดยอัตโนมัติ

หากต้องการให้โมเดลตอบกลับเป็นภาษาอื่นที่ไม่ใช่ภาษาอังกฤษหรือเป็นภาษาใดภาษาหนึ่งเสมอ คุณสามารถกำหนดการตอบกลับของโมเดลได้โดยใช้คำสั่งของระบบ เช่น ตัวอย่างต่อไปนี้

  • ย้ำกับโมเดลว่าภาษาที่ไม่ใช่ภาษาอังกฤษอาจเหมาะสม

    Listen to the speaker carefully. If you detect a non-English language, respond
    in the language you hear from the speaker. You must respond unmistakably in the
    speaker's language.
    
  • บอกให้โมเดลตอบกลับเป็นภาษาที่เฉพาะเจาะจงเสมอ

    RESPOND IN LANGUAGE. YOU MUST RESPOND UNMISTAKABLY IN LANGUAGE.
    



การถอดเสียงเป็นคำสำหรับอินพุตและเอาต์พุตเสียง

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

คุณสามารถรับการถอดเสียงเป็นคำของอินพุตเสียงและการตอบกลับด้วยเสียงของโมเดลได้ โดยเป็นส่วนหนึ่งของการตอบกลับของโมเดล คุณตั้งค่านี้เป็นส่วนหนึ่งของการกำหนดค่าโมเดล

  • หากต้องการถอดเสียงอินพุตเสียง ให้เพิ่ม inputAudioTranscription

  • หากต้องการการถอดเสียงเป็นคำสำหรับการตอบกลับด้วยเสียงของโมเดล ให้เพิ่ม outputAudioTranscription

โปรดทราบดังต่อไปนี้

  • คุณสามารถกำหนดค่าโมเดลให้แสดงผลการถอดเสียงทั้งอินพุตและเอาต์พุต (ดังที่แสดงในตัวอย่างต่อไปนี้) หรือจะกำหนดค่าให้แสดงผลเพียงอย่างใดอย่างหนึ่งก็ได้

  • ระบบจะสตรีมข้อความถอดเสียงพร้อมกับเสียง ดังนั้นจึงควรเก็บรวบรวมข้อความถอดเสียงเหมือนกับที่เก็บรวบรวมส่วนข้อความในแต่ละเทิร์น

  • ระบบจะอนุมานภาษาของการถอดเสียงเป็นคำจากอินพุตเสียงและการตอบกลับด้วยเสียงของโมเดล

Swift


// ...

let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_API_MODEL_NAME",
  // Configure the model to return transcriptions of the audio input and output.
  generationConfig: LiveGenerationConfig(
    responseModalities: [.audio],
    inputAudioTranscription: AudioTranscriptionConfig(),
    outputAudioTranscription: AudioTranscriptionConfig()
  )
)

var inputTranscript: String = ""
var outputTranscript: String = ""

do {
  let session = try await liveModel.connect()
  for try await response in session.responses {
    if case let .content(content) = response.payload {
      if let inputText = content.inputAudioTranscription?.text {
        // Handle transcription text of the audio input.
        inputTranscript += inputText
      }

      if let outputText = content.outputAudioTranscription?.text {
        // Handle transcription text of the audio output.
        outputTranscript += outputText
      }

      if content.isTurnComplete {
        // Log the transcripts after the current turn is complete.
        print("Input audio: \(inputTranscript)")
        print("Output audio: \(outputTranscript)")

        // Reset the transcripts for the next turn.
        inputTranscript = ""
        outputTranscript = ""
      }
    }
  }


} catch {
  // Handle error
}

// ...

Kotlin


// ...

val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_API_MODEL_NAME",
    // Configure the model to return transcriptions of the audio input and output.
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO
        inputAudioTranscription = AudioTranscriptionConfig()
        outputAudioTranscription = AudioTranscriptionConfig()
   }
)

val liveSession = liveModel.connect()

fun handleTranscription(input: Transcription?, output: Transcription?) {
    input?.text?.let { text ->
        // Handle transcription text of the audio input.
        println("Input Transcription: $text")
    }
    output?.text?.let { text ->
        // Handle transcription text of the audio output.
        println("Output Transcription: $text")
    }
}

liveSession.startAudioConversation(null, ::handleTranscription)

// ...

Java


// ...

ExecutorService executor = Executors.newFixedThreadPool(1);

LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
    "GEMINI_LIVE_API_MODEL_NAME",
    // Configure the model to return transcriptions of the audio input and output.
    new LiveGenerationConfig.Builder()
            .setResponseModality(ResponseModality.AUDIO)
            .setInputAudioTranscription(new AudioTranscriptionConfig())
            .setOutputAudioTranscription(new AudioTranscriptionConfig())
            .build()
    );

LiveModelFutures liveModel = LiveModelFutures.from(lm);
ListenableFuture<LiveSessionFutures> sessionFuture = liveModel.connect();

Futures.addCallback(sessionFuture, new FutureCallback<LiveSessionFutures>() {
    @Override
    public void onSuccess(LiveSessionFutures ses) {
        LiveSessionFutures session = ses;
        session.startAudioConversation((Transcription input, Transcription output) -> {
            if (input != null) {
                // Handle transcription text of the audio input.
                System.out.println("Input Transcription: " + input.getText());
            }
            if (output != null) {
                // Handle transcription text of the audio output.
                System.out.println("Output Transcription: " + output.getText());
            }
            return null;
        });
    }

    @Override
    public void onFailure(Throwable t) {
        // Handle exceptions
        t.printStackTrace();
    }
}, executor);

// ...

Web


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

const liveModel = getLiveGenerativeModel(ai, {
  model: 'GEMINI_LIVE_API_MODEL_NAME',
  // Configure the model to return transcriptions of the audio input and output.
  generationConfig: {
    responseModalities: [ResponseModality.AUDIO],
    inputAudioTranscription: {},
    outputAudioTranscription: {},
  },
});

const liveSession = await liveModel.connect();

liveSession.sendAudioRealtime({ data, mimeType: "audio/pcm" });

const messages = liveSession.receive();
for await (const message of messages) {
  switch (message.type) {
    case 'serverContent':
      if (message.inputTranscription) {
        // Handle transcription text of the audio input.
        console.log(`Input transcription: ${message.inputTranscription.text}`);
      }
      if (message.outputTranscription) {
        // Handle transcription text of the audio output.
        console.log(`Output transcription: ${message.outputTranscription.text}`);
      } else {
      	 // Handle other message types (modelTurn, turnComplete, interruption).
      }
    default:
      // Handle other message types (toolCall, toolCallCancellation).
  }
}

// ...

Dart


// ...

final _liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_LIVE_API_MODEL_NAME',
  // Configure the model to return transcriptions of the audio input and output.
  liveGenerationConfig: LiveGenerationConfig(
    responseModalities: [ResponseModalities.audio],
    inputAudioTranscription: AudioTranscriptionConfig(),
    outputAudioTranscription: AudioTranscriptionConfig(),
  ),
);

final LiveSession _session = await _liveModel.connect();

await for (final response in _session.receive()) {
  if (response.message case LiveServerContent message) {
    if (message.inputTranscription?.text case final inputText?) {
      // Handle transcription text of the audio input.
      print('Input: $inputText');
    }

    if (message.outputTranscription?.text case final outputText?) {
      // Handle transcription text of the audio output.
      print('Output: $outputText');
    }
  }
}

// ...

Unity


// ...

var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
    modelName: "GEMINI_LIVE_API_MODEL_NAME",
    // Configure the model to return transcriptions of the audio input and output
    liveGenerationConfig: new LiveGenerationConfig(
        responseModalities: new[] { ResponseModality.Audio },
        inputAudioTranscription: new AudioTranscriptionConfig(),
        outputAudioTranscription: new AudioTranscriptionConfig()
    )
);

try
{
    var session = await liveModel.ConnectAsync();
    var stream = session.ReceiveAsync();
    await foreach (var response in stream) {
        if (response.Message is LiveSessionContent sessionContent) {
            if (!string.IsNullOrEmpty(sessionContent.InputTranscription?.Text)) {
              // handle transcription text of input audio
            }

            if (!string.IsNullOrEmpty(sessionContent.OutputTranscription?.Text)) {
              // handle transcription text of output audio
            }
        }
    }
}
catch (Exception e)
{
    // Handle error
}

// ...



การตรวจจับกิจกรรมเสียงพูด (VAD)

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

คุณสามารถปรับแต่งลักษณะการทำงานของ VAD ได้ รวมถึงการจัดการการขัดจังหวะ หรือจะปิดใช้ VAD อัตโนมัติเพื่อควบคุมการเปลี่ยนเทิร์นในการสนทนาด้วยตนเองทั้งหมดก็ได้ (เช่น สำหรับอินเทอร์เฟซแบบ Push-to-Talk)

กำหนดค่าการตรวจจับกิจกรรมเสียงพูด (VAD)

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

ตัวอย่างต่อไปนี้แสดงวิธีปรับแต่งลักษณะการทำงานของ VAD โดยการตั้งค่า realtimeInputConfig เป็นส่วนหนึ่งของการกำหนดค่าโมเดล ดูข้อมูลเกี่ยวกับการตั้งค่าที่ใช้ได้สำหรับการกำหนดค่านี้ในตารางด้านล่างในส่วนนี้

Swift


// ...

let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_API_MODEL_NAME",
  generationConfig: LiveGenerationConfig(
    responseModalities: [.audio],
    // Configure voice activity detection (VAD), interruption handling, and turn coverage.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    realtimeInputConfig: RealtimeInputConfig(
      automaticActivityDetection: ActivityDetectionConfig(
        startSensitivity: .low,
        endSensitivity: .low,
        prefixPadding: .milliseconds(20),
        silenceDuration: .milliseconds(500)
      ),
      activityHandling: .interrupt,
      turnCoverage: .onlyActivity
    )
  )
)

Kotlin


// ...

val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_API_MODEL_NAME",
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO
        // Configure voice activity detection (VAD), interruption handling, and turn coverage.
        // IMPORTANT: Example values shown here. Make sure to update for your use case.
        realtimeInputConfig = realtimeInputConfig {
            automaticActivityDetection = ActivityDetectionConfig.builder()
                .setStartSensitivity(ActivityDetectionConfig.Sensitivity.LOW)
                .setEndSensitivity(ActivityDetectionConfig.Sensitivity.LOW)
                .setPrefixPaddingMs(20)
                .setSilenceDurationMs(500)
                .build()
            activityHandling = RealtimeInputConfig.ActivityHandling.INTERRUPT
            turnCoverage = RealtimeInputConfig.TurnCoverage.ONLY_ACTIVITY
        }
    }
)

Java


// ...

LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
        "GEMINI_LIVE_API_MODEL_NAME",
        new LiveGenerationConfig.Builder()
                .setResponseModality(ResponseModality.AUDIO)
                // Configure voice activity detection (VAD), interruption handling, and turn coverage.
                // IMPORTANT: Example values shown here. Make sure to update for your use case.
                .setRealtimeInputConfig(RealtimeInputConfig.builder()
                        .setAutomaticActivityDetection(ActivityDetectionConfig.builder()
                                .setStartSensitivity(ActivityDetectionConfig.Sensitivity.LOW)
                                .setEndSensitivity(ActivityDetectionConfig.Sensitivity.LOW)
                                .setPrefixPaddingMs(20)
                                .setSilenceDurationMs(500)
                                .build())
                        .setActivityHandling(RealtimeInputConfig.ActivityHandling.INTERRUPT)
                        .setTurnCoverage(RealtimeInputConfig.TurnCoverage.ONLY_ACTIVITY)
                        .build())
                .build()
);

Web

Not yet supported for Web apps - check back soon!

Dart


// ...

final _liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_LIVE_API_MODEL_NAME',
  liveGenerationConfig: LiveGenerationConfig(
    responseModalities: [ResponseModalities.audio],
    // Configure voice activity detection (VAD), interruption handling, and turn coverage.
    // IMPORTANT: Example values shown here. Make sure to update for your use case.
    realtimeInputConfig: RealtimeInputConfig(
      automaticActivityDetection: ActivityDetectionConfig(
        startSensitivity: Sensitivity.low,
        endSensitivity: Sensitivity.low,
        prefixPaddingMS: 20,
        silenceDurationMS: 500,
      ),
      activityHandling: ActivityHandling.interrupt,
      turnCoverage: TurnCoverage.onlyActivity,
    ),
  ),
);

Unity


// ...

var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
    modelName: "GEMINI_LIVE_API_MODEL_NAME",
    liveGenerationConfig: new LiveGenerationConfig(
        responseModalities: new[] { ResponseModality.Audio },
        // Configure voice activity detection (VAD), interruption handling, and turn coverage.
        // IMPORTANT: Example values shown here. Make sure to update for your use case.
        realtimeInputConfig: new RealtimeInputConfig(
            automaticActivityDetection: new ActivityDetectionConfig(
                startSensitivity: ActivityDetectionConfig.Sensitivity.Low,
                endSensitivity: ActivityDetectionConfig.Sensitivity.Low,
                prefixPaddingMS: 20,
                silenceDurationMS: 500
            ),
            activityHandling: RealtimeInputConfig.ActivityHandling.Interrupt,
            turnCoverage: RealtimeInputConfig.TurnCoverage.OnlyActivity
        )
    )
);

การตั้งค่าการกำหนดค่า VAD ที่พร้อมใช้งาน

การตั้งค่า ค่าที่รองรับมีดังนี้ คำอธิบาย
การตรวจจับกิจกรรมอัตโนมัติ (automaticActivityDetection)
startSensitivity
  • LOW (ค่าเริ่มต้น)
  • HIGH

กำหนดความพร้อมของเซิร์ฟเวอร์ในการตรวจจับเมื่อผู้ใช้เริ่มพูด ดังนี้

  • LOW: ไวต่อเสียงเบาน้อยลง ซึ่งจะช่วยป้องกันไม่ให้เสียงแวดล้อมเริ่มการพูดโดยไม่ตั้งใจ
  • HIGH: มีความไวมากขึ้น ซึ่งจะช่วยตรวจจับเสียงพูดที่เบาหรือคำพูดที่นุ่มนวลได้อย่างรวดเร็ว
endSensitivity
  • LOW (ค่าเริ่มต้น)
  • HIGH

กำหนดความรวดเร็วที่เซิร์ฟเวอร์จะตรวจจับเมื่อผู้ใช้หยุดพูด

  • LOW: ไวต่อการหยุดชั่วคราวท้ายประโยคน้อยลง เซิร์ฟเวอร์จะรองรับการลังเลตามธรรมชาติหรือคำที่เบากว่าในตอนท้ายของประโยค
  • HIGH: มีความไวมากขึ้น เซิร์ฟเวอร์จะสรุปเทิร์นการพูดทันทีเมื่อระดับเสียงลดลง
prefixPadding ระยะเวลาในหน่วยมิลลิวินาที
(ค่าเริ่มต้น: ไม่มี (0 มิลลิวินาที))

กำหนดปริมาณเสียงที่จะรวมก่อนตรวจพบคำพูด

บัฟเฟอร์ "ย้อนหลัง" นี้ช่วยให้โมเดลจับจุดเริ่มต้นของคำพูดได้อย่างสมบูรณ์ รวมถึงพยางค์แรกซึ่งมักจะเริ่มก่อนที่ตัวตรวจจับจะทริกเกอร์

  • ค่าเริ่มต้นของ 0 อาจทำให้คำที่ขึ้นต้นถูกตัดออก
  • แนะนำ: 20-50 มิลลิวินาที
silenceDuration ระยะเวลาในหน่วยมิลลิวินาที
(ค่าเริ่มต้น: ~800 มิลลิวินาที)

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

  • แนะนำ (500-800 มิลลิวินาที): ให้ความสมดุลที่ดี โดยโมเดลจะได้รับเสียงที่สมบูรณ์และมีบริบทครบถ้วน ขณะเดียวกันก็รักษาเวลาในการตอบสนองที่เหมาะสมไว้ได้
  • ต่ำเกินไป (ต่ำกว่า 200 มิลลิวินาที): สิ้นสุดการพูดในช่วงที่หยุดพักตามธรรมชาติ โดยแบ่งคำพูดออกเป็นส่วนย่อยๆ ซึ่งอาจทำให้คุณภาพของการถอดเสียงและการตอบกลับลดลง
  • สูงเกินไป (มากกว่า 2,000 มิลลิวินาที): เพิ่มความล่าช้าที่สังเกตเห็นได้ก่อนที่โมเดลจะเริ่มตอบสนอง
ลักษณะการขัดจังหวะ (activityHandling)
activityHandling
  • INTERRUPT (ค่าเริ่มต้น)
  • NO_INTERRUPT

กำหนดวิธีที่เซิร์ฟเวอร์จัดการกิจกรรมของผู้ใช้ที่เข้ามาขณะสร้างคำตอบ

  • INTERRUPT: ยกเลิกคำตอบปัจจุบันของโมเดลทันทีเมื่อตรวจพบกิจกรรมของผู้ใช้
  • NO_INTERRUPT: สร้างคำตอบต่อไปโดยไม่หยุดชะงัก
เปิดการครอบคลุมบริบท (turnCoverage)
turnCoverage
  • ONLY_ACTIVITY (ค่าเริ่มต้น)
  • ALL_INPUT
  • AUDIO_ACTIVITY_AND_ALL_VIDEO

กำหนดอินพุตที่สตรีมซึ่งรวมอยู่ในบริบทเทิร์นการสนทนา

  • ONLY_ACTIVITY: ยกเว้นช่วงที่ไม่มีเสียง โดยเก็บไว้เฉพาะส่วนที่ตรวจพบกิจกรรมของผู้ใช้
  • ALL_INPUT: เก็บอินพุตที่สตรีมทั้งหมด รวมถึงเสียงที่ไม่มีเสียงพูด
  • AUDIO_ACTIVITY_AND_ALL_VIDEO: เก็บเฟรมวิดีโอที่สตรีมทั้งหมด แต่เก็บเฉพาะส่วนของเสียงที่ตรวจพบกิจกรรม

จัดการการขัดจังหวะ

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

  • การสร้างถูกยกเลิก: โมเดลจะหยุดการตอบกลับทันทีและทิ้งการเรียกใช้เครื่องมือที่รอดำเนินการ

  • ประวัติเซสชันถูกตัด: ระบบจะเก็บเฉพาะเนื้อหาที่ส่งไปยังแอปของคุณก่อนการหยุดชะงักไว้ในหน้าต่างบริบทของเซสชัน

  • ระบบจะส่งสัญญาณการขัดจังหวะ: เซิร์ฟเวอร์จะส่งข้อความตอบกลับที่ระบุว่าการสร้างถูกขัดจังหวะ (เช่น wasInterrupted ใน Swift หรือ interrupted ใน Kotlin, Java, เว็บ, Flutter และ Unity)

ปิดใช้การตรวจจับกิจกรรมเสียงพูด (VAD) อัตโนมัติ

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

เมื่อปิดใช้การตรวจหาอัตโนมัติ เซิร์ฟเวอร์จะไม่ตรวจหาคำพูดหรือทริกเกอร์คำตอบโดยอัตโนมัติเมื่อไม่มีเสียงพูด แต่แอปของคุณต้องส่งสัญญาณขอบเขตกิจกรรมของผู้ใช้ด้วยตนเองในระหว่างเซสชันที่ใช้งานอยู่

1. ปิดใช้ VAD อัตโนมัติในการกำหนดค่าโมเดล

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

Swift


// ...

let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_API_MODEL_NAME",
  generationConfig: LiveGenerationConfig(
    responseModalities: [.audio],
    // Disable automatic voice activity detection (VAD).
    realtimeInputConfig: RealtimeInputConfig(
      automaticActivityDetection: .disabled()
    )
  )
)

Kotlin


// ...

val liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_API_MODEL_NAME",
    generationConfig = liveGenerationConfig {
        responseModality = ResponseModality.AUDIO
        // Disable automatic voice activity detection (VAD).
        realtimeInputConfig = realtimeInputConfig {
            automaticActivityDetection = ActivityDetectionConfig.disabled()
        }
    }
)

Java


// ...

LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
        "GEMINI_LIVE_API_MODEL_NAME",
        new LiveGenerationConfig.Builder()
                .setResponseModality(ResponseModality.AUDIO)
                // Disable automatic voice activity detection (VAD).
                .setRealtimeInputConfig(RealtimeInputConfig.builder()
                        .setAutomaticActivityDetection(ActivityDetectionConfig.disabled())
                        .build())
                .build()
);

Web

Not yet supported for Web apps - check back soon!

Dart


// ...

final _liveModel = FirebaseAI.googleAI().liveGenerativeModel(
  model: 'GEMINI_LIVE_API_MODEL_NAME',
  liveGenerationConfig: LiveGenerationConfig(
    responseModalities: [ResponseModalities.audio],
    // Disable automatic voice activity detection (VAD).
    realtimeInputConfig: RealtimeInputConfig(
      automaticActivityDetection: ActivityDetectionConfig.disabled(),
    ),
  ),
);

Unity


// ...

var liveModel = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetLiveModel(
    modelName: "GEMINI_LIVE_API_MODEL_NAME",
    liveGenerationConfig: new LiveGenerationConfig(
        responseModalities: new[] { ResponseModality.Audio },
        // Disable automatic voice activity detection (VAD).
        realtimeInputConfig: new RealtimeInputConfig(
            automaticActivityDetection: ActivityDetectionConfig.Disabled()
        )
    )
);

2. ส่งสัญญาณขอบเขตกิจกรรมของผู้ใช้ด้วยตนเอง

หากปิดใช้ VAD แอปของคุณต้องกำหนดขอบเขตของกิจกรรมของผู้ใช้แต่ละรอบด้วยตนเอง ดังนี้

  1. ส่งสัญญาณการเริ่มกิจกรรม: เรียกใช้ sendStartActivityRealtime ก่อนส่งข้อมูลเสียง (เช่น เมื่อผู้ใช้กดปุ่ม Push-To-Talk)

  2. สตรีมอินพุตของผู้ใช้: สตรีมข้อมูลเสียง (หรือวิดีโอ) อย่างต่อเนื่องโดยใช้ sendAudioRealtime

  3. ส่งสัญญาณสิ้นสุดกิจกรรม: เรียกใช้ sendStopActivityRealtime เมื่อผู้ใช้พูดจบ (เช่น เมื่อผู้ใช้ปล่อยปุ่มกดเพื่อพูด) ซึ่งจะส่งสัญญาณว่าตาของผู้ใช้สิ้นสุดลงแล้วและทริกเกอร์การตอบกลับของโมเดล

Swift

// If you disable automatic voice activity detection (VAD), you need to manually demarcate user activity.

// Signal the start of user activity (for example, on push-to-talk button press).
await session.sendStartActivityRealtime()

// Stream user audio data while the user speaks.
await session.sendAudioRealtime(audioData)

// Signal the end of user activity (for example, on button release) to trigger model response.
await session.sendStopActivityRealtime()

Kotlin

// If you disable automatic voice activity detection (VAD), you need to manually demarcate user activity.

// Signal the start of user activity (for example, on push-to-talk button press).
session.sendStartActivityRealtime()

// Stream user audio data while the user speaks.
session.sendAudioRealtime(audioData)

// Signal the end of user activity (for example, on button release) to trigger model response.
session.sendStopActivityRealtime()

Java

// If you disable automatic voice activity detection (VAD), you need to manually demarcate user activity.

// Signal the start of user activity (for example, on push-to-talk button press).
session.sendStartActivityRealtime();

// Stream user audio data while the user speaks.
session.sendAudioRealtime(audioData);

// Signal the end of user activity (for example, on button release) to trigger model response.
session.sendStopActivityRealtime();

Web

Not yet supported for Web apps - check back soon!

Dart

// If you disable automatic voice activity detection (VAD), you need to manually demarcate user activity.

// Signal the start of user activity (for example, on push-to-talk button press).
await session.sendStartActivityRealtime();

// Stream user audio data while the user speaks.
await session.sendAudioRealtime(audioData);

// Signal the end of user activity (for example, on button release) to trigger model response.
await session.sendStopActivityRealtime();

Unity

// If you disable automatic voice activity detection (VAD), you need to manually demarcate user activity.

// Signal the start of user activity (for example, on push-to-talk button press).
await session.SendStartActivityRealtimeAsync();

// Stream user audio data while the user speaks.
await session.SendAudioRealtimeAsync(audio: audioData);

// Signal the end of user activity (for example, on button release) to trigger model response.
await session.SendStopActivityRealtimeAsync();



การจัดการเซสชัน

ดูข้อมูลเกี่ยวกับหัวข้อต่อไปนี้ที่เกี่ยวข้องกับเซสชัน