Create Your Voice-Controlled AI Assistant with Python and ChatGPT

Updated on Oct 27,2025

This guide explores how to create your very own AI voice assistant using Python and ChatGPT. It covers project setup, required libraries, virtual environments, and detailed code snippets. Whether you're a seasoned developer or just starting, this tutorial will provide the knowledge to craft an AI assistant to understand voice commands, and interact with ChatGPT for intelligent responses.

Key Points

Learn to set up a virtual environment for your AI assistant project in Python.

Discover how to install and utilize essential libraries such as SpeechRecognition, pyttsx3, and OpenAI.

Understand the basics of creating functions for voice recognition, text processing, and synthesized speech.

Create personalized voice controlled AI assistant.

Explore techniques for handling different types of user commands and generating responses using ChatGPT.

Building a Voice-Controlled AI Assistant

Core Code Structure and Functions

Once the environment is set up, you can proceed to write the Python code that will power your AI assistant. This involves creating functions for voice recognition, text processing, and synthesized speech. Creating each functionality is detailed as follows:

  1. Import Necessary Libraries:

    • Begin by importing the required libraries into your Python script

      . This will give you access to the functionalities you need for your AI assistant:

      import speech_recognition as sr
      import pyttsx3
      import datetime
      import webbrowser
      import os
      from dotenv import load_dotenv
      from openai import OpenAI
  2. Set Up the Text-to-Speech Engine:

    • Initialize the pyttsx3 engine for converting text to speech :
      engine = pyttsx3.init()

      This initializes the text-to-speech engine, which will be used to convert text responses into audible speech.

  3. Create a Speak Function:

    • Define a function to handle the text-to-speech conversion :
      def speak(text):
      print(f"Assistant: {text}")
      engine.say(text)
      engine.runAndWait()

      This speak function takes a text string as input and uses pyttsx3 to convert it into speech. The text is also printed to the console for debugging and monitoring purposes.

  4. Define a Listen Function:

    • Craft a function that captures audio from the microphone and converts it into text. This is done with the help of the SpeechRecognition library :
      
      def listen():
      recognizer = sr.Recognizer()
      with sr.Microphone() as source:
      print("Listening...")
      recognizer.adjust_for_ambient_noise(source)
      audio = recognizer.listen(source)

    try: command = recognizer.recognize_google(audio) print(f"You said: {command}") except sr.UnknownValueError: speak("Sorry, I didn't catch that") return "" except sr.RequestError as e: speak("Speech service is not working") return ""

    return command.lower()

    
    The `listen` function sets up a speech recognizer and microphone, listens for audio input, and uses Google’s speech recognition service to transcribe the audio into text. Any exceptions, such as an unknown voice or service unavailability, are caught and handled appropriately.
  5. Chat with ChatGPT Function:

    • Integrate with the OpenAI API to fetch a response from ChatGPT. You'll need to define a function that takes a question as input and returns the AI's answer, like so :
      
      def chat_with_gpt(question):
      load_dotenv()
      client = OpenAI(
      api_key=os.getenv('OPENAI_API_KEY'),
      )

    try: response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[{ "role": "user", "content": question, }], ) answer = response.choices[0].message.content return answer except Exception as e: print(f"ChatGpt Error {e}") return "I couldn't get a response from ChatGpt"

    
    This function calls the OpenAI API, sending the user's question and receiving a response from the ChatGPT model. It manages exceptions and retrieves the relevant text from the API’s response. This is where the power of ChatGPT comes into play, enabling your assistant to provide intelligent and contextual replies.
  6. Run Assistant Function : This the Main function and starting point of our application.

    def run_assistant():
    speak("Hello! I'm your AI assistant, What can I do for you?")
    while True:
    command = listen()
    
    if "time" in command:
    now = datetime.datetime.now().strftime("%H:%M %p")
    speak(f"The time is: {now}")
    
    elif "open youtube" in command:
    speak("Opening YouTube")
    webbrowser.open("https://www.youtube.com")
    
    elif "open google" in command:
    speak("Opening google")
    webbrowser.open("https://www.google.com")
    
    elif "stop" in command or "exit" in command:
    speak("Good Bye Friend!")
    break
    
    elif command:
    answer = chat_with_gpt(command)
    speak(answer)
  7. Running The Program :

