Quick Start: Create a Flow Using OpenAI Node
This section demonstrates how to quickly create and test an LLM-based Flow in the Flow Designer through a practical use case.
This demonstration shows you how to build a workflow that receives sensor data from MQTT topics and uses an LLM (e.g., OpenAI GPT) to interpret the data and summarize its meaning in natural language. The resulting summary is republished to a new topic, ai/summary
, for downstream consumption.
Scenario Description
Assume a device reports temperature and humidity readings to the MQTT topic sensors/temp_humid
. Each message includes raw sensor data in JSON format. The EMQX Flow will perform the following steps:
- Data Processing: Extract the device ID and sensor values.
- LLM-Based Processing: Use an OpenAI model to summarize the sensor reading.
- Message Republish: Publish the AI-generated summary to a new topic,
ai/summary
.
Sample message:
{
"device_id": "device123",
"temperature": 38.2,
"humidity": 75,
"timestamp": 1717568000000
}
Expected output (AI-generated):
Device device123 reported a temperature of 38.2°C and 75% humidity.
Create the Flow
Prerequisite
Make sure you have a valid OpenAI API Key.
Click the Create Flow button on the Flows page.
Add a Messages node.
- Drag a Messages node from the Source panel.
- Set the topic to
sensors/temp_humid
. - Click Save.
Add a Data Processing node.
- Drag a Data Processing node from the Processing section.
- Add the following mappings:
payload.device_id
to aliasdevice_id
payload.temperature
to aliastemperature
payload.humidity
to aliashumidity
- Click Save.
Add an OpenAI node.
- Drag an OpenAI node from the Processing section and connect it to the Data Processing node.
- Configure the node:
- Input: Enter
payload
. - System Message: Enter
Generate a short summary of the device’s sensor readings in human-readable format
. - Model: Select
gpt-4o
. - API Key: Enter your OpenAI API key.
- Base URL: Leave empty.
- Output Result Alias: Enter
summary
.
- Input: Enter
- Click Save.
Add a Republish node.
- Drag a Republish node from the Sink section and connect it to the OpenAI node.
- Set the topic to
ai/summary
. - Set the payload to
${summary}
. - Click Save.
Connect all the nodes and click Save in the upper-right corner to save the Flow.
Flows and form rules are interoperable. You can also view the SQL and related rule configurations on the Rule page.
Test the Flow
Connect an MQTT client to EMQX.
To quickly test the flow, you can use the Diagnostic Tools -> WebSocket Client on the Dashboard to simulate an MQTT client. Alternatively, you can also use the MQTTX tool or a real MQTT client:
- Connect to your EMQX server.
- Subscribe to the topic
ai/summary
.
Start Testing.
In the Flow Designer, click any node to open the Edit panel.
Click Edit, then click Start Test to open the test panel at the bottom.
Click Input Simulated Data and publish this message to topic
sensors/temp_humid
by clicking Submit Test:json{ "device_id": "device123", "temperature": 38.2, "humidity": 75 }
Review results.
You can see the successful execution result of the flow.
Return to the WebSocket Client page and you should receive an AI-generated summary like:
“The sensor readings from device "device123" indicate that the current temperature is 38.2°C and the humidity level is 75%.”
If the test results are unsuccessful, error messages will be displayed accordingly.
To view the running statistics and metrics of the OpenAI node, click the node to open the Edit panel and click the Overview tab.