This tutorial shows you how to interact with the ChatGPT API on your Raspberry Pi.

ChatGPT is an impressive AI chatbot developed by OpenAI that can respond to prompts with detailed and readable responses.
This AI has been trained on a large amount of data, making it knowledgeable in a variety of fields and able to write code when prompted.
While the Raspberry Pi itself cannot run the ChatGPT model, it can interact with an API that allows us to query the AI running in the cloud.
In the following sections, we will show you how to write a Python script that interacts with ChatGPT from your Raspberry Pi.
Please note that the ChatGPT API is not free and has a cost, which you can see on OpenAI’s website by checking the model you choose to use in the script.
Depending on your account, you may need to add credit before OpenAI will respond to your requests. You can see your account’s current limits within the “Limits” section of your OpenAI dashboard settings.
Equipment
Below is the equipment we used to access ChatGPT from our Raspberry Pi.
Recommended
Optional
We last tested this tutorial on a Raspberry Pi 5 using the latest version of Raspberry Pi OS Trixie 64-bit.
Video Tutorial
The video below walks you through setting up ChatGPT AI on your Raspberry Pi.
We take you through installing the required packages, signing up for OpenAI, and writing a basic script. Alternatively, you can find the full written tutorial immediately below the video.
Getting an API Key for OpenAI
First, we need to generate an API key to use ChatGPT from our Raspberry Pi. This key allows us to interact with the API from our Pi.
1. Before being able to get an API key for OpenAI, you will need to sign up for an account.
You can create an OpenAI account by going to their platform’s sign-up page. If you already have an account, then simply log in to continue.
2. Once logged into OpenAI, you will want to find the “View API keys” option and click it.

3. Now that we are on the API keys page, we will need to generate a new secret key. We will use this secret key for your Raspberry Pi to interact with the ChatGPT API.
Click the “Create new secret key” button to generate the API key.

4. Before the key is generated, you will get a chance to give a name to your new key (1.). You can ignore this if you like.
You will also need to set an expiry (2.) for your key. After the specified time, the key will no longer work. Pick the expiry that fits your needs.
Once you are happy, click the “Create secret key” button (3.) to proceed.

5. You should now have access to the secret key (1.) you require to interact with OpenAI. Copy this key to a safe place, and do not share it with anyone. OpenAI will only show it to you once.
Once you have copied the key, click the Done (2.) button.

6. With the key generated, you may want to know where to track your usage.
On the left-hand side of this screen, you should see a sidebar where this is the option Usage. Click this option to change to the usage screen.

7. Below, you can see how much you have utilized OpenAI from your Raspberry Pi. This screen lets you quickly see how many credits you are consuming.
Depending on your account, you may need to set up billing before you will be able to use the OpenAI API.

