AI Chat Bot with voice commands using ChatGPT

AI Chat Bot with voice commands using ChatGPT


In this project, our goal was to build a system allowing users to search for information using voice commands. We decided to integrate ChatGPT, a large language model developed by OpenAI, to interpret and process the voice commands. In this blog post, we will walk through the process of setting up ChatGPT, using it for voice command search, and the results we achieved.

Setting up the fronted

We have used basic HTML and vanilla javascript in the frontend.

  1. In the index.html file, we have just used an input field and a chat container for the results.
    <div id="app">
      <div id="chat_container"></div>
        <textarea name="prompt" id="pmpt" rows="1" cols="1" placeholder="Ask Chatlink by typing or voice"></textarea>
        <button type="button" id="record"><img src="assets/record.png" alt="send" /></button>
        <button type="submit"><img src="assets/send.svg" alt="send" />
    <script type="module" src="script.js"></script>
  1. In the script file, we have used some function loader, typeText, chatStripe, record, and handleSubmit

  2. The loader function sets the text content of an element to an empty string and starts a timer that alternates between adding a period "." to the element's text content and resetting the text content to an empty string every 300 milliseconds, creating a loading indicator effect. The timer can be stopped by calling the "clearInterval" function with the "loadInterval" variable as an argument.

let loadInterval
function loader(element) {
  element.textContent = ''
  loadInterval = setInterval(() => {
    element.textContent += '.'
    if (element.textContent === '....') {
      element.textContent = ''
  }, 300)
  1. The typeText creates a typing effect by updating an element's innerHTML with a string of text, one character at a time, at an interval of 20 milliseconds. When all of the characters in the text have been typed, the interval timer is cleared.
function typeText(element, text) {
  let i = 0
  const interval = setInterval(() => {
    if (i < text.length) {
      element.innerHTML += text.charAt(i)
    } else {
  }, 20)
  1. The chatStripe function generates HTML code for a chat message stripe. The "wrapper" element's class is set to "ai" if the message is from an AI, and the "message" element includes the message text and a unique identifier as its id attribute. The "profile" element includes an image element with a source attribute that is set to either a user image or a bot image.
function chatStripe(isAi, value, uniqueId) {
  return `
        <div class="wrapper ${isAi && 'ai'}">
            <div class="chat">
                <div class="profile">
                      src=${isAi ? bot : user}
                      alt="${isAi ? 'bot' : 'user'}"
                <div class="message" id=${uniqueId}>${value}</div>
  1. The handleSubmit is an event handler for a form submission event. It creates a FormData object from the form, adds a chat message stripe to a chat container element with the form data as the message text, resets the form, generates a unique identifier, adds another chat message stripe to the chat container with the unique identifier as its id attribute, scrolls the chat container to the bottom, selects the chat message element with the unique identifier, calls a function called "loader" with the element as an argument, sends a POST request to a specified URL with the form data as the request body, and handles the response depending on whether it is successful or not.
const handleSubmit = async (e) => {
  const data = new FormData(form)
  chatContainer.innerHTML += chatStripe(false, data.get('prompt'))
  const uniqueId = generateUniqueId()
  chatContainer.innerHTML += chatStripe(true, ' ', uniqueId)
  chatContainer.scrollTop = chatContainer.scrollHeight
  const messageDiv = document.getElementById(uniqueId)
  const response = await fetch('', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    body: JSON.stringify({
      prompt: data.get('prompt'),
  messageDiv.innerHTML = ''
  if (response.ok) {
    const data = await response.json()
    const parsedData =
    typeText(messageDiv, parsedData)
  } else {
    const error = await response.text()
    messageDiv.innerHTML = 'something went wrong'
  1. The record function uses the Web Speech API to enable voice recognition. It generates a unique identifier, adds a chat message stripe to a chat container element, selects the chat message element, calls a function called "loader" with the element as an argument, and starts a new speech recognition process. When the recognition process produces a result, the function updates a form field with the transcript, stops the recognition process, removes the chat message stripe, and calls a function called "handleSubmit" with an event object as an argument.
function record() {
  const uniqueId = generateUniqueId()
  chatContainer.innerHTML += chatStripe(false, ' ', uniqueId)
  chatContainer.scrollTop = chatContainer.scrollHeight
  const messageDiv = document.getElementById(uniqueId)
  var recognition = new webkitSpeechRecognition()
  recognition.continuous = false
  recognition.interimResults = false
  recognition.lang = 'en-US'
  recognition.onresult = function (e) {
    document.getElementById('pmpt').value = e.results[0][0].transcript

Backend and Integration with ChatGPT

  1. First, we need an openAI account. Sign in to openai then click on the profile icon to get API keys.

  2. For the configuration, code go to Playground on openai website and then choose text-davinci-003, which is the best for text and code prompts. You can also choose other models based on the requirements. Then click on view code to get the code

  3. Make a basic node server using express and then create a post request which will look like the below code'/', async (req, res) => {
  try {
    const prompt = req.body.prompt
    const response = await openai.createCompletion({
      model: 'text-davinci-003',
      prompt: `${prompt}`,
      temperature: 0.3,
      max_tokens: 3000,
      top_p: 1,
      frequency_penalty: 0.5,
      presence_penalty: 0,
  } catch (err) {
      error: err,

This function is a route handler for an HTTP POST request in an Express.js app. When the route is accessed, it retrieves the "prompt" field from the request body and makes a request to the OpenAI API using the "createCompletion" method, passing in a set of options as an object. It sends a response with a status code of 200 and a JSON object with the bot's response as the "bot" field.

Results and Conclusion

Overall, we were pleased with the results we achieved using ChatGPT in our voice command search system. It was able to accurately interpret and respond to a wide range of voice commands, improving the user experience. In conclusion, ChatGPT proved to be a powerful and effective tool for interpreting and processing voice commands in our project. We look forward to continuing to explore the capabilities of this impressive language model in the future. For the fun part, the content of this blog is generated using this Chat Bot only.

The reference for this project has been taken from JS Mastery. Many thanks to him for creating such awesome videos.


Repository Link


Show some love to the blog ❤️..