Connect to OpenAI and build a voice chatbot
Time: 10:35 AM to 11:25 AM
The architecture
Here is what you are building: Every component runs on your Raspberry Pi except the OpenAI API, which runs in the cloud. Vosk (speech-to-text) and espeak (text-to-speech) both run locally — no internet needed for those.Set up the API key
The facilitator will provide an OpenAI API key. Store it on your robot:Program 1: Test the API connection
Before adding voice, confirm that your robot can talk to GPT over the internet.Step 1 — Import and connect
Load the API key from your secret file and create an OpenAI client:sys.path.insert tells Python where to find secret.py. The OpenAI client handles all communication with the API.
Step 2 — Send a single prompt
The API uses a messages list. Each message has arole and content:
- system — hidden instructions that shape the model’s behavior
- user — what you (the human) say
- assistant — what the model says back
Step 3 — Interactive chat loop
To have a back-and-forth conversation, keep a growingmessages list. Append each user message and assistant response so the model remembers the conversation:
Run it
Click to see the complete test_openai.py program
Click to see the complete test_openai.py program
Program 2: Test speech-to-text
Now test the microphone and Vosk speech recognition. This program listens through your USB mic and prints what it hears in real time.Step 1 — Suppress ALSA noise and import libraries
The Pi’s audio system prints dozens of harmless warnings. This block silences them before importing the audio libraries:ctypes code — it just tells ALSA to be quiet. The important imports are vosk (speech recognition) and pyaudio (microphone access).
Step 2 — Find the USB microphone
The Pi may have multiple audio devices. This function scans for one with “usb” in its name:Step 3 — Load Vosk and open the mic stream
Create the speech recognition model and open a live audio stream from the mic:chunk is how many audio samples to read at a time — one quarter of a second’s worth. The recognizer processes these chunks and detects when you finish a sentence.
Step 4 — The transcription loop
Read audio chunks in a loop. When Vosk detects a complete sentence, print it:AcceptWaveform returns True when the recognizer detects a pause — meaning you finished a word or sentence. The result is a JSON string containing the transcribed text.
Run it
Click to see the complete test_stt_vosk.py program
Click to see the complete test_stt_vosk.py program
Program 3: The voice chatbot
Now combine everything into a full conversation loop.Step 1 — The speak function
Use espeak to convert text to speech:Step 2 — The listen function
Wrap the Vosk transcription loop in a function with a timeout. This listens for a single sentence:timeout seconds, it returns whatever partial text it has (or an empty string).
Step 3 — The system prompt
Define the robot’s personality. This hidden instruction shapes every response:Step 4 — The main conversation loop
Tie everything together. Listen, send to GPT, speak the response, repeat:messages list grows with every exchange. The model sees the full history, so it can reference things you said earlier in the conversation.
Run it
Click to see the complete voice_chatbot.py program
Click to see the complete voice_chatbot.py program
Customize the personality
The system prompt defines who the robot thinks it is. Try changing theSYSTEM_PROMPT variable in voice_chatbot.py to give the robot a different personality:
Edit the file:
SYSTEM_PROMPT variable near the top and replace the text between the triple quotes.

