How to create your own AI voice assistant chat bot using Python


Before we get started let us understand a few things.

Ollama is a library of Large Language Models. It has tons of models to offer.

To install it open your terminal and type

curl -fsSL https://ollama.com/install.sh | sh

Next select your desired model to run. You can make your selection on ollama’s website. For the purpose of this experiment we will be using gemma3:1b for it’s speed.

Let us install our model using the following command:

ollama pull gemma3:1b

To get started we need 3 libraries. Install them using pip install terminal command respectively. Then create an empty python script and let us start importing them by typing:

import speech_recognition as sr
import pyttsx3
import ollama

Now we initialize the speech recognizer

r = sr.Recognizer()

Then create the following function

def SpeakText(command):

# Initialize the engine
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()

# we initialize our model as well
messages = [
{“role”: “system”, “content”: “You are a helpful assistant who gives concise answers.”}
]

Now we create a loop for the user to infinitely speak to our bot:

  while(1):
# Exception handling to handle
# exceptions at the runtime
try:
    # use the microphone as source for input.
    with sr.Microphone() as source:

        # wait for a second to let the recognizer
        # adjust the energy threshold based on
        # the surrounding noise level 
        r.adjust_for_ambient_noise(source, duration=0.2)

        #listens for the user's input 
        audio = r.listen(source)

        # Using google to recognize audio
        MyText = r.recognize_google(audio)
        MyText = MyText.lower()

        user_input = MyText
        print('user_input :',user_input)

        if user_input:
           if(user_input == 'goodbye'):
                	SpeakText('shutting down')
                	break
           else:
                        reply = ollama.generate(model='gemma3:1b', prompt=user_input)
                        print(reply)
                        print("Summary:", reply['response'])
                        SpeakText(reply['response']) 
        else:
            print('no user input')



except sr.RequestError as e:
       print('Could not request results')
    # print('Could not request results; {0}'.format(e))

except sr.UnknownValueError:
    print('unknown error occurred')

This is how we have create an infinite loop for the bot to handle our speech and reply in return.

Once the bot detects goodbye, it will exit the loot and the code will end.

Make sure you have a good microphone on your headset.

Congralutions! You have now create your very own AI Voice Assistant Chatbot!

Leave a Comment