Can MQTT be used to connect to large AI models?
mqtt_chat
Repository link: [https://github.com/Yaochenger/mqtt_chat](https://github.com/Yaochenger/mqtt_chat)
Introduction:
AI platforms have not provided a way to connect via MQTT, and for MCU platforms, developers need to configure a bunch of software packages to enable basic functionalities of large models. Additionally, many developers are not well-versed in networking, even though MQTT is widely used. So, is it possible to interact with AI using MQTT? Yes, it is! The basic implementation idea is to use a script to forward interaction information through an MQTT service, allowing any MQTT client to connect to the MQTT server for AI interaction. The response speed of the conversation is also within an acceptable range. If you are interested, you can give it a try! Just create an MQTT example with your RT-Spark and start experiencing it.
Steps to Use:
1. Obtain an API Key
Visit the API Key page of any AI large model to obtain your API Key. The link is as follows: https://console.volcengine.com/ark/region:ark+cn-beijing/apiKey
Go to the website mentioned above, find the API Key in the specified location, and remember where the Key is, as it will be needed later.
2. Install Dependencies
In a Windows environment, open the command line or PowerShell and execute the following command to install the required software for running the script.
pip install paho-mqtt openai python-dotenv
If you encounter permission issues,
Run as an administrator:
pip install paho-mqtt openai python-dotenv --user
Press Win, search for “PowerShell,” right-click, and select “Run as administrator.”
Verification:
python -c "import paho, openai, dotenv"
If there are no error messages, it means the dependencies have been installed successfully.
Finally, directly insert the API Key obtained in the first step into the `.env` file located in the tool’s directory.
3. Run the Script
Run the current script in PowerShell, as shown below:
The information output by the script includes the topics that the MQTT client needs to subscribe to and publish. At this point, you can use any MQTT client to implement a dialogue with the large model based on subscription and publication.
Testing
1. Test with MQTTX Software:
The configuration information for MQTTX is as follows:
Using MQTTX for AI interaction is as follows:
2. Use any MQTT client program on a development board to subscribe to the topics provided by the script in order to receive messages returned by the AI. Publish messages to the topics specified by the script to submit them to the AI for processing. Below, we will test this using the RT-Thread official development board, Xinghuo №1 (STM32F407), utilizing the kawaii-mqtt component. The test code is placed in the `example` folder at the root of the project.
RT-Spark purchase link:aliexpress.com/item/1005008704008478.html