Using Python on the Raspberry Pi to Interact with ChatGPT
To interact with ChatGPT from our Raspberry Pi, we will utilize the OpenAI Python module. This module makes interacting with OpenAI and its various models simple.
This script will take user input, send it to ChatGPT, and print its response. Hopefully, it will give you a good understanding of how you could implement it into your Pi projects.
Preparing your Raspberry Pi to use ChatGPT
1. Before we begin, we should ensure that our Raspberry Pi is up to date by grabbing the latest package list and upgrading any out-of-date packages.
You can do both with the following commands.
sudo apt update
sudo apt upgradeCopy
2. With our Raspberry Pi now updated, we need to install any packages that we require.
We are installing three packages: Python 3, its package manager (pip), and the venv module we will use to create a virtual environment. We must use pip to install the OpenAI package in the next step.
sudo apt install python3 python3-pip python3-venv -yCopy
Setting up the Python Virtual Environment for OpenAI on the Raspberry Pi
3. Due to changes to Raspberry Pi OS, we must set up a Python virtual environment to install the OpenAI Python library.
To do this, let us create a directory where we will write our ChatGPT script.
mkdir ~/openaiCopy
4. After creating the directory, we must now change into it by using the command below.
cd ~/openaiCopy
5. Once we are in the correct place, we must use Python to generate the Python virtual environment.
This environment lets us install additional Python packages without the operating system kicking up a fuss.
python -m venv envCopy
6. After generating the virtual environment, we must tell the terminal that we want to utilize it. You will need to run this command every time you want to use your ChatGPT script.
We can utilize the virtual environment by using the source command as shown below.
source env/bin/activateCopy
7. Our next step is to use pip to install the OpenAI Python package to our virtual environment.
This Python package will allow us to interact with the OpenAI API from our Raspberry Pi and in turn, interact with ChatGPT.
Installing this package is as straightforward as running the following command within the terminal.
python3 -m pip install openaiCopy
Creating the ChatGPT Python Script on the Raspberry Pi
8. To begin writing our Python script, we will run the following command within the terminal.
This command will launch the nano text editor and start writing to a file named chatgpt.py.
nano chatgpt.pyCopy
For our guide, we will use the nano text editor to write our code from the command line. However, if you use a desktop flavor of Raspberry Pi OS, you may want to use a GUI code editor like Thonny.
Writing the Simple ChatGPT Script
a. The first thing we need to do in our Raspberry Pi’s ChatGPT Python script is to import the openai Python library.
This library lets us send requests to the OpenAI API with a few short lines of code.
from openai import OpenAICopy
b. With the OpenAI library now imported, we need to instantiate the library. During the instantiation, we must pass in the API key that we will use to talk with the OpenAI API from our Raspberry Pi.
With the line below, we will create a copy of the OpenAI object and store it in a variable called client. We then pass the API key into this function.
Ensure you replace the word SECRETKEY with the key you got earlier in this guide, but keep it wrapped in the double quotes (" ") as we have done in the example.
Treat this key like a password. Anyone who has it can use your OpenAI credit, so remove it before sharing your script with anyone.
client = OpenAI(
api_key = "SECRETKEY"
)Copy
c. The ChatGPT documentation specifies that you must always start with a message that tells the AI how you want the assistant to behave.
The role for this message must be system, and we must specify the message within the content variable. In our example, we will tell the AI that it is an “helpful assistant”.
messages = [
{
"role": "system",
"content": "You are a helpful assistant"
}
]Copy
d. At this point, we will create an infinite loop so that after every message we send, we will get the chance to respond to ChatGPT’s response.
Creating an infinite loop is as simple as using a while loop with the value of True. Remember, since we are using Python, the following lines must be indented to remain within the loop.
while True:Copy
e. At the start of every loop, we need the script to prompt for a response from the user. We can do this in Python using its input() function.
The script will wait for you to press ENTER before continuing, allowing you to type in the message you want to ask ChatGPT.
message = input("You: ")Copy
f. Once we have received input from the user, we must append our new message onto the messages list we created earlier. We keep appending to this list in each loop so ChatGPT can analyze the whole conversation and provide context.
However, this time the role will be set to user, and the content variable will be set to the message we got from the user.
messages.append(
{
"role": "user",
"content": message
},
)Copy
g. Finally, we can now use OpenAI and send a message from our Raspberry Pi to the ChatGPT AI. We will need to use the client.chat.completions.create() function to achieve this.
For this function, we must pass in two variables that we will quickly explain now.
model– We set themodelvariable togpt-6-luna. At the time of writing, this is OpenAI’s most efficient model, making it quick and cheap for a simple chat script like this.messages– Here, we pass in the list that we populated in the previous step. ChatGPT will analyze the messages within this list to create a response that we can output to the user.
The response from ChatGPT will be stored in our chat variable.
chat = client.chat.completions.create(
messages=messages,
model="gpt-6-luna"
)Copy
h. We now need to grab the reply from the response data from ChatGPT and store it into a variable where we can easily reference it.
After setting the reply variable to the message object ChatGPT sent us, we then use the print() function to output the content of the reply.
reply = chat.choices[0].message
print("Assistant: ", reply.content)Copy
i. Finally, we append the reply to the messages list so that ChatGPT can reference it for your next response.
messages.append(reply)Copy
Final Code
9. The final version of your code should look like what we have shown below. Our script below shows that utilizing ChatGPT from your Raspberry Pi is effortless.
You could easily extend this code to fit into your own projects and take input from different sources.
from openai import OpenAI
client = OpenAI(
api_key = "SECRETKEY"
)
messages = [
{
"role": "system",
"content": "You are a helpful assistant"
}
]
while True:
message = input("You: ")
messages.append(
{
"role": "user",
"content": message
},
)
chat = client.chat.completions.create(
messages=messages,
model="gpt-5.4-mini"
)
reply = chat.choices[0].message
print("Assistant: ", reply.content)
messages.append(reply)Copy
10. When you have finished writing your script, you can save and quit by pressing CTRL + X, followed by Y, then ENTER.
Testing your ChatGPT Python Code on the Raspberry Pi
11. Now that we have written the script, we can use it to start interacting with the ChatGPT AI from our Raspberry Pi.
If you have opened a new terminal since setting up the virtual environment, you will first need to change into the “openai” directory and activate it again.
cd ~/openai
source env/bin/activateCopy
To run the script, you must run the following command in your terminal.
python3 chatgpt.pyCopy
12. Upon loading up the script, you will be prompted for your input. For our example, we will send a simple question with “What is PiMyLifeUp?”.
You: What is PiMyLifeUp?Copy
13. Below, you can see that ChatGPT read the message our Raspberry Pi sent and then sent us the response below based on our message.
Assistant: PiMyLifeUp is a technology tutorial website focused on Raspberry Pi projects and software. It also publishes guides on Linux, self-hosting, home automation, and other DIY tech topics. Its tutorials are generally aimed at helping readers set up and use things step by step.Copy
14. You can then ask another question, such as asking ChatGPT to rewrite the previous response.
You: Rewrite that response.Copy
15. Below, you can see how ChatGPT remembered its response and then rewrote it differently.
Assistant: PiMyLifeUp is a website with step-by-step tutorials for Raspberry Pi projects, Linux, self-hosting, home automation, and other DIY technology.Copy
If you’re finished interacting with the Assistant, simply use CTL + C to exit the script.
Conclusion
By this point, you should understand how to use ChatGPT on your Raspberry Pi. While we cannot run ChatGPT directly on our Pi, the OpenAI team provides an affordable API we can use instead.
If you no longer need the script, delete your key from the API keys page in the OpenAI dashboard so that it cannot be used again. To uninstall the script, simply delete the contents of ~/openai.
Please comment below if you have issues communicating with the ChatGPT AI. Check out our many other Raspberry Pi projects and Python tutorials to learn more.

