ChatGPT on ESP32: Enabling Edge AI Conversations with Ease



In recent years, there has been a remarkable advancement in artificial intelligence (AI) and machine learning (ML) technologies. One particular breakthrough is the development of large language models like GPT-3, which can generate human-like text and engage in meaningful conversations. While these models have primarily been used in cloud-based environments, the emergence of edge computing has opened up new possibilities for running AI models on low-power devices. In this blog post, we will explore how to run ChatGPT, a variant of GPT-3, on the ESP32 microcontroller, enabling AI conversations at the edge.

What is the ESP32 microcontroller?
The ESP32 is a versatile and affordable microcontroller that has gained significant popularity in the embedded systems community. It features a dual-core processor, built-in Wi-Fi and Bluetooth connectivity, and provides a range of input/output options. Its low power consumption and rich set of features make it an ideal choice for edge computing applications.

Setting up the ESP32 for AI Conversations:
To run ChatGPT on the ESP32, we need to set up the development environment and configure the necessary software components. Here’s a step-by-step guide to get started:

  1. Install the Arduino IDE: The Arduino IDE is a user-friendly integrated development environment widely used for programming microcontrollers. Download and install the latest version of the Arduino IDE from the official website.
  2. Install ESP32 board support: Open the Arduino IDE and navigate to “File” -> “Preferences.” In the “Additional Boards Manager URLs” field, enter the following URL: Then, open the “Boards Manager” from “Tools” -> “Board” -> “Boards Manager,” search for “esp32,” and install the “esp32” board support package.
  3. Install required libraries: To enable AI capabilities on the ESP32, we need to install some libraries. Go to “Sketch” -> “Include Library” -> “Manage Libraries,” and search for and install the “ESPAsyncWebServer” and “ArduinoJson” libraries.
  4. Obtain the ChatGPT model: Due to the limited resources of the ESP32, we need to use a compact variant of the ChatGPT model. You can either train a smaller version yourself or find pre-trained models that are compatible with the ESP32’s resources.
  5. Connect ESP32 to the internet: Utilize the built-in Wi-Fi capabilities of the ESP32 to connect it to the internet. You can configure the Wi-Fi settings in your code or via the Arduino IDE’s serial monitor.

Developing the ChatGPT application:
Now that we have set up the ESP32 for AI conversations, let’s dive into the coding aspect. We’ll create a simple application that allows us to interact with ChatGPT. Here’s an outline of the code:

  1. Import the required libraries: Include the necessary libraries for the ESP32, Wi-Fi, and the AI model.
  2. Set up Wi-Fi connection: Configure the ESP32 to connect to your Wi-Fi network. Provide the SSID and password in the code.
  3. Initialize the AI model: Load the ChatGPT model and any associated data files into memory. Make sure to adapt the code according to the specific requirements of your model.
  4. Create a web server: Set up an asynchronous web server on the ESP32 to handle incoming requests.
  5. Handle client requests: Define routes for different endpoints, such as “/chat” for processing chat messages from the client.
  6. Process and generate responses: When the “/chat” endpoint is accessed, extract the user’s message from the request, pass it to the ChatGPT model, and generate a response. Send the response back to the client.

Testing the Chatgpt API on Postman

In the below image you can see, how do we test the chatgpt api on postman. So as to check if the chatgpt API is giving the proper response or not. Once this process is done, then we’ll shift to the coding part for running chatgpt on ESP32.

testing chatgpt api on postman

Uploading the Code

Now download the code from the link given below and check the two Sketches. Before you upload the program just make sure to change the said, password and chatgpt_token. If you don’t know how to get the API token, then watch the above video for clarification.

Download Full Code

Running ChatGPT on the ESP32 microcontroller enables AI conversations at the edge, empowering low-power devices with sophisticated language processing capabilities. By leveraging the ESP32’s resources and the simplicity of the Arduino IDE, developers can create innovative applications ranging from voice assistants to smart home automation. With further advancements in edge computing and AI technologies, the potential for bringing intelligent conversations to the edge will continue to expand, revolutionizing the way we interact with the digital world.

Note: Running AI models on resource-constrained devices like the ESP32 requires careful optimization and consideration of memory limitations. It’s important to select or design models that are suitable for such platforms and implement efficient coding practices to ensure optimal performance.

Also, check out our other playlist Rasa ChatbotInternet of thingsDockerPython ProgrammingMQTT, etc.
Become a member of our social family on youtube here.

Stay tuned and Happy Learning. ✌🏻😃

Leave a Reply