How to set up ChatGPT on a Raspberry Pi Pico W

ChatGPT's ability to understand natural language inputs and its versatility make it a useful tool for developing various applications with the Raspberry Pi Pico W. An example can be voice recognition, with the help of external microphones and speakers, ChatGPT can be used to develop voice-controlled systems that can perform various tasks, such as controlling home appliances or responding to user queries.

This tutorial demonstrates how to set up ChatGPT for your Raspberry Pi Pico W using their API in this tutorial. By the end of it, you will be sending ChatGPT prompts and getting responses in return.

Step 1-) Generate an API Key

API keys can be generated here: https://platform.openai.com/account/api-keys

The Chat GPT API key is a unique code that allows you to access the Chat GPT service through its API (Application Programming Interface).

When you make a request to the Chat GPT API, the API key is used to authenticate your request and identify your account. This ensures that only authorized users can access the Chat GPT service and that your usage is tracked for billing and security purposes. Eventually, it will ask for billing information, as the API key is only free for a certain amount of usage, so keep that in mind when testing. You will need the Chat GPT API key if you want to use the Chat GPT service in your application or website. Without the API key, you won't be able to make requests to the Chat GPT API and receive responses from the language model.

Step 2-) Connect to the Internet and use API Call

import json
import network
import time
import urequests

# Internal libs
import constants

def chat_gpt(ssid, password, endpoint, api_key, model, prompt, max_tokens):
    """
        Description: This is a function to hit chat gpt api and get
            a response.
        
        Parameters:
        
        ssid[str]: The name of your internet connection
        password[str]: Password for your internet connection
        endpoint[str]: API enpoint
        api_key[str]: API key for access
        model[str]: AI model (see openAI documentation)
        prompt[str]: Input to the model
        max_tokens[int]: The maximum number of tokens to
            generate in the completion.
        
        Returns: Simply prints the response
    """
    # Just making our internet connection
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    
    # Wait for connect or fail
    max_wait = 10
    while max_wait > 0:
      if wlan.status() < 0 or wlan.status() >= 3:
        break
      max_wait -= 1
      print('waiting for connection...')
      time.sleep(1)
    # Handle connection error
    if wlan.status() != 3:
       print(wlan.status())
       raise RuntimeError('network connection failed')
    else:
      print('connected')
      print(wlan.status())
      status = wlan.ifconfig()
    
    ## Begin formatting request
    headers = {'Content-Type': 'application/json',
               "Authorization": "Bearer " + api_key}
    data = {"model": model,
            "prompt": prompt,
            "max_tokens": max_tokens}
    
    print("Attempting to send Prompt")
    r = urequests.post("https://api.openai.com/v1/{}".format(endpoint),
                       json=data,
                       headers=headers)
    
    if r.status_code >= 300 or r.status_code < 200:
        print("There was an error with your request \n" +
              "Response Status: " + str(r.text))
    else:
        print("Success")
        response_data = json.loads(r.text)
        completion = response_data["choices"][0]["text"]
        print(completion)
    r.close()
      
chat_gpt(constants.INTERNET_NAME,
         constants.INTERNET_PASSWORD,
         "completions",
         constants.CHAT_GPT_API_KEY,
         "text-davinci-003",
         "Write a tagline for an ice cream shop. ",
         100)

In this code we do a series of actions:

  • We start by connecting to the internet, as you need an active internet connection to access their API. Please pass in an internet name and password.
  • We pass in the remaining information needed for the ChatGPT model, most importantly the API key but also the model and the max_tokens variables. You can find all information here in their documentation.
  • We are using the “completions” API but there are also several other API paths that have different purposes that you can look into, this includes things such as edits or embeddings which can also have powerful use cases.
  • It is important to note that there are many other configurable variables that can be passed in, and you would need to slightly modify the function to incorporate other parameters.
  • A very interesting parameter you can add in as an experiment is the temperature parameter. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.It is generally recommended to alter this or top_p but not both.

    Conclusion

    Do not forget to like, comment and subscribe to the channel. If you have any questions please let us know in the comment section and thanks for reading. Stay tuned!

    Create a free account to access full content.

    All access to code and resources on ShillehTek.

    Signup Now

    Already a member? Sign In

    Explore More on Our Blog

    How to Use ADS1115 with the Raspberry Pi (Part 1)

    How to Use ADS1115 with the Raspberry Pi (Part 1)

    Discover how to expand your Raspberry Pi projects by integrating the ADS1115 ADC for precise analog signal reading....

    How to Install Pip Packages in AWS Lambda Using Docker and ECR

    How to Install Pip Packages in AWS Lambda Using Docker and ECR

    Learn how to streamline AWS Lambda deployments by using Docker and Amazon Elastic Container Registry (ECR) to package...

    Create Tabular Product Descriptions on Your Shopify Store

    Create Tabular Product Descriptions on Your Shopify Store

    Enhance your Shopify store's product pages with our comprehensive guide on implementing tabular descriptions. Learn how to add a...

    SSH Into Raspberry Pi with Tailscale VPN

    SSH Into Raspberry Pi with Tailscale VPN

    Effortlessly access and manage your Raspberry Pi from anywhere using Tailscale's secure mesh VPN.

    Send Email with Lua and the ESP32

    Send Email with Lua and the ESP32

    In this tutorial, we delve into sending emails with the ESP32-S3 using Lua, focusing on the Xedge IDE's built-in SMTP...

    How to Code with Lua on ESP32 with XEdge32

    How to Code with Lua on ESP32 with XEdge32

    Learn how to set up Xedge32 and start coding on the ESP32-S3 with Lua programming!

    Stream Audio From Raspberry Pi to Local Computer

    Stream Audio From Raspberry Pi to Local Computer

    Discover the simplicity of streaming live audio directly from a USB microphone connected to your Raspberry Pi to...

    SSH Raspberry Pi via Cell Phone

    SSH Raspberry Pi via Cell Phone

    This beginner-friendly guide will walk you through remotely controlling your Raspberry Pi using SSH through your cell phone.

    Remotely Control Raspberry Pi via SSH from External Network

    Remotely Control Raspberry Pi via SSH from External Network

    Learn how to SSH into your Raspberry Pi from any network. This is critical in IoT since you can control...

    Stream Video from Raspberry Pi Camera to YouTube Live

    Stream Video from Raspberry Pi Camera to YouTube Live

    Learn how to stream to YouTube from a Raspberry Pi Camera.

    Back to blog

    Leave a comment

    Please note, comments need to be approved before they are published.