2 typos in the code, somewhat frustrating.
There should be single quotes around the secret key, and the variable to assign the key to is api_key, not my_api_key.
Indentation is another problem, the ‘copy’ does not include that, but I will assume anyone writing python will know how to indent.
Hi Matthew,
Apologies for the typos and the frustrations that produced. While I had corrected them in the final code block I had forgotten to go back through the steps themselves that explain the code.
Also thank you for making it known about the problem with the copy functionality. I rewrote how it works last night so hopefully it will now copy the code exactly as it is written.
Cheers,
Emmet
Thanks for your response, I believe I made the corrections but now I get the following message:
File “/home/bills/chatgpt.py”, line 3, in
openai.api_key = sk-cuoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
NameError: name ‘sk’ is not defined
I x’d out the full key.
Thanks for your help.
Hi Bill,
Make sure that you keep your key wrapped in single (
' ') or double quotes (" ") as it needs to be defined as a string. For example, it should be the following:Cheers,
Emmet
Followed the instructions and when I run the script I get this:
python3 chatgpt.py
File “/home/bills/chatgpt.py”, line 5
message = input(“You: “)
^
IndentationError: expected an indented block
What do I need to do to correct this?
Thanks
Hi Bill,
Sorry to see that you have ran into an issue.
With Python indentation is incredibly important as that what helps the interpreter determine when code belongs to that particular block.
To correct this, you just need to make sure you indent by at least one space or tab. I personally recommend that for every block you indent by 4 spaces.
I wrote a guide on Python that explains what indentation means for Python a couple of years ago that is worth checking out if you need a better explanation.
Please let me know if you need any extra assistance.
Cheers,
Emmet