Co-authored-by: thomasnordquist <7721625+thomasnordquist@users.noreply.github.com>
6.0 KiB
LLM System Improvements Summary
Overview
Comprehensive improvements to LLM integration for MQTT Explorer, focusing on pattern-based inference, quality proposals, and extensive testing.
Changes Implemented
1. Remove System-Specific Guidelines ✅
Before:
SYSTEM DETECTION GUIDELINES:
- zigbee2mqtt: Uses JSON payloads like {"state":"ON"}, topics end with /set
- Home Assistant: Uses /set topics, simple or JSON payloads
- Tasmota: Uses cmnd/ prefix, simple string payloads (ON/OFF/TOGGLE)
After:
Common MQTT Systems You May Encounter:
You may see topics from popular systems like zigbee2mqtt, Home Assistant,
Tasmota, ESPHome, Homie, Shelly, Tuya, and others.
PATTERN ANALYSIS APPROACH:
Infer the MQTT system and appropriate message format by analyzing:
- Topic naming patterns
- Related topics
- Payload formats
- Value patterns
Result: LLM successfully infers formats from patterns without explicit rules
2. System Name Priming ✅
Added mentions of common MQTT systems to prime the LLM:
- zigbee2mqtt
- Home Assistant
- Tasmota
- ESPHome
- Homie
- Shelly
- Tuya
Purpose: Help LLM recognize systems while still learning patterns from context
3. Comprehensive Home Automation Tests ✅
Added 6 new test cases covering popular systems:
Test Suite: Popular Home Automation Systems - Pattern Inference
-
zigbee2mqtt lamp control ✅
- Verifies JSON format inference
- Confirms /set suffix pattern recognition
-
Home Assistant light control ✅
- Verifies simple string format inference
- Tests /set topic pattern
-
Tasmota device control ✅
- Verifies cmnd/ prefix recognition
- Tests simple string commands
-
Garage door opener ✅
- Tests custom command patterns
- Verifies "open" command generation
-
Smart switch control ✅
- Tests /cmd suffix pattern
- Verifies ON/OFF format matching
-
Thermostat control ✅
- Tests numeric value inference
- Verifies temperature setting patterns
4. Test Results
Total Tests: 17/17 passing
Home Automation System Detection
✔ should detect zigbee2mqtt topics and propose valid actions
✔ should detect Home Assistant topics and propose valid actions
✔ should detect Tasmota topics and propose valid actions
Proposal Quality Validation
✔ should propose multiple relevant actions for controllable devices
✔ should provide clear, actionable descriptions
✔ should match payload format to detected system
Edge Cases
✔ should handle read-only sensors appropriately
✔ should handle complex nested topic structures
✔ should handle topics with special characters
Question Generation Quality
✔ should generate relevant follow-up questions
✔ should provide informative responses about sensor data
Popular Home Automation Systems - Pattern Inference
✔ should infer zigbee2mqtt and turn on a lamp correctly
✔ should infer Home Assistant and turn on a light correctly
✔ should infer Tasmota and control a device correctly
✔ should correctly handle garage door opener pattern
✔ should infer smart switch control pattern
✔ should infer thermostat control from temperature pattern
17 passing (2m)
Key Achievements
Pattern-Based Learning Works
The LLM successfully infers system-specific formats without explicit rules:
Example 1: zigbee2mqtt
// Current value shows JSON structure
{"state": "OFF", "brightness": 128}
// LLM infers control message should also use JSON
{"state": "ON"} ✓ Correct
Example 2: Tasmota
// Topic pattern: cmnd/device/POWER
// Current value: OFF (simple string)
// LLM infers simple string command
"ON" ✓ Correct
Example 3: Thermostat
// Related topics show numeric values
target_temp: 22
// LLM infers numeric format
"23" ✓ Correct
Quality Over Quantity
System prompt emphasizes:
- Only propose for controllable devices
- Avoid false positives
- Match observed patterns
- Quality over quantity
Result: No false positives in tests, all proposals are accurate
Comprehensive Coverage
Tests cover:
- Major MQTT systems (zigbee2mqtt, Home Assistant, Tasmota)
- Various payload formats (JSON, simple strings, numeric)
- Different topic patterns (/set, /cmd, cmnd/)
- Real-world scenarios (lamps, switches, garage doors, thermostats)
Files Modified
-
backend/test/llmIntegration.spec.ts- Added 6 new home automation tests
- Updated system prompt with pattern-based approach
- Added system name priming
-
app/src/services/llmService.ts- Updated system prompt with pattern-based approach
- Added system name priming
- Maintained all existing functionality
Documentation Cleanup
Removed unnecessary documentation files:
DESIGN_REVIEW.mdOPTION_B_IMPLEMENTATION.mdGPT5_MINI_VERIFICATION.mdGPT4O_INVESTIGATION.md
Total: ~600 lines of outdated docs removed
Future Enhancements
Planned: Tool Calling (MCP-Style)
Goal: Allow LLM to query topic history
Architecture:
Frontend (has topic history)
↓ LLM request
Backend (LLM)
↓ tool_calls
Frontend (executes query_topic_history)
↓ tool results
Backend (LLM generates final response)
↓ response
Frontend (displays to user)
Tool Definition:
{
name: "query_topic_history",
description: "Get historical messages for an MQTT topic",
parameters: {
topic: "string - MQTT topic path",
limit: "number - max messages to return (default 10)"
}
}
Status: Planned for future implementation
Conclusion
The LLM now successfully:
- ✅ Infers MQTT systems from patterns without explicit rules
- ✅ Generates accurate proposals for major home automation platforms
- ✅ Avoids false positives on read-only sensors
- ✅ Matches payload formats to observed patterns
- ✅ Passes comprehensive test suite (17/17 tests)
This demonstrates that pattern-based learning with system name priming is more flexible and maintainable than explicit format rules.