From ‘Super Brain’ to Microcomputer: Can ChatGPT Power a Raspberry Pi Zero W Chatbot?

César P. Soares, PhD
6 min readMay 2, 2023

--

Building a chatbot can be an exciting and rewarding experience, especially when you can see it come to life on a Raspberry Pi Zero W! In this post, I’ll explore how to create a chatbot using the GPT-3.5-based language model, ChatGPT, and run it on a low-cost, credit card-sized computer.

The Raspberry Pi Zero W is an ideal platform for building chatbots, providing a low-power, affordable, and versatile computing solution. Furthermore, with the ability to connect to Wi-Fi networks, the Raspberry Pi Zero W can also act as an intelligent assistant that can answer questions, provide helpful advice, and even control your home automation devices.

ChatGPT is a state-of-the-art natural language processing model developed by OpenAI, which can generate human-like responses to a wide range of prompts. Using ChatGPT, we can train a chatbot to understand and respond to user queries conversationally, making it feel like you’re talking to a natural person. You can see the chatbot that I built in the video below:

In general, there are four steps to build the featured chatbot:

1- Audio to text conversion
2- Send the converted text to the ChatGPT API
3- Take the text from the ChatGPT API response
4- Conversion text to audio

In this post, I’ll walk you through the steps of building a chatbot using ChatGPT on a Raspberry Pi Zero W, from setting up the hardware to training and deploying the model. I’ll also cover how to improve your chatbot’s performance and make it more responsive to user inputs.

It is important to say in advance that the idea of the post is not to cover in detail the code built but give an overall presentation of how to set up your chatbot on a Raspberry Pi Zero W. Any questions about the code, please, ask in the comments. You can also access the complete code on my GitHub.

By the end of this post, you’ll have a fully functional chatbot that you can use to impress your friends and family and maybe even solve some real-world problems, like the ones that we urgently need to tackle: named climate change! So, let’s get started!

What do you need:

  • One USB/Bluetooth mouse/keyboard
  • One HDMI cable
  • A 5v adapter
  • A screen
  • One Bluetooth/USB speaker/microphone
  • A Raspberry Pi Zero W with Raspberry Pi OS installed

When the above equipment is connected and turned on, open the Thonny editor and import the necessary libraries, including openai, speech_recognition, os, gtts, playsound, and sys.

import openai
import speech_recognition as sr
import os
from gtts import gTTS
from playsound import playsound
import sys

The OpenAI API key is then set up by assigning it to the openai.api_key variable. The speech recognition object is created using sr.Recognizer(), the user is prompted to provide the profile or identity that he/she wants the chat to have, which is transcribed from speech to text using Google’s speech recognition API.

# Set up the OpenAI API
openai.api_key = "Your API Key"

# Set up the speech recognition object
r = sr.Recognizer()

# Record audio from microphone
while True:

try:

with sr.Microphone() as source:
tts("Who do you want me to be?")
audio1 = r.listen(source)
print("Who do you want me to be?")
break

except sr.UnknownValueError:
# If speech recognition fails, prompt the user to repeat their question
tts("Sorry, I didn't understand. I will ask you again:")
print("Sorry, I didn't understand. I will ask you again:")

except:
sys.exit()

# Use Google's speech recognition API to transcribe audio to text
profile = r.recognize_google(audio1)
tts(f'I am a: {profile}')
print('I am a:', profile)

Next, the program enters a loop that allows the user to converse with the chatbot. The loop runs until the conversation ends or until the maximum number of messages is reached by the OpenAI Server (2048 Tokens).

#List of the questions/messages (chat)
messages = []
chat = 0

while len(messages) < 1000:

try:

#First talk
if chat == 0:

while True:

try:
# Record audio from microphone
with sr.Microphone() as source:
tts("What is your question?")
audio2 = r.listen(source)
print("What is your question?")

# Use Google's speech recognition API to transcribe audio to text
text = r.recognize_google(audio2)
print('Your question:', text)

#Chatbot settings
messages = [{"role": "system", "content": profile}, {"role": "user", "content": text}]