if __name__ == '__main__':
 run_assistant()

By running this code the project starts listening to our commands.

Customizing Your AI Assistant

Customizing the Code for Specific Tasks

To truly make the AI assistant your own, consider the following customizations:

  • Extending Functionality: Add more elif command statements to handle additional tasks such as:

    • Opening specific websites or applications.
    • Setting reminders.
    • Controlling smart home devices.
  • Personalizing Responses: Tailor the assistant's responses to match your preferences. You can modify the speak function to use different voices, tones, or greetings.

  • Implementing Context Awareness: Enhance the assistant’s memory and awareness by storing conversation history and user preferences. This allows the assistant to provide more relevant and personalized responses based on the conversation context.

  • Adding More Error Handling: Implement error handling to cover other situations such as network issues, API rate limits and so on. Providing useful error messages helps manage issues better.

  • Creating a More Engaging Personality: Play around and make your AI more conversational using certain coding techniques. Add functions to respond to different feelings or respond more naturally to conversational cues to do this. You should also consider other factors, such as your personality, to have more engaging conversations. These factors might also provide an added security measure.

How to use Your AI voice assistant

Key Aspects

Here’s how your user can experience your new voice assistant

:

  1. Start a new voice assistant. You can create new folders for all your files, and rename it to easily remember what it is.

  2. Begin the program. Then, you can type python assistent.py to start the program.

  3. Engage in conversation By giving the right command, the assistant can follow commands such as, looking something up, making jokes and more.

Useful Markdown Tables For Improved Structure and Readability

Tables can organize information neatly and increase readability. Here's how one could be implemented for comparing different Voice Assistants:

Comparison of Voice Assistants

Feature Custom AI Assistant Alexa Google Assistant
Customization High Medium Medium
Privacy High Medium Medium
Task Automation Limited High High
Integration Limited Extensive Extensive
Development Effort High N/A N/A

Building a Custom Voice-Controlled AI Assistant: Weighing the Pros and Cons

👍 Pros

Tailor the AI assistant's responses and functionality to meet precise needs.

Complete ownership of data and processing, reducing reliance on external services.

Enhances programming skills and understanding of AI and natural language processing.

👎 Cons

Requires coding, testing, and debugging, which can be time-consuming.

Dependency management with libraries and compatibility issues can be complex.

Requires constant updates and revisions to keep with changes in technology.

FAQ

What are the key libraries needed to build this voice-controlled AI assistant?
The main libraries include SpeechRecognition (for voice input processing), pyttsx3 (for text-to-speech), OpenAI (for accessing ChatGPT), and python-dotenv (for managing environment variables such as the OpenAI API key).
How do I activate the virtual environment in my project?
The activation command depends on your operating system: 'venv\Scripts\activate' for Windows and 'source venv/bin/activate' for macOS/Linux.
How can I customize my AI assistant?
You can extend functionality by adding more elif command statements to handle new tasks, personalize responses, implement context awareness by storing conversation history and user preferences, add more error handling, and create a more engaging personality.

Related Questions

What can be done to resolve the 'AttributeError: Could not find PyAudio; check installation' error?
In Python, you may encounter an 'AttributeError: Could not find PyAudio; check installation' error during speech-recognition development. Fortunately, there are strategies and procedures to resolve it to continue developing. This Python error, often encountered in speech recognition projects, indicates an issue with the PyAudio library installation. PyAudio is crucial for capturing and playing audio in Python applications. To resolve this, verify the library's installation and ensure proper setup for speech recognition tasks. Ensure PyAudio is installed: To begin, ensure that the PyAudio library is installed correctly. The basic step involves using pip, Python's package installer. Open your command line or terminal and execute the following command: pip install PyAudio Consider Using a Conda Environment: Conda can manage Python environments and library installations. Create a new environment with the command: conda create -n myenv python=3.x conda activate myenv conda install -c conda-forge pyaudio Update pip: Sometimes outdated pip can cause issues. Ensure pip is up to date using: pip install --upgrade pip These steps should get you started on fixing this annoying problem!

Most people like