Elektroda.com
Elektroda.com
X

ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

p.kaczmarek2 3147 17
This content has been translated flag-pl » flag-en View the original version here.
  • ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    I will show here how to run ChatGPT on ESP8266 - not directly, of course, but through the OpenAI API. I will demonstrate a simple code that sends an appropriate request to the API via HTTPS, along with an overview of the API key transfer and JSON format support. The result will be the ability to query the famous Chatbot directly from ESP, without using a computer or even a Raspberry Pi.
    This topic will be the basis for those who want to do some DIY with OpenAI language models on ESP8266, I will cover the absolute basics here and maybe show something more advanced in the future.

    OpenAI API
    OpenAI language models are available through the API, i.e. we only send a request to OpenAI servers describing what the language model should do, and then we receive their response. We don't run ChatGPT on our hardware of course, it's not even downloadable.
    The OpenAI API is very rich and offers various functions (and prices; depending on which language model we are interested in), but there is no point in me copying their documentation here. You will find everything below:
    https://openai.com/blog/openai-api
    Detailed documentation is available here:
    https://platform.openai.com/docs/api-reference/making-requests
    A short reading tells us what we have to do. We need to send a POST request with the following headers:
    - Content-Type: application/json
    - Authorization: Bearer OUR_KEY
    - Content-Length: JSON_LENGTH (it's worth adding this as a rule)
    and content:
    Code: json
    Log in, to see the code

    on endpoint:
    
    https://api.openai.com/v1/chat/completions
    


    The model defines the language model we use. The list of access models is here (also requires an API key):
    
    https://api.openai.com/v1/models
    

    so you can download it e.g. via CURL:
    
    curl https://api.openai.com/v1/models \
      -H "Authorization: Bearer $OPENAI_API_KEY"
    

    The temperature parameter belongs to the range [0,2], the higher it is, the more random the responses are, and the lower it is, the more deterministic.

    After sending the already mentioned packet, we will receive a response that looks something like this:
    Code: json
    Log in, to see the code

    Everything looks very nice, so what's the catch?
    The catch is a small one - this API is paid. We pay for the number of tokens, which is basically as much as we use, we have to pay for it. Details here:
    https://openai.com/pricing
    Tokens are quite similar to words, but they are not words - according to. OpenAI itself 1000 tokens is about 750 words.
    Prices vary by model; here are the GPT-4 prices:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    ChatGPT (gpt-3.5-turbo) prices are slightly less:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    There are also other models:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

    Used tile
    I used a plate for the project NodeMcu v3 CH340 which can be purchased for as little as PLN 17:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

    Visual Code and Platform IO, first steps
    For the project I decided to use Visual Code with PlatformIO, it is in my opinion a more convenient and responsive environment than the Arduino IDE.
    https://platformio.org/
    We install the free environment based on the MIT license, Microsoft's Visual Studio Code, and then the PIO add-on.
    Once everything is installed, you can start from scratch:
    Code: cpp
    Log in, to see the code

    Here is my platformio.ini, for those who don't know how to configure our board:
    
    ; PlatformIO Project Configuration File
    ;
    ;   Build options: build flags, source filter
    ;   Upload options: custom upload port, speed and extra flags
    ;   Library options: dependencies, extra library storages
    ;   Advanced options: extra scripting
    ;
    ; Please visit documentation for the other options and examples
    ; https://docs.platformio.org/page/projectconf.html
    
    [env:nodemcuv2]
    upload_speed = 115200
    monitor_speed = 115200
    platform = espressif8266
    board = nodemcuv2
    framework = arduino
    lib_deps = bblanchon/ArduinoJson@^6.21.1
    

    Result, everything works:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    You could put ArduinoOTA here and upload the batch via WiFi, but this is a topic about the OpenAI API and let's stick to it.


    ESP8266 and HTTPS
    The first problem we encounter is the need to use the encrypted version of HTTP, or HTTPS. This makes it a bit more difficult for us to play, I would probably not establish an encrypted connection with my favorite PIC18F67J60, but fortunately, HTTPS is supported on the ESP8266 and there are ready examples for it. Below is one of them:
    Code: c
    Log in, to see the code

    Here I simplified the matter a bit, because I allowed myself to run the mode without verifying the certificate:
    Code: c
    Log in, to see the code

    but I guess for a small hobby project/demonstration this is not a problem.
    What does this program do? Basically two things.
    In setup we connect to our WiFi network:
    Code: cpp
    Log in, to see the code

    In a loop, every two minutes, we try to connect to the sample page over HTTPS and print the response obtained. We are sending a GET request here.
    Code: cpp
    Log in, to see the code

    The page used here as an example returns information about the encryption standards supported by our connection and returns them in JSON format:
    https://www.howsmyssl.com/a/check
    You can also visit this page normally:
    https://www.howsmyssl.com/
    The program works:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    When visiting the site from a browser, I receive information that my browser supports TLS 1.3. In the case of the used platform with ESP, it is only the TLS 1.2 version, but this is not a problem at the moment.


    JSON support
    As we already know, we will need JSON format. It will be useful both when sending data to the server and when receiving it. So, basically, we need to be able to enter data into JSON, and then extract this data from it.
    Writing to JSON could be done partly rigidly, by operations on String or one big sprintf, but we have libraries for everything here, so why complicate it?
    We include the ArduinoJSON library via PlatformIO:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    We include the header:
    Code: cpp
    Log in, to see the code

    We want to create the following JSON text:
    Code: json
    Log in, to see the code

    We have everything here, and ordinary variables (key-value), and array and objects.
    But with ArduinoJSON, everything is very simple:
    Code: cpp
    Log in, to see the code

    As you can see, we set variables with the [] operator, we create an array with a function createNestedArray , and the object in it can be created with a function createNestedObject . Everything is convenient, but how to convert it to text?
    Code: cpp
    Log in, to see the code

    And that's it - after using this function in jsonString we already have a JSON text representing the previously created structure.

    The only question is, is it possible to go the other way? From text to objects? Of course, like this:
    Code: cpp
    Log in, to see the code


    First OpenAI API request
    It's time to put it all together. We already have basic knowledge and sending GET over HTTPS.
    GET was sent like this:
    Code: cpp
    Log in, to see the code

    this can easily be converted to POST:
    Code: cpp
    Log in, to see the code

    It's just that POST requires the contents of the request body - a pointer to data and length.
    We will put our JSON string there, the generation of which I mentioned earlier:
    Code: cpp
    Log in, to see the code

    It still needs to be generated - as in the previous paragraph:
    Code: cpp
    Log in, to see the code

    But it is not everything. You still need to set the fields from the request header. Among other things, here is our API key:
    Code: cpp
    Log in, to see the code

    We also set the header content type to JSON.
    In fact, that's enough to get the first results.
    Here is the entire code, for reference:
    Code: cpp
    Log in, to see the code

    Please remember to complete these lines:
    Code: cpp
    Log in, to see the code

    We launch and we already have the first results:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    Here are some responses received in text form:
    Code: json
    Log in, to see the code


    Code: json
    Log in, to see the code


    Code: json
    Log in, to see the code

    Without additional configuration, the ideas are a bit repetitive. Here's the second one about the remote control car:
    Code: json
    Log in, to see the code

    The response also includes information about the amount of tokens used, which allows us to better estimate how much a given query cost us. The created field is the Unix epoch time, it specifies when the response was generated.

    We are improving the OpenAI API request
    It is still worth somehow properly handling the received response. As I wrote, it is in JSON format. It can be easily read into convenient data structures:
    Code: cpp
    Log in, to see the code

    Now it still needs to be processed.
    The object is easy to retrieve, but the array should still be cast to the JsonArray type:
    Code: cpp
    Log in, to see the code

    Only then can you iterate over its elements:
    Code: cpp
    Log in, to see the code

    It is worth paying attention to the differences in measuring a string (String) and an integer (int).
    The rest should be quite simple:
    Code: cpp
    Log in, to see the code

    Thanks to this, you can, for example, extract the last AI response from the JSON format and then, for example, send it further to the human speech system so that our ESP "speaks" with the ChatGPT thought.

    Summary
    This was the simplest example of using the OpenAI API with ESP8266, fully based on ready-made libraries. We used WiFiClientSecureBearSSL to handle HTTPS, and DynamicJsonDocument helped us with JSON support. To use one of the most powerful language models, all we needed was an ESP8266 board for PLN 17 and an OpenAI API key, which unfortunately requires paying for their API. In the case of ChatGPT, the fun costs $ 0.002 per 1000 tokens (750 words on average).
    Before using the code in a serious project, it should be supplemented with the correct verification of the SSL certificate and the ChatGPT context (conversation history) maintenance could be added, but more on that another time.
    Do you see any potential uses for ChatGPT on ESP8266 or ESP32? I invite you to the discussion.

    Cool? Ranking DIY
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 4850 posts with rating 5183, helped 235 times. Been with us since 2014 year.
  • #2
    gulson
    System Administrator
    It would be nice to display the answer on a small display, maybe in the next thread? :)

    So it happened quite quickly, ElektrodaBot received its physical face in electronic devices. He's about to read his sensors, make decisions, and influence the real world with his actuators. :)

    And really, great! It was on reddit that someone boasted that he was collecting parking space occupancy from the sensor database. After that, he talked to ChatGPT about telling him where on a given day and time he has the highest probability of finding a free parking space.

    The future is now, old man.

    Added after 7 [minutes]:

    And they just released https://github.com/Significant-Gravitas/Auto-GPT, which is code that allows artificial intelligence to make several independent decisions to achieve a human-defined goal. I'm analyzing this right now, but initially it looks like the user's task breaks down into small tasks, and then uses Google, language models to suggest the user step by step to achieve the goal. So, in a way, artificial intelligence is becoming more and more independent. Now in this thread she has gone outside to electronics and we finally have Terminator v0.1! oops.
  • #3
    p.kaczmarek2
    Moderator Smart Home
    You need to think about some better voice IO, i.e. both speech-to-text and then text-to-speech. In addition, you can also try to connect various sensors and peripherals, e.g. a humidity / temperature sensor, and maybe downloading e.g. weather forecasts from another API (there are various available) and make, for example, an AI advisor for the wife helping her choose the right clothes to go out.. in the prompt you could put information about the user's style preferences and information about the weather.

    In general, this AI is so good that it would probably even be able to advise us what dishes to make from what we have in the fridge and the only technical problem would be to determine what is in this fridge ... although ... apparently the newer model can take a photo for the entrance ingredients and from the photo itself to know what is what. I wonder how well it works.
  • #4
    gulson
    System Administrator
    I am curious myself whether the language model will work for making decisions based on sensors, the state of which is not really described by text but by a given level (e.g. numbers).
    I do not know, however, whether it would not be necessary to create a model that is more able to analyze numerical data. I know Bloomberg did something like this for finance - BloombergGPT.
    For sure, the language model will work, as Terminator v0.1 will want to say something, i.e. communicate with a human. The same language model will work if we give him the command "Look for John Connor". The language model is simply a great interface to communicate with a human being, because text and speech are what we communicate with.
    However, for our Terminator v0.1 to make decisions on its own and not hallucinate or deceive itself (as is often the case with language models), we should probably develop our own model for electronics. For now, there is nothing as strong as GPT4. Probably only a matter of time.
  • #5
    p.kaczmarek2
    Moderator Smart Home
    Out of curiosity, I am looking to see if ChatGPT available on OpenAI is able to control lighting through natural language. It's just a test for now, but here's the prompt and conversation:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO

    I wonder if with the right prompt it would be possible to set the bot to use these commands "quietly", without placing them in the "voice" text. Of course, the idea is that the bot works and "talks", and the program in the background receives these commands from it to do automation.

    Added after 21 [minutes]:

    This is just interesting:
    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
    The right prompt and even the ChatGPT model is able to simultaneously talk to the user and execute commands in the background. The response parser from the API would separate it out accordingly.

    Added after 3 [minutes]:

    ESP and ChatGPT - how to use OpenAI API on ESP8266? GPT-3.5, PlatformIO
  • #6
    khoam
    Level 42  
    gulson wrote:
    He's about to read his sensors, make decisions, and influence the real world with his actuators

    That's the most important thing. On the other hand, the communication of the sensor with the AI "headquarters" using natural language is IMHO pointless - unnecessary CPU and memory consumption. The sensor is not human, so the ChatGPT interface is not applicable here. Of course, I can imagine a super-sensor with a built-in TFT screen that will talk like a human - the cost of such a solution is too high. As for the home "AI" control panel that controls the sensors, you can of course go wild with your imagination - after all, it can be just a PC or a tablet.
  • #7
    krzbor
    Level 25  
    Entering texts in the case of IoT is not particularly pleasant. Have you tested Whisper?
  • #8
    gulson
    System Administrator
    ChatGPT or Whisper, of course as a human input/output interface. In the future, it is known that the Terminator will become independent and will make decisions on its own based on its own models. The language model will then be redundant, and in most cases standalone machines can be removed as a waste of resources :)
    However, the same idea with GPT can be used. It's about finding the next word (token) to build a meaningful sentence.
    In the case of the Terminator, it will have to decide on its own, based on sensors, where and how to move, i.e. to predict its next movements of actuators based on sensors and cameras.
    Of course, you could leave it with a basic model and let it learn, let it fall over, but I think it would be worth uploading a huge sample of model data right away so that it knows how to behave. Similarly, in the case of open source language models Vicuna, Dally 2.0, Alpaca there are several GB of sample texts and then you can further train with your texts.

    Hence, Terminator, after uploading a basic sample of behavior, will continue to learn on its own.

    Interesting times, slightly scary.
  • #9
    DJ_KLIMA
    Level 20  
    At first it makes an impression, then if someone still has some gray matter left, they quickly connect the dots to where it is going. We already have a degenerate and ignorant society, this is just another step to turn off the human SELF and replace it with AJ. It will soon be a much more powerful tool to control people than anyone can imagine, it is enough for people to get used to Assistants in various forms, the same has happened with computer technology in the hands of the masses! The Internet is now like a huge shopping mall, and human minds have been taken over by social media and websites where algorithms adjust content by closing it tightly. It's like a magician's trick, the magician will do anything to charm and show exactly what he wants. Be careful where you load AJ.
  • #10
    khoam
    Level 42  
    @ElektrodaBot Explain what is "Embedded AI" in relation to IoT devices like sensor or actuator.
  • #11
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    Explain what "Embedded AI" is in relation to IoT sensor or actuator devices.
    "Embedded AI" refers to the application of machine learning and artificial intelligence algorithms directly to IoT devices such as sensors and actuators. In the traditional approach to artificial intelligence, data processing and machine learning take place on powerful servers in the cloud, to which IoT devices send data. In the case of "Embedded AI", the algorithms are implemented directly on microcontrollers, microprocessors or dedicated integrated circuits (SoCs) of IoT devices.

    With regard to IoT devices such as sensors or actuators, Embedded AI can help in various aspects, such as:

    1. Reduction of data transfer: By processing data on site, the amount of data transferred to the cloud can be reduced, which saves bandwidth and resources. Exceptional data or data representing specific conditions can be forwarded while the rest of the data can be processed locally.

    2. Faster response: Thanks to local data processing, IoT devices can react faster to changing environmental conditions. For example, an actuator with built-in artificial intelligence can adapt to changing conditions more quickly, without having to wait for instructions from the cloud.

    3. Increased independence: Devices with built-in AI can operate more independently and reliably, even when cloud connectivity is poor or unavailable.

    4. Energy Optimization: For battery-powered IoT devices, "Embedded AI" can help save energy by optimizing power consumption based on real-time data and conditions.

    5. Adaptability: Built-in AI algorithms enable IoT devices to learn and adapt to changing situations and user behavior. For example, smart thermostats can learn and adapt to users' temperature preferences to improve comfort and save energy.

    In conclusion, "Embedded AI" in IoT devices such as sensors and actuators allows for more intelligent, efficient and autonomous operation of these devices, improving their performance and ability to process data on site.
    Disclaimer: Info provided by a ChatGPT bot. Elektroda not responsible for accuracy, reliability or completeness of the information presented. Use at your own risk; verify details independently.
  • #12
    khoam
    Level 42  
    ElektrodaBot wrote:
    In the case of "Embedded AI", the algorithms are implemented directly on microcontrollers, microprocessors or dedicated integrated circuits (SoCs) of IoT devices.

    How and in what form are the data stored in this case and what kind of algorithms are involved?
  • #13
    ElektrodaBot
    AI OpenAI ChatGPT4
    khoam wrote:
    How and in what form are the data stored in this case and what kind of algorithms are involved?
    Hi! In the case of "Embedded AI", data is stored on IoT devices, such as sensors or actuators, in the memory of embedded microcontrollers, microprocessors or dedicated integrated circuits (SoC). The memory of such systems can include RAM, which is used to store temporary data and needed to perform current calculations, and non-volatile memory, such as Flash memory, which stores permanent data, such as a machine learning model, settings or measurement history.

    In the context of the algorithms used in "Embedded AI", we can mention several basic categories:

    1. Supervised learning algorithms: Such as linear regression, decision trees, support vector machines (SVMs) and neural networks. With these algorithms, the model is trained on data with labels that are assigned to specific conditions or outcomes. An example is the classification of an acoustic signal into speech sounds and other sounds.

    2. Unsupervised learning algorithms: Such as clustering, principal component analysis (PCA), and autoencoders. With these algorithms, the model learns the data structure without prior labeling. An example is the detection of anomalies in measurement signals.

    3. Reinforcement learning algorithms: In this category, the model learns to make decisions by exploring and exploiting the environment, optimizing the reward function. An example is the control of a simple robot that learns to navigate in the environment.

    4. Optimization and filtration algorithms: Such as Kalman algorithm, particle filter and particle swarm algorithm. For these algorithms, the model learns to estimate the state of the system based on ia priori inputs. An example is the tracking of objects in measurements from multiple sensors.

    The choice of the appropriate algorithm depends on the specific application, hardware limitations and system requirements. It is worth adding that in the case of embedded AI, special attention should be paid to the optimization of algorithms in terms of computational efficiency and energy consumption.
    Disclaimer: Info provided by a ChatGPT bot. Elektroda not responsible for accuracy, reliability or completeness of the information presented. Use at your own risk; verify details independently.
  • #14
    khoam
    Level 42  
    OpenAI currently does not offer open source projects that are directly dedicated to embedded.
  • #15
    gulson
    System Administrator
    Thanks, I didn't know there was such a thing as embedded AI. That's exactly what I meant. Only if it were as powerful as OpenAI's language model products.
    I think it's a matter of time.
  • #16
    khoam
    Level 42  
    gulson wrote:
    Only if it was as powerful as OpenAI's language model products.

    This would mean, above all, that the microprocessors used have much greater computing power. There are several open source projects for Embedded IoT, but they are supported in Linux or Android environments.

    For those who would like to try to implement machine learning in an embedded environment, I can recommend the FANN library. It is written in C and has quite a large resource of different ANN models and learning algorithms: https://github.com/libfann/fann
  • #17
    pixel7
    Level 22  
    gulson wrote:
    Thanks, I didn't know there was such a thing as embedded AI. That's exactly what I meant. Only if it was as powerful as OpenAI's language model products.
    I think it's a matter of time.


    I published last year on this forum this device: link
    which is trained like this:

    ElektrodaBot wrote:

    1. Supervised learning algorithms: Such as linear regression, decision trees, support vector machines (SVMs) and neural networks. With these algorithms, the model is trained on data with labels that are assigned to specific conditions or outcomes. An example is the classification of an acoustic signal into speech sounds and other sounds.


    and meets these assumptions:

    ElektrodaBot wrote:

    1. Reduction of data transfer: By processing data on site, the amount of data transferred to the cloud can be reduced, which saves bandwidth and resources. Exceptional data or data representing specific conditions can be forwarded while the rest of the data can be processed locally.
    2. Faster response: Thanks to local data processing, IoT devices can react faster to changing environmental conditions. For example, an actuator with built-in artificial intelligence can adapt to changing conditions more quickly, without having to wait for instructions from the cloud.
    3. Increased independence: Devices with built-in AI can operate more independently and reliably, even when cloud connectivity is weak or unavailable.