With advancements in artificial intelligence and natural language processing, personalized chatbots have gained popularity for their ability to provide interactive and engaging conversations. These chatbots can understand natural language and respond intelligently, making human-computer interaction more intuitive and user-friendly. This project involves building a voice-activated chatbot using a Raspberry Pi, integrating OpenAI's GPT-3.5 model for natural language understanding and generation.
The chatbot is designed to listen for a wake word (“Hey”), after which it processes user input, generates a response using GPT-3.5, and communicates it back to the user through a text-to-speech engine. The main goal of this project is to create an interactive, hands-free voice assistant that can have personalized conversations.
Objectives of Raspberry Pi Chatbot Project
The objective of this project is to:
- Build a voice-activated chatbot using a Raspberry Pi.
- Use Google's Speech Recognition API to recognize speech and trigger the chatbot.
- Utilize OpenAI's GPT-3.5 model to generate natural language responses.
- Implement text-to-speech functionality to vocalize the responses.
- Provide personalized interactions by integrating custom greetings and wake word detection.
Materials Required for Raspberry Pi Chatbot
To build this project, the following components are necessary:
- Raspberry Pi – The central processing unit that will run the chatbot code and connect to the OpenAI API.
- Monitor – Used to operate the Raspberry Pi and visualize the output.
- Keyboard – For typing commands and programming on the Raspberry Pi.
- Microphone – To capture the user’s speech input.
- Speaker or Headphones – To output the chatbot's vocal responses.
- SD Card – Stores the Raspberry Pi OS and the project files.
- Power Supply for Raspberry Pi – Provides power to the Raspberry Pi.
- Internet Connection – Required for the chatbot to interact with the OpenAI API.
- Python Libraries – To enable speech recognition, text-to-speech, and API communication.
Steps to build OpenAi Chatbot using Raspberry Pi
Step 1: Writing and Testing the Code for Chatbot on Your Computer
-
Set Up Your Development Environment:
- Install Python on your computer if it is not already installed. You can download it from the official Python website.
- Install the required libraries using the following command in your terminal or command prompt:
pip install openai python-dotenv SpeechRecognition pyttsx3 numpy
-
Obtain Your OpenAI API Key:
- Visit the OpenAI website.
- Sign up for an account or log in if you already have one.
- Navigate to the API section and create a new API key. Make sure to save this key securely, as it will be required to authenticate requests to the OpenAI service.
-
Create a Python Script:
- Create a new file named
chatbot.py
on your computer using a text editor or an Integrated Development Environment (IDE) like Visual Studio Code or PyCharm. - Here is the explaination of the code:
- Create a new file named
import openai
from dotenv import load_dotenv
import time
import speech_recognition as sr
import pyttsx3
import numpy as np
- os: Used for interacting with the operating system, particularly to access environment variables.
- openai: This library provides access to the OpenAI API, allowing the program to generate responses using the GPT model.
-
load_dotenv: Loads environment variables from a
.env
file, where sensitive information (like the OpenAI API key) can be stored. - time: Provides time-related functions (though not used extensively in this code).
- speech_recognition (sr): Allows the program to convert speech into text using various speech recognition engines.
- pyttsx3: A text-to-speech conversion library in Python that enables the chatbot to speak responses.
- numpy (np): A library for numerical operations, here used to randomly select greetings.
openai.api_key = os.getenv('OPENAI_API_KEY') # Make sure to set this in your .env file
model = 'gpt-3.5-turbo'
- Loads the environment variables and retrieves the OpenAI API key. Make sure that the
.env
file contains your key. - Note : You need to make a separate .env file in your script folder and in that you have to wirte your api key and not in this script itself.
- Sets the model to be used for generating responses, in this case,
gpt-3.5-turbo
.
engine = pyttsx3.init("dummy")
voice = engine.getProperty('voices')[1]
engine.setProperty('voice', voice.id)
- Recognizer: Creates a recognizer object to recognize speech from audio input.
- pyttsx3 Initialization: Initializes the text-to-speech engine. The "dummy" argument is typically used to specify the driver (on some systems, you can also use "sapi5" for Windows).
- Retrieves and sets the voice for the text-to-speech engine to the second available voice.
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?"
]
- Sets the user’s name to personalize the interaction.
- Defines a list of greeting messages that the chatbot can randomly choose from when the wake word is detected.
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 Definition: Defines a function to continuously listen for a wake word (in this case, "hey").
- Listening Loop: The function continuously listens for audio input.
- Speech Recognition: It uses Google’s speech recognition to convert spoken words into text. If the text contains "hey," it acknowledges the wake word.
- Response: The chatbot randomly selects a greeting from the list and speaks it using the text-to-speech engine.
-
Invoke Response Function: Calls the
listen_and_respond
function to handle further interaction.
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
engine.say(response_text)
engine.runAndWait()
except sr.UnknownValueError:
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
- Function Definition: This function listens for user input and responds accordingly.
- Listening Loop: Similar to the wake word function, it listens continuously for user speech.
- Speech Recognition: Converts the recognized speech into text. If speech is recognized, it prints what the user said.
- OpenAI API Request: Sends the user input to the OpenAI API, creating a chat completion request, and retrieves the response from the model.
- Speak the Response: The chatbot speaks the response using the text-to-speech engine.
- Error Handling: If no audio is recognized, it returns to listening for the wake word. It also handles API request errors.
listen_for_wake_word(source)
- Uses the default microphone as the audio source. This sets up the microphone and starts listening for the wake word by calling
listen_for_wake_word
.
.
Step 2: Setting Up Raspberry Pi
A. Install Raspberry Pi OS
- Download the Raspberry Pi Imager from the official Raspberry Pi website.
- Install the Raspberry Pi OS (64-bit or 32-bit depending on your Raspberry Pi model) using the Raspberry Pi Imager onto an SD card (16GB or higher).
- Insert the SD card into your Raspberry Pi and power it on.
- Complete the initial setup by following the on-screen instructions (setting up language, time zone, Wi-Fi, etc.).
B. Install Python and Necessary Libraries
- Open a terminal on the Raspberry Pi.
- Update your system packages by running the following commands:
sudo apt update sudo apt upgrade
- Install Python if it’s not already pre-installed:
sudo apt install python3
- Install the required Python libraries by running:
pip3 install openai python-dotenv SpeechRecognition pyttsx3 numpy
C. Obtain and Set Up Your OpenAI API Key
- Follow the same steps as in Step 1 to obtain your OpenAI API key from the OpenAI website.
- Create a
.env
file in your Raspberry Pi home directory and add the API key in this format:OPENAI_API_KEY=your-api-key-here
chatbot.py
File from PC to Raspberry Pi Using a USB Drive
-
On Your PC:
- Plug in a USB drive.
- Copy the
chatbot.py
file to the USB drive. - Eject the USB drive safely.
-
On the Raspberry Pi:
- Plug the USB drive into the Raspberry Pi.
- Open a terminal on the Raspberry Pi.
- Navigate to the
/media/pi/
directory to locate the USB drive:cd /media/pi/
- List the contents to identify your USB drive (usually named after the USB label or model):
ls
- Enter the USB drive directory:
cd [USB_DRIVE_NAME]
- Copy the
chatbot.py
file to the Raspberry Pi home directory:cp chatbot.py /home/pi/
-
Verifying the File Transfer:
- Navigate to your home directory and check if the file has been copied successfully:
cd /home/pi/ ls
- You should see
chatbot.py
listed.
- Navigate to your home directory and check if the file has been copied successfully:
E. Connect Microphone and Speaker
- Plug in the USB microphone into the Raspberry Pi.
- Connect speakers or headphones to the Raspberry Pi’s 3.5mm audio jack or use Bluetooth-enabled speakers.
F. Testing the Setup
- Run the
chatbot.py
script to test the setup:python3 chatbot.py
Results
Upon successfully running the chatbot, you can interact with it by speaking. The chatbot will listen for the wake word and respond to your queries using voice output. The integration with the OpenAI API allows for dynamic and intelligent conversations.
Code
import os
import openai
from dotenv import load_dotenv
import time
import speech_recognition as sr
import pyttsx3
import numpy as np
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
model = 'gpt-3.5-turbo'
r = sr.Recognizer()
engine = pyttsx3.init("dummy")
voice = engine.getProperty('voices')[1]
engine.setProperty('voice', voice.id)
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?"
]
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
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
response = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": text}]
)
response_text = response.choices[0].message.content
print(response_text)
engine.say(response_text)
engine.runAndWait()
except sr.UnknownValueError:
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
with sr.Microphone() as source:
listen_for_wake_word(source)