AI Agent per Admin Panel π€
PrioritΓ : π‘ Media
Effort Stimato: 2-3 settimane
Stato: Proposta
Problemaβ
Attualmente per investigare problemi sui device (es. "perchΓ© questo device era offline?") Γ¨ necessario:
- Controllare manualmente i logs su Grafana
- Query manuali su Redis e PostgreSQL
- Verificare i vari microservizi coinvolti
- Correlare manualmente le informazioni
Questo processo richiede conoscenza tecnica approfondita e molto tempo.
Soluzione Propostaβ
Integrare un AI Agent direttamente nell'admin panel che puΓ²:
- Rispondere a domande in linguaggio naturale
- Interrogare automaticamente database, API e logs
- Fornire analisi contestuali sui device
- Suggerire azioni correttive
Architetturaβ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Admin Panel (React) β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β Device Card ββ
β β βββββββββββββββββββ βββββββββββββββββββββββββββββββββ ββ
β β β Device Info β β π€ AI Assistant β ββ
β β β ID: xxx-985 β β β ββ
β β β Status: Online β β "PerchΓ© questo device era β ββ
β β β Last seen: ... β β offline ieri alle 14:00?" β ββ
β β βββββββββββββββββββ β β ββ
β β β β Risposta con analisi logs, β ββ
β β β eventi, e dati Redis β ββ
β β βββββββββββββββββββββββββββββββββ ββ
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β AI Agent Microservice (Python) β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββ β
β β LLM Client β β Tools β β Context Builder β β
β β (OpenAI/ β β - query_db β β - Device context β β
β β Anthropic) β β - call_api β β - User permissions β β
β β β β - get_logs β β - History β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββΌββββββββββββββββββ
βΌ βΌ βΌ
ββββββββββββ ββββββββββββ ββββββββββββ
β Devices β β Positionsβ β Redis β
β API β β API β β (Logs) β
ββββββββββββ ββββββββββββ ββββββββββββ
Componenti Tecniciβ
1. AI Agent Microserviceβ
Nuovo microservice Python che segue la Clean Architecture esistente:
services/ai-agent/
βββ src/
β βββ application/
β β βββ use_cases/
β β β βββ process_query.py # Main use case
β β β βββ stream_response.py # SSE streaming
β β βββ ports/
β β βββ llm_port.py # LLM abstraction
β β βββ tool_port.py # Tool abstraction
β βββ domain/
β β βββ entities/
β β β βββ conversation.py
β β β βββ tool_call.py
β β βββ services/
β β βββ context_builder.py
β βββ infrastructure/
β β βββ adapters/
β β β βββ openai_adapter.py
β β β βββ anthropic_adapter.py
β β β βββ tools/
β β β βββ database_tool.py
β β β βββ api_tool.py
β β β βββ redis_tool.py
β β β βββ loki_tool.py
β β βββ api/
β β βββ routes.py
β βββ main.py
βββ tests/
βββ Dockerfile
βββ pyproject.toml
2. Tools Disponibiliβ
| Tool | Descrizione | Esempio Query |
|---|---|---|
query_devices | Interroga il database devices | "Trova device con IMEI xxx" |
query_positions | Ultime posizioni di un device | "Dove si trovava ieri?" |
query_events | Eventi/allarmi di un device | "Quali allarmi ha generato?" |
query_redis | Stato presence in Redis | "Γ online adesso?" |
query_logs | Cerca nei logs Loki | "Errori nelle ultime 24h" |
send_command | Invia comando al device | "Invia comando di localizzazione" |
3. Frontend Componentβ
// components/AIAssistant.tsx
interface AIAssistantProps {
deviceId?: string; // Contesto device opzionale
userId?: string; // Contesto utente opzionale
initialContext?: string; // Contesto iniziale
}
const AIAssistant: React.FC<AIAssistantProps> = ({ deviceId, userId }) => {
const [messages, setMessages] = useState<Message[]>([]);
const [isLoading, setIsLoading] = useState(false);
const sendMessage = async (content: string) => {
// SSE streaming per risposte real-time
const response = await fetch('/api/ai-agent/chat', {
method: 'POST',
body: JSON.stringify({
message: content,
context: { deviceId, userId },
}),
});
// Handle SSE stream...
};
return (
<div className="ai-assistant">
<ChatHistory messages={messages} />
<ChatInput onSend={sendMessage} isLoading={isLoading} />
</div>
);
};
Tecnologieβ
| Componente | Opzione Consigliata | Alternative |
|---|---|---|
| LLM Provider | OpenAI GPT-4 | Anthropic Claude, Google Gemini |
| Framework | LangChain | LlamaIndex, custom implementation |
| Streaming | Server-Sent Events (SSE) | WebSocket |
| Caching | Redis | In-memory |
Stima Costiβ
Costi Infrastrutturaβ
- Nessun costo aggiuntivo GKE (pod piccolo, ~100m CPU, 256Mi RAM)
Costi API LLM (stima mensile)β
| Scenario | Query/mese | Costo Stimato |
|---|---|---|
| Uso leggero | 500 | ~$10-15 |
| Uso medio | 2000 | ~$40-60 |
| Uso intenso | 5000 | ~$100-150 |
Basato su GPT-4 Turbo pricing (~$0.01/1K input, ~$0.03/1K output)
Considerazioni di Sicurezzaβ
Accesso e Permessiβ
- β Solo utenti admin possono usare l'agent
- β Le query sono limitate ai device dell'account
- β Audit log di tutte le interazioni
- β Rate limiting per prevenire abusi
Dati Sensibiliβ
- β οΈ Non inviare dati sensibili al LLM
- β οΈ Sanitizzare output prima di mostrare
- β οΈ Mascherare IMEI/numeri telefono nei logs
Esempio di Interazioneβ
π€ Admin: "PerchΓ© il device S21L-985 era offline ieri pomeriggio?"
π€ Agent pensando...
1. π Query devices API β Device trovato, owner: user@example.com
2. π Query positions β Ultima posizione: 14:23:45
3. π Query Redis presence β Gap dalle 14:23 alle 15:10
4. π Query logs Loki β Trovato: "ws-consumer lag warning"
π€ Agent: "Il device S21L-985 Γ¨ risultato offline dalle 14:23 alle 15:10
di ieri. L'analisi dei logs mostra che il consumer WebSocket aveva
accumulato un lag di 45 minuti nella lettura del presence stream.
π Timeline:
- 14:23:45 - Ultima posizione ricevuta
- 14:25:00 - Device marcato offline (timeout 10 min)
- 15:10:22 - Consumer riavviato automaticamente
- 15:10:30 - Device tornato online
π‘ Suggerimento: Considera di aumentare le repliche del ws-consumer
o implementare un circuit breaker per gestire meglio i picchi di carico."
Roadmap Implementazioneβ
Fase 1: MVP (1 settimana)β
- Setup microservice base
- Integrazione OpenAI/Anthropic
- Tool base: query_devices, query_positions
- UI chat semplice nell'admin
Fase 2: Espansione (1 settimana)β
- Aggiunta tools: query_events, query_redis
- Streaming SSE
- Context awareness (device selezionato)
- History conversazioni
Fase 3: Polish (1 settimana)β
- Query logs Loki
- Comandi device (send_command)
- Caching risposte
- Rate limiting e audit