Using the ChatGPT API on the Raspberry Pi

In this tutorial, we will show you how to interact with the ChatGPT API on your Raspberry Pi.

Raspberry Pi ChatGPT

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 considerable amount of data that allows it to be knowledgeable in a variety of fields and can even write code for you 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.

Over 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 a free service and does have a cost that you can see by visiting OpenAI’s website and looking at “gpt-3.5-turbo“.

When you first create an account, OpenAI will often give you some trial credits to use with the service. However, some restrictions are still applied to your account until you set up billing. For example, with ChatGPT, you will be limited to only 3 messages per minute till you set up billing.


In the list below you can find a list of the equipment that we used when accessing ChatGPT from our Raspberry Pi.



We last tested this tutorial on a Raspberry Pi 400 using the latest version of Raspberry Pi OS Bullseye 64-bit.

Getting an API Key for OpenAI

We must first generate an API key to use the ChatGPT AI 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 signup page. If you already have an account, then simply log in to continue.

2. Once logged into OpenAI, click your profile in the top-right corner to open up a pop-up menu.

Open Profile Menu

3. Within the pop-up menu, you will want to find the “View API keys” option and click it.

Change to View API Key page

4. 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.

Generate new key for ChatGPT on the Raspberry Pi

5. Before the key is generated, you will get a chance to give a name for your new key (1.). You can ignore this if you like.

Once you are happy, click the “Create secret key” button (2.) to proceed.

Optionally give key a name

6. You should now have access to the secret key (1.) you require to interact with OpenAI. Copy this key down, as it will only be shown once.

Once you have copied the key, click the “Done” (2.) button.

Copy generated API key for ChatGPT

7. With the key generated, you may want to know where to track your usage.

On the left hand of this screen, you should see a sidebar where this is the option “Usage“. Click this option to change to the usage screen.

Swap to Usage Screen

8. Below, you can find out how much you have utilized OpenAI from your Raspberry Pi. This screen allows you to see how many credits you are consuming quickly.

OpenAPI Usage screen

Preparing your Raspberry Pi to use ChatGPT

Now that we have the OpenAI secret key we next need to set up our Raspberry Pi with everything it needs to interact with ChatGPT.

Luckily for us, interacting with ChatGPT is relatively simple, thanks to the Python package that they provide.

1. Before we begin, we should ensure that our Raspberry Pi is current by grabbing the latest package list and upgrading any out-of-date packages.

You can achieve both of these tasks by using the following command.

sudo apt update
sudo apt upgrade

2. With our Raspberry Pi now updated we need to install any packages that we require.

The two packages we are installing at this point are Python 3 and its package manager called pip. We must use pip to install the OpenAI package in the next step.

sudo apt install python3 python3-pip

3. Our next step is to use pip3 to install the OpenAI Python package.

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.

sudo pip3 install openai

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 we installed earlier.

This script will be straightforward as the OpenAI library we installed earlier handles most of the work for us.

This script we are writing will take user input, send it to ChatGPT then print its response. Hopefully, it will give you a good understanding of how you could implement it into your Pi projects.

Beginning Writing our ChatGPT Python Script on the Raspberry Pi

For our guide, we will use the nano text editor to write our code from the command line. However, if you use a desktop flavour of Raspberry Pi OS, you may want to use a GUI code editor like Thorny.

1. 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 ““.


Writing our Simple ChatGPT Script

2. The first thing we need to do in our Raspberry Pi’s ChatGPT Python script is import the “openai” Python library.

As mentioned earlier, this library will allow us to easily send requests to ChatGPT with a few simple lines of code.

import openai

3. With the OpenAI library now imported, we next need to set the API key for the library to utilize.

To set the OpenAI API Key, you must type in the following line of code.

Ensure you replace the word “SECRETKEY” with the key you got earlier in this guide but keep it wrapped in the single quotes (' ') as we have done in the example.

openai.api_key = 'SECRETKEY'

4. 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 “intelligent assistant”.

messages = [ {"role": "system", "content": "You are a intelligent assistant." } ]

5. 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 that as we are using Python the following lines will all need to be indented to remain within the loop.

while True:

6. 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: ")

7. Once we have received input from the user, we must append our new message onto the “messages” list we created earlier. We continue to append to this list on every loop so that ChatGPT can analyze the whole conversation and give it 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.

        {"role": "user", "content": message},

8. Finally, we can now put OpenAI to use and send a message from our Raspberry Pi to the ChatGPT AI. We will need to use the “openai.ChatCompletion.create()” function to achieve this.

For this function, we must pass in two variables that we will quickly explain now.

  • model – We set the “model” variable to “gpt-3.5-turbo“. This model is the latest version of ChatGPT available to us and is the same model that the OpenAI web interface utilizes.
  • 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 = openai.ChatCompletion.create(
        model="gpt-3.5-turbo", messages=messages

9. 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)

10. Finally, we append the reply to the “messages” list so that ChatGPT can reference it for your next response.


Final Code

11. The final version of your code should look like what we have shown below. Our script below shows that utilising 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.

import openai

openai.api_key = 'SECRETKEY'

messages = [ {"role": "system", "content": "You are an intelligent assistant." } ]

while True:
    message = input("You: ")

        {"role": "user", "content": message},

    chat = openai.ChatCompletion.create(
        model="gpt-3.5-turbo", messages=messages

    reply = chat.choices[0].message

    print("Assistant: ", reply.content)

12. 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

13. Now that we have written the script, we can use it to start interacting with the ChatGPT AI from our Raspberry Pi.

To run the script, you must run the following command in your terminal.



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?”.

User: What is PiMyLifeUp?

15. 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 website that provides tutorials and resources for Raspberry Pi projects and programming. It aims to help beginners and enthusiasts to learn how to use Raspberry Pi to its full potential in various applications, such as home automation, gaming, and media centers. It also covers topics like Python programming, Linux, and electronics.

16. You can then ask another question such as asking ChatGPT to rewrite the previous response.

User: Rewrite that response.

17. Below, you can see how ChatGPT remembered its response and then rewrote it differently.

Assistant:  PiMyLifeUp is a comprehensive online platform that specializes in offering guidance and materials about Raspberry Pi projects and programming. The website targets both amateur and experienced enthusiasts to help them leverage the full capabilities of Raspberry Pi in several applications ranging from home automation, gaming, to media centers. PiMyLifeUp provides extensive coverage on various subjects including Python programming, Linux, and electronics as they relate to Raspberry Pi.


Hopefully, by this point, you will now understand how to use the ChatGPT AI on your Raspberry Pi.

While we cannot directly run ChatGPT on our Pi, the OpenAI team provides an affordable API that we can utilize instead.

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.


  1. Avatar for Matthew
    Matthew on

    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.

    1. Avatar for Emmet
      Emmet on

      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.


  2. Avatar for Bill Steiner
    Bill Steiner on

    Thanks for your response, I believe I made the corrections but now I get the following message:
    File “/home/bills/”, 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.

    1. Avatar for Emmet
      Emmet on

      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:

      openai.api_key = 'sk-cuoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'


  3. Avatar for Bill Steiner
    Bill Steiner on

    Followed the instructions and when I run the script I get this:
    File “/home/bills/”, line 5
    message = input(“You: “)
    IndentationError: expected an indented block

    What do I need to do to correct this?

    1. Avatar for Emmet
      Emmet on

      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.


Leave a Reply

Your email address will not be published. Required fields are marked *