This guide will help you set up a voice-controlled chatbot on a Raspberry Pi 4 using OpenAI’s GPT-3.5 model. By the end, you’ll have a chatbot that listens to commands, processes them using OpenAI, and responds to you.
Prerequisites
- Raspberry Pi 4 with Raspberry Pi OS (Raspbian) installed.
- Microphone and Speakers: A USB microphone and speakers connected to the Raspberry Pi.
- Internet Connection: Ensure the Raspberry Pi is connected to the internet.
Setting Up the Raspberry Pi
Step 1: Open the Terminal
First, open the terminal on your Raspberry Pi. You can do this by clicking on the terminal icon in the taskbar or by pressing Ctrl+Alt+T
.
Step 2: Update and Upgrade System
Run the following commands in the terminal to update and upgrade the system packages. Type these commands and press Enter after each one:
sudo apt update
sudo apt upgrade -y
Installing Required Libraries and Dependencies
Step 3: Install Python Packages
Type the following command in the terminal to install the necessary Python libraries:
python3 -m pip install python-dotenv openai SpeechRecognition pyttsx3 gtts numpy
Step 4: Install System Dependencies
Install additional required system dependencies by typing:
sudo apt install python3-pyaudio flac espeak -y
Preparing the Environment
Step 5: Create a .env
File
A .env
file will securely store your OpenAI API key. Here’s how to create and edit it:
- Navigate to Your Home Directory:
cd ~
- Create a New Directory for the Project:
mkdir chatbot_project cd chatbot_project
- Create and Edit the
.env
File:nano .env
This opens a text editor. Type the following line into the file, replacing
your_openai_api_key_here
with your actual OpenAI API key:OPENAI_API_KEY=your_openai_api_key_here
Press
Ctrl+X
to exit,Y
to confirm changes, andEnter
to save.
Verifying OpenAI’s GPT-3.5 Model
Step 6: Create a Test Script
Create a new Python script to verify that GPT-3.5 is available and functioning with your API key:
- Create the Test Script File:
nano test_openai_model.py
- Add the Following Code to Test the Model: Copy and paste this code into
test_openai_model.py
:import openai from dotenv import load_dotenv import os # Load environment variables from .env file load_dotenv() openai.api_key = os.getenv('OPENAI_API_KEY') # Define a test function def test_gpt_model(): try: response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content": "Hello, how are you?"} ] ) print("Model response:", response.choices[0].message.content) except openai.error.OpenAIError as e: print("An error occurred:", e) if __name__ == "__main__": test_gpt_model()
- Run the Test Script Save and close the file by pressing
Ctrl+X
,Y
, andEnter
. Then run the script:python3 test_openai_model.py
If the model is accessible, you should see a response from GPT-3.5. If there’s an error, it will be printed in the terminal, which can help identify issues such as API key problems or model availability.
Creating the Python Script
Step 7: Create the Main Python Script
Create a new Python script for the chatbot:
- Create the Script File:
nano chatbot.py
- Copy and Paste the Following Code into
chatbot.py
:import openai from dotenv import load_dotenv import time import speech_recognition as sr import pyttsx3 import numpy as np import os # Load environment variables from .env file load_dotenv() openai.api_key = os.getenv('OPENAI_API_KEY') # Model name model = 'gpt-3.5-turbo' # Set up the speech recognition and text-to-speech engines r = sr.Recognizer() engine = pyttsx3.init() # Set the voice to the second available voice if possible voices = engine.getProperty('voices') if len(voices) > 1: engine.setProperty('voice', voices[1].id) else: engine.setProperty('voice', voices[0].id) # User's name name = "YOUR NAME HERE" greetings = [ f"What's up, master {name}?", "Yeah?", "Well, hello there, Master of Puns and Jokes - how's it going today?", f"Ahoy there, Captain {name}! How's the ship sailing?", f"Bonjour, Monsieur {name}! Comment ça va? Wait, why the hell am I speaking French?" ] # Function to listen for the wake word "hey" def listen_for_wake_word(source): print("Listening for 'Hey'...") while True: audio = r.listen(source) try: text = r.recognize_google(audio) if "hey" in text.lower(): print("Wake word detected.") engine.say(np.random.choice(greetings)) engine.runAndWait() listen_and_respond(source) break except sr.UnknownValueError: pass # Function to listen for input and respond using OpenAI API def listen_and_respond(source): print("Listening...") while True: audio = r.listen(source) try: text = r.recognize_google(audio) print(f"You said: {text}") if not text: continue # Send input to OpenAI API response = openai.ChatCompletion.create( model=model, messages=[{"role": "user", "content": text}] ) response_text = response.choices[0].message.content print(response_text) # Speak the response print("speaking") os.system(f"espeak '{response_text}'") engine.say(response_text) engine.runAndWait() if not audio: listen_for_wake_word(source) except sr.UnknownValueError: time.sleep(2) print("Silence found, shutting up, listening...") listen_for_wake_word(source) break except sr.RequestError as e: print(f"Could not request results; {e}") engine.say(f"Could not request results; {e}") engine.runAndWait() listen_for_wake_word(source) break # Use the default microphone as the audio source with sr.Microphone() as source: listen_for_wake_word(source)
Replace
"YOUR NAME HERE"
with your name or a preferred name. - Save and Exit Save any changes by pressing
Ctrl+X
,Y
, andEnter
.This code is a Python script for creating a voice-controlled chatbot using OpenAI’s GPT-3.5 model on a Raspberry Pi. Let’s break down the code step by step: - Imports: The script imports necessary libraries and modules:
openai
: OpenAI’s API for accessing GPT-3 models.dotenv
: A library for loading environment variables from a.env
file.time
: Provides time-related functions.speech_recognition as sr
: Library for speech recognition.pyttsx3
: Text-to-speech library.numpy as np
: Library for numerical computing.os
: Provides operating system interfaces.
- Environment Variables: It loads the OpenAI API key from a
.env
file usingload_dotenv()
and sets it asopenai.api_key
. - Model Selection: It specifies the GPT-3.5 model to be used by assigning
'gpt-3.5-turbo'
to the variablemodel
. - Speech Recognition and Text-to-Speech Setup:
- It initializes the speech recognition engine as
r
usingsr.Recognizer()
. - It initializes the text-to-speech engine as
engine
usingpyttsx3.init()
. - It sets the voice for the text-to-speech engine. If multiple voices are available, it selects the second one. Otherwise, it selects the first one.
- It initializes the speech recognition engine as
- User Configuration:
- It sets the variable
name
to represent the user’s name. - It defines a list of greetings to be randomly chosen when the wake word is detected.
- It sets the variable
- Function Definitions:
listen_for_wake_word(source)
: This function continuously listens for the wake word “hey” using the speech recognition engine. Once the wake word is detected, it responds with a random greeting and calls thelisten_and_respond(source)
function.listen_and_respond(source)
: This function listens for input commands after the wake word is detected. It sends the input to the OpenAI API for processing, retrieves the response, and speaks the response using text-to-speech.
- Main Execution:
- It uses the default microphone as the audio source and calls the
listen_for_wake_word(source)
function to start listening for commands.
- It uses the default microphone as the audio source and calls the
Running the Script
Step 8: Run the Chatbot Script
In the terminal, make sure you are in the directory where you created chatbot.py
. If not, navigate to it:
cd ~/chatbot_project
Run the script by typing:
python3 chatbot.py
Additional Considerations
Microphone and Speaker Configuration
- Check Connected Audio Devices:
- Microphone:
arecord --list-devices
- Speakers:
speaker-test -t wav -c 2
- Microphone:
- Test the Microphone: Record and play back a short audio clip:
arecord -D plughw:1,0 -d 5 test.wav aplay test.wav
Adjust
-D plughw:1,0
based onarecord --list-devices
output. - Adjust Audio Settings: Use
alsamixer
to adjust the volume and ensure the correct devices are selected and not muted:alsamixer
Handling API Key
- Avoid Hardcoding API Keys:
- Store your API key in the
.env
file. - Load the API key securely in your script using the
dotenv
library.
- Store your API key in the
- Environment Configuration:
- Ensure your deployment environment is configured to use these environment variables.
Error Handling and Debugging
- Handle Specific Exceptions:
- Handle exceptions like
sr.UnknownValueError
andsr.RequestError
to make your script more resilient.
- Handle exceptions like
- Logging:
- Use logging to keep track of events and errors:
import logging logging.basicConfig(level=logging.INFO) logging.info("Listening for 'Hey'...")
- Graceful Exit:
- Ensure your script can handle unexpected terminations gracefully:
import signal import sys def signal_handler(sig, frame): print('Exiting gracefully') sys.exit(0) signal.signal(signal.SIGINT, signal_handler)
Optimizing Performance
- Optimize Resource Usage:
- Ensure your
script is not consuming unnecessary resources. – Monitor CPU and memory usage using tools like htop
.
- Asynchronous Processing:
- For advanced optimization, consider using asynchronous processing for handling API requests and responses.
Setting Up as a Background Service
- Create a Systemd Service File:
sudo nano /etc/systemd/system/chatbot.service
Add the following content:
[Unit] Description=Chatbot Service After=network.target [Service] ExecStart=/usr/bin/python3 /home/pi/chatbot_project/chatbot.py WorkingDirectory=/home/pi/chatbot_project StandardOutput=inherit StandardError=inherit Restart=always User=pi [Install] WantedBy=multi-user.target
- Enable and Start the Service:
sudo systemctl daemon-reload sudo systemctl enable chatbot.service sudo systemctl start chatbot.service sudo systemctl status chatbot.service
Conclusion
Follow this guide to set up a voice-controlled chatbot on your Raspberry Pi 4. The chatbot listens for the wake word “hey,”(Which you can change) processes the commands using OpenAI’s GPT-3.5 model, and responds using text-to-speech.
If you encounter any issues, refer back to the respective sections for troubleshooting and optimization tips. Enjoy your new chatbot!
Feel free to reach out if you need further assistance.