When MQTT Meets ChatGPT: Explore IoT Smart Applications that Can Interact Naturally

foreword

With the rapid development of the Internet of Things technology, the interaction between people and equipment, and between equipment and equipment has become no longer difficult, but how to realize the interaction more naturally, efficiently and intelligently has become a new challenge in the field of Internet of Things.

Recently, advanced large language models (LLM) such as ChatGPT, GPT-3.5, and GPT-4 released by OpenAI and their applications have been rapidly popularized around the world. brings more possibilities.

As an advanced natural language processing application, ChatGPT can easily realize natural conversations between humans and machines with its excellent natural language processing capabilities. MQTT (Message Queuing Telemetry Transport), the mainstream protocol in the IoT field, ensures real-time data transmission and efficient processing through a lightweight, low-bandwidth communication method and a publish/subscribe model.

From this, we can boldly imagine that the combination of the MQTT protocol and ChatGPT can more easily realize the human-computer intelligent interaction in the field of Internet of Things:

  • In the field of smart home, users can control smart devices at home through natural conversations with ChatGPT to improve the quality of life.
  • In the field of industrial automation, ChatGPT can help engineers analyze equipment data more quickly and improve production efficiency.

Based on this, this article will discuss how to combine the MQTT protocol with natural language processing applications such as ChatGPT. At the same time, it will show the combined application scenario through a simple construction example, and provide some ideas for readers to explore the intelligent application of the Internet of Things.

basic concept

Before we start, we need to briefly understand some basic concepts about MQTT and ChatGPT.

MQTT protocol

As mentioned above, the MQTT protocol is a lightweight message transmission protocol based on the publish/subscribe model. energy and other fields.

Using the MQTT protocol to connect massive IoT devices requires an MQTT server as a key component. In the following scheme design, we will use EMQX, a large-scale distributed IoT MQTT message server, to realize efficient and reliable connection of massive IoT devices and real-time processing and distribution of message and event stream data.

After that, we can use the MQTT client to connect to the MQTT server to communicate with IoT devices. This article uses the open source cross-platform MQTT client MQTTX, which includes desktop, command line and web applications, which can easily realize the connection test with the MQTT server and help developers quickly develop and debug MQTT services and applications.

MQTT Broker

ChatGPT

ChatGPT is a natural language processing application based on OpenAI's GPT-3.5 and GPT-4 and other advanced large language models. GPT (Generative Pre-trained Transformer) is a deep learning model known for its powerful text generation and understanding capabilities. ChatGPT can understand and generate natural language to have a smooth and natural conversation with users. To realize the natural language processing capability of ChatGPT, we need to use the API provided by OpenAI to interact with the GPT model.

ChatGPT interface

Scheme design and preparation

Based on the capabilities of the MQTT protocol and ChatGPT, we will design a solution to realize the combination and interoperability of the two.

In order to realize the natural language processing function similar to ChatGPT, we will write another client script, and use the API provided by OpenAI to interact with the GPT model in the script. When the MQTT client in this script receives the message and forwards it to the API, it will generate a corresponding natural language response, and then the response message will be published to a specific MQTT topic to realize the communication between ChatGPT and the MQTT client. Interactive loop.

Through this design scheme, we will demonstrate the interoperability process of message receiving, processing and forwarding between ChatGPT and MQTT protocol.

First, follow the steps below to prepare the required tools and resources.

  • Install EMQX:

    You can use Docker to quickly install and start EMQX 5.0:

    docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqx/emqx:latest
    

    In addition to Docker installation, EMQX also supports installation using RPM or DEB packages. For specific installation methods, please refer to EMQX 5.0 Installation Guide .

  • Install the MQTTX desktop application:

    Enter the MQTTX official website , select the corresponding operating system and CPU architecture version, click download and install.

  • Sign up for an OpenAI account and get an API key:

    Once in OpenAI , create or log in to your account. When done, click on the upper right corner, select View API Keys, and under API keysthe column , click Create new secret keyto generate a new API key. Please keep this key safe, because it will be used for API authentication in subsequent procedures.

    Sign up for an OpenAI account and get an API key

After completing the above steps, we already have the tools and resources needed to combine the MQTT protocol with the ChatGPT application. Regarding how to use OpenAI's API to interact with the GPT language model, you can refer to the OpenAI documentation for detailed guidance and learning materials.

Code

