iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎤

Implementing a Voice Chatbot Using Chrome's Built-in Gemini Nano and Web Speech API

に公開

Introduction

In this article, I will explain how to implement a voice-interactive chatbot by combining Chrome's built-in AI model, "Gemini Nano," with the Web Speech API.

Demo

First of all, please take a look at the following demo. (To enable Gemini Nano in Chrome, please refer to "Trying out Chrome's built-in AI Gemini Nano" for configuration.)

I recorded a video of it in action.

https://twitter.com/kentaro/status/1804919054949601657

Main Technical Points

The technical points of this demo are as follows:

  1. Use of Chrome's built-in Gemini Nano
  2. Speech recognition and synthesis using the Web Speech API

Using Gemini Nano

Gemini Nano is an AI model built into Chrome 126 and later. It is used as follows:

const generate = async (message, language) => {
  if (!window.ai) {
    console.error("AI module is not available");
    return "AI is not available.";
  }
  try {
    const session = await window.ai.createTextSession();
    const prompt = `
User language: ${language}
User input: ${message}
Assistant response (concise, in the same language as the user):`;

    const response = await session.prompt(prompt);
    console.log("AI response:", response);
    return response.trim();
  } catch (error) {
    console.error("AI error:", error);
    return "An error occurred.";
  }
};

In this implementation, the window.ai object is used to create a text session and send a prompt to obtain a response from the AI.

Using the Web Speech API

We use the Web Speech API to implement speech recognition and speech synthesis.

Speech Recognition

const recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.lang = 'ja-JP';

recognition.onresult = async (event) => {
  const result = event.results[event.results.length - 1];
  const transcript = result[0].transcript.trim();
  console.log('Recognized text:', transcript);

  // Display the user's utterance immediately
  addMessageToChat(transcript, true);

  if (result.isFinal) {
    const language = recognition.lang;
    try {
      console.log('Querying AI...');
      const reply = await generate(transcript, language);
      console.log('AI response:', reply);

      // Display the AI's response
      addMessageToChat(reply, false);

      // Speech synthesis
      const utterance = new SpeechSynthesisUtterance(reply);
      utterance.voice = voices.find(v => v.name === $('#voice').value);
      speechSynthesis.speak(utterance);
    } catch (error) {
      console.error('AI response generation error:', error);
      addMessageToChat("An error occurred while generating the AI response.", false);
    }
  }
};

Speech Synthesis

const utterance = new SpeechSynthesisUtterance(reply);
utterance.voice = voices.find(v => v.name === $('#voice').value);
speechSynthesis.speak(utterance);

Summary

By combining Chrome's built-in Gemini Nano with the Web Speech API, we were able to implement a chatbot with the following features:

  1. Lightweight AI functionality that works entirely within the browser
  2. Real-time speech recognition and speech synthesis

This implementation method expands the possibilities for new interactions in web applications. However, it is an experimental feature at this time, and we need to keep an eye on future updates.

References

  1. Trying out Chrome's built-in AI "Gemini Nano"
  2. I tried Chrome's built-in AI "Gemini Nano"
  3. Chromium Source Code
GitHubで編集を提案

Discussion