#Stop the conversation if asked
if text.lower() == 'stop':
tts('Thank you for talking')
print('Thank you for talking')
sys.exit()

# Call the OpenAI API to generate a response
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
max_tokens=50
)

# Print the response text
output = response.choices[0].message.content
tts(output)
print('Here is my answer:', output)

# Append to the list of messages to continue the chat
messages.append({"role": "assistant", "content": output})

#Continue the chat
chat += 1
break

except sr.UnknownValueError:
# If speech recognition fails, prompt the user to repeat their question
tts("Sorry, I didn't understand. I will ask you again:")
print("Sorry, I didn't understand. I will ask you again:")

except:
sys.exit()

# Continue the chat
else:

while True:

try:
# Record audio from microphone to continue the chat
with sr.Microphone() as source:
tts("Any other question?")
audio3 = r.listen(source)
print("Any other question?")

# Use Google's speech recognition API to transcribe audio to text
text = r.recognize_google(audio3)
print('Your question:', text)

#Append to the list to continue the chat
messages.append({"role": "user", "content": text})

#Stop the conversation if asked
if text.lower() == 'stop':
tts('Thank you for talking')
print('Thank you for talking')
sys.exit()

# Call the OpenAI API to generate a response
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
max_tokens=50
)

# Print the response text
output = response.choices[0].message.content
tts(output)
print('Here is my answer:', output)

# Append to the list of messages to continue the chat
messages.append({"role": "assistant", "content": output})
break

except sr.UnknownValueError:
# If speech recognition fails, prompt the user to repeat their question
tts("Sorry, I didn't understand. I will ask you again:")
print("Sorry, I didn't understand. I will ask you again:")

except:
sys.exit()

#Maximum tokens achieved in OpenAI
except openai.error.InvalidRequestError:
print("Error: Message length exceeds maximum context length")
sys.exit()

except:
sys.exit()

Inside the loop, the program first prompts the user for their question, which is transcribed from speech to text using the Google speech recognition API. The program then constructs a list of messages, including the user’s question and the chatbot’s response. The OpenAI API is then called to answer the user’s query based on the context of the previous messages. The response is returned as text, converted to speech using the gtts library, and played using the playsound library. The chatbot’s response is then appended to the list of messages, and the loop continues with the user’s next question or message.

If the user says “stop,” the conversation is ended, and the program exits. If an error occurs, the program exits with an error message. Additionally, an error message is printed if the maximum number of tokens is exceeded in the OpenAI API and the program exits.

Finally, it is worth noting that the tts() function used in the code above is based on the gTTS (Google Text-to-Speech) library, aiming to convert the given text into speech. It has a single parameter text, which is the text that needs to be converted to speech. Therefore, it is important to add this function right after you import the libraries at the beginning. Otherwise, the code will not work.

#Function text-to-speech
def tts(text):
# Create a gTTS object with the given text and language
tts = gTTS(text=text, lang='en')

# Save the audio file
tts.save('output.mp3')

# Play the audio file using the playsound library
playsound('output.mp3')

# Remove the audio file
os.remove('output.mp3')

After you do it, save the archive as <name of the archive.py>. Then open the command line of your Raspberry Pi Zero W. Navigate to the folder where the afore mention archive is located. Run the script:

python <name of the archive.py>

Now, enjoy your conversation! The code provided can be changed as you desire. Not just what and how the bot asks you but also the language (I will soon post a Portuguese version of it).

At last, it is important to remember that as we continue to integrate AI into our daily lives, it’s essential that we consider the ethical implications of these technologies. As developers, we are responsible for prioritizing transparency, accountability, and fairness in our designs, especially when creating AI-powered systems that may impact individuals and communities. By prioritizing these values, we can help ensure that AI technologies serve society positively and equitably. So let’s work together to build a future where AI is used ethically and responsibly.

Any questions about the code, please, ask in the comments. All codes are available on my GitHub.

--

--

César P. Soares, PhD
César P. Soares, PhD

Written by César P. Soares, PhD

Constantly working with theoretical/practical topics that can contradict each other in the face of the contemporary global socioenvironmental crisis.

No responses yet