After the resources and environment are prepared, we will use the Node.js environment to build an MQTT client, which will receive messages through the MQTT topic, send data to the OpenAI API, and generate natural language through the GPT model. The generated natural language will then be published to the specified MQTT topic for integration interaction. Of course, you can also choose other programming languages ​​such as Python and Golang according to your needs and familiarity. For the sake of visualization, we will use the API directly, but you can also choose to use the official library, which provides a more concise way of using Node.js and Python.

For more information, please refer to: OpenAI Libraries .

  1. Prepare the Node.js environment: Make sure you have installed Node.js (v14.0 or higher is recommended). Create a new project folder and initialize the project with the npm init command. Then, install the necessary dependencies with the following command:

    npm init -y
    npm install axios mqtt dotenv
    

    Axios is used to send HTTP requests, mqtt is used to connect to MQTT server, and dotenv is used to load environment variables.

  2. Using environment variables: Create a file .envcalled and add your OpenAI API key to it:

    OPENAI_API_KEY=your_openai_api_key
    
  3. Write the code: create a new index.jsfile , and connect to the MQTT server in the file, subscribe to the specified MQTT topic, and listen to the message. After receiving the message, use axios to send an HTTP request to the OpenAI API, generate a natural language reply, and publish the reply to the specified MQTT topic. The key codes of each step are listed below for your reference :

    • Use the mqtt library to connect to the MQTT server. After the connection is successful, the default subscription chatgpt/request/+topic is used to receive the sent MQTT messages:

      const host = "127.0.0.1";
      const port = "1883";
      const clientId = `mqtt_${Math.random().toString(16).slice(3)}`;
      const OPTIONS = {
        clientId,
        clean: true,
        connectTimeout: 4000,
        username: "emqx",
        password: "public",
        reconnectPeriod: 1000,
      };
      const connectUrl = `mqtt://${host}:${port}`;
      const chatGPTReqTopic = "chatgpt/request/+";
      const client = mqtt.connect(connectUrl, OPTIONS);
      
    • Write genTextan asynchronous function, receive the userId parameter, and use axios to create an HTTP client instance, use the OpenAI API key in HTTP Headers for authentication, and then send a POST request to the OpenAI API to generate a natural language response. The generated reply content is published to the specific topic subscribed by the user through the MQTT client to receive the reply. The historical messages are stored in the Messages array:

      // Add your OpenAI API key to your environment variables in .env
      const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
      let messages = []; // Store conversation history
      const maxMessageCount = 10;
      const http = axios.create({
        baseURL: "https://api.openai.com/v1/chat",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${OPENAI_API_KEY}`,
        },
      });
      const genText = async (userId) => {
        try {
          const { data } = await http.post("/completions", {
            model: "gpt-3.5-turbo",
            messages: messages[userId],
            temperature: 0.7,
          });
          if (data.choices && data.choices.length > 0) {
            const { content } = data.choices[0].message;
            messages[userId].push({ role: "assistant", content: content });
            if (messages[userId].length > maxMessageCount) {
              messages[userId].shift(); // Remove the oldest message
            }
            const replyTopic = `chatgpt/response/${userId}`;
            client.publish(replyTopic, content, { qos: 0, retain: false }, (error) => {
              if (error) {
                console.error(error);
              }
            });
          }
        } catch (e) {
          console.log(e);
        }
      };
      
    • Finally, by listening to the message chatgpt/request/+of , the received message is stored in the Messages array, and genTextthe function is called to generate a natural language reply and directly sent to the specific topic subscribed by the user within the function. The maximum number of historical messages is 10:

      client.on("message", (topic, payload) => {
        // Check if the topic is not the one you're publishing to
        if (topic.startsWith(chatGPTReqTopicPrefix)) {
          const userId = topic.replace(chatGPTReqTopicPrefix, "");
          messages[userId] = messages[userId] || [];
          messages[userId].push({ role: "user", content: payload.toString() });
          if (messages[userId].length > maxMessageCount) {
            messages[userId].shift(); // Remove the oldest message
          }
          genText(userId);
        }
      });
      
  4. Run the script to serve:

    node index.js
    

So far, we have completed the basic function part of the demo project. In addition to the basic functions, the code also realizes the access isolation between users, and only needs to add different suffixes to specific topics. By storing previous message history, GPT models can also understand the context within the context of a conversation and generate more coherent and contextual responses based on previous conversations.

The complete code can be viewed in openai-mqtt-nodejs on GitHub.

another option

In addition to the above examples, we can also directly use the rule engine and Webhook in the data bridging function provided by EMQX to achieve rapid development.

EMQX supports setting rules to trigger Webhook callbacks when a message is published to a specific topic. We only need to write a simple web service, use the OpenAI API to interact with the GPT model and send the generated reply through the HTTP response, which can be published to the specified topic by creating a new MQTT client, or directly use EMQX's Publish API to complete the operation , and finally achieve the purpose of integrated interaction.

For users who already have Web services, this method can save development costs to the greatest extent and realize PoC or Demo quickly. The advantage is that there is no need to write an independent MQTT client, and the EMQX rule engine can be used to simplify the integration process and flexibly process data. However, web services still need to be written and maintained, and webhooks may not be convenient enough for complex application scenarios.

Therefore, the solutions mentioned above have their own advantages, and we can choose a more suitable solution according to the actual business needs and the technical level of developers. But either way, EMQX, as an MQTT infrastructure, provides important support for system integration, enabling developers to quickly build project prototypes and promote digital transformation.

Demo display

After completing the example development of the interaction between the MQTT client and the GPT model, we can use the MQTTX desktop client to test this demo project. The user interface of MQTTX is similar to chat software, which simplifies the page operation, so it is more suitable for demonstrating the interaction with the dialogue robot.

First, we need to create a new connection in MQTTX, connect to the same MQTT server in the above code, for example: , 127.0.0.1and then subscribe to chatgpt/response/demothe topic to receive replies and chatgpt/request/demosend messages to the topic. The demo suffix here can be replaced with other strings to achieve access isolation between users. We can test it by sending a Hello message:

MQTTX send message to ChatGPT

Next, we simulate some more complex demo environments. If the temperature of a certain sensor exceeds a preset threshold, the ChatGPT bot sends an alert message to another MQTT topic, which is connected to a monitoring device, such as a smart watch. Or a smart speaker. After the monitoring device receives the alarm message, it can use natural language technology to convert the alarm information into voice, so that users can receive and understand it more conveniently.

MQTTX send message to ChatGPT

For example, we can also create a smart home environment, which includes multiple MQTT topics, which correspond to different types of equipment (such as lighting, air conditioning, audio, etc.). We will use ChatGPT to generate natural language commands for real-time interaction with these devices via MQTT client etc.

Generate natural language commands using ChatGPT

future outlook

Combining the ChatGPT and MQTT protocols can realize an intelligent Internet of Things system, which has wide application potential in the fields of smart home and industrial automation. Through natural language interaction, users can control the switch, brightness, color and other parameters of household equipment to achieve a more intelligent and comfortable living environment; in industrial automation, using ChatGPT and MQTT to realize intelligent equipment maintenance and control can bring more Efficient and intelligent manufacturing process.

In the future, we can envision ChatGPT or smarter AGI tools playing more roles in improving efficiency and productivity in the IoT field, such as:

  • Message parsing: Parse the messages transmitted through MQTT, extract the required data, and prepare for subsequent processing and analysis.
  • Semantic understanding: understand and process the semantics of the messages received from MQTT, so as to extract more accurate information.
  • Intelligent processing: AI technology is used to intelligently process received MQTT messages to help users obtain appropriate solutions faster.
  • User feedback: As a representative of intelligent interaction, it receives user feedback information through MQTT and provides corresponding responses.
  • Virtual Assistant: As a virtual assistant, it controls smart home devices through language recognition technology, provides users with smarter and more efficient services, and improves the convenience and comfort of life.

epilogue

In this blog, we briefly explored the combination of MQTT and ChatGPT and their potential applications. Through EMQX and MQTTX, combined with the API provided by Open AI, an AI application similar to ChatGPT is realized, and the processed data is received and forwarded by using MQTT connection, showing the integration of MQTT and ChatGPT.

Although the combination of these technologies has not yet been put into the production environment, as more products integrating AI technology are launched (such as New Bing integrates the GPT model into the search engine, and GitHub's Copilot, etc.), we believe that artificial intelligence (AI) and The future development direction of Internet of Things (IoT) technology will also include the optimization of natural language interaction, the improvement of intelligent device control, and more innovative application scenarios.

In conclusion, the combination of MQTT and ChatGPT reveals an area worthy of attention and further exploration. We look forward to these continuously evolving innovative technologies to bring us a better world.

Copyright statement: This article is original by EMQ, please indicate the source for reprinting.

Original link: https://www.emqx.com/zh/blog/when-mqtt-meets-chatgpt

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4174826/blog/10084229