Deprecated: Creation of dynamic property Timber\Site::$pingback is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Site.php on line 180

Deprecated: Creation of dynamic property Timber\User::$name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/User.php on line 118

Deprecated: Creation of dynamic property Timber\Post::$has_disclaimer is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_lock is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_edit_last is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_meta_box_present is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_send_notification is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_modify_title_and_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_heading is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$onesignal_notification_custom_content is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskw is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_metadesc is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_linkdex is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_content_score is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_focuskeywords is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_keywordsynonyms is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_estimated-reading-time-minutes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_wordproof_timestamp is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_tipologia is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_attributi is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_in_evidenza_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_inviato_in_newsletter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_category is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_blz_affiliation_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_content_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_attributes is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_wpseo_primary_speciali is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_thumbnail_id is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$response_body is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$_yoast_indexnow_last_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_date_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$ping_status is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_password is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_name is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$to_ping is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$pinged is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_modified_gmt is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_content_filtered is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$guid is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$menu_order is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$post_mime_type is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$comment_count is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71

Deprecated: Creation of dynamic property Timber\Post::$filter is deprecated in /var/www/html/web/app/themes/ilsoftware/vendor/timber/timber/lib/Core.php on line 71
Vanna, ovvero come chattare con un database SQL usando l'IA | IlSoftware.it

Vanna, ovvero come chattare con un database SQL usando l'IA

Come interrogare un database SQL usando una richiesta in linguaggio naturale con Vanna.ai, l'intelligenza artificiale e un LLM (Large Language Model). L'IA compone la query SQL, la visualizza e ne mostra l'output accedendo alla base dati.

Gli SQL JOIN sono operatori che consentono di combinare righe da due o più tabelle in base a una condizione specifica. Le tabelle del database SQL sono collegate in base ai valori delle colonne definite nella condizione di JOIN. L’operazione consente di ottenere risultati più articolati e completi rispetto a una singola query sul contenuto di una specifica tabella contenuta nel database.

Un INNER JOIN, ad esempio, restituisce solo le righe che hanno corrispondenze in entrambe le tabelle. Il LEFT (OUTER) JOIN fornisce tutte le righe dalla prima tabella specificata (quella a sinistra) e le corrispondenti righe dalla tabella a destra (la seconda tabella indicata). Al contrario, RIGHT (OUTER) JOIN dà tutte le righe della tabella a destra e le corrispondenti righe tratte dalla tabella a sinistra. Infine, il FULL (OUTER) JOIN restituisce tutte le righe quando c’è una corrispondenza in una delle tabelle.

Padroneggiare l’utilizzo dei JOIN non è semplice, soprattutto quando le tabelle sono contraddistinte da una struttura complessa e non facilmente sovrapponibile.

Cos’è e come funziona la libreria Vanna che sfrutta l’intelligenza artificiale

Vanna.ai è una libreria Python open source che permette di usare Large Language Models (LLM) come OpenAI GPT o Mistral per interrogare database utilizzando il linguaggio naturale. Invece di utilizzare la sintassi SQL, si possono porre domande del tipo “quali sono i prodotti tecnologici che hanno generato un numero di vendite maggiore per Paese nel corso degli ultimi due anni?” Il tutto senza più mettere mano alle tradizionali query SQL.

Il compito di generare l’interrogazione SQL corretta e di eseguirla sul database, fa capo proprio a Vanna. Il modello inserisce ogni query corretta in un database vettoriale così da migliorare costantemente il comportamento del LLM.

Eseguire query SQL su database con Vanna

La documentazione ufficiale spiega come installare Vanna nel proprio ambiente e utilizzare l’apposita funzione per generare una chiave API. La chiave può essere memorizzata in modo da non dover ogni volta effettuare l’operazione di login.

Gli ideatori di Vanna spiegano anche come specificare l’utilizzo del proprio LLM preferito, suggerendo però di iniziare con un modello pubblico generato a partire da un database di esempio (Chinook).

Il database di esempio Chinook

Il database di esempio Chinook è un database relazionale ampiamente utilizzato per scopi didattici, dimostrativi e di pratica nell’ambito della gestione di database e del linguaggio SQL. È progettato per simulare un sistema di gestione di una libreria di musica digitale e include diverse tabelle correlate, che rappresentano concetti come artisti, album, tracce, clienti, ordini e altro ancora. Le tabelle sono progettate in modo tale da riflettere le relazioni tipiche che esistono all’interno di una tipica applicazione per la gestione di una libreria musicale.

La sezione Train contiene le informazioni utili per addestrare il modello a partire da un database proprio. Vanna supporta sorgenti di dati in vari formati ed è in grado di connettersi a basi di dati Snowflake, BigQuery, PostgreSQL così come ad altre popolari fonti. Al di là di PostgreSQL, gli altri due sono sistemi di gestione di database e servizi di data warehousing basati sul cloud. A conferma di come Vanna sia una soluzione pensata per interagire con volumi di dati impegnativi.

Un esempio che fa capire le potenzialità di Vanna

Si prenda come esempio il seguente codice Python, preso pari pari dalla documentazione del progetto. In primis importiamo diverse librerie, come pandas per la manipolazione dei dati, numpy per le operazioni matematiche, Vanna che attiva l’interazione con il sistema di question-answer basato su linguaggio naturale, e plotly per la visualizzazione dei dati.

Inizialmente si impostano e si utilizzano le chiavi API per l’uso di Vanna quindi è richiesto l’utilizzo del modello costruito a partire dal contenuto del database Chinook, citato in precedenza:

import pandas as pd
import numpy as np
import vanna as vn
import plotly as px

api_key = vn.get_api_key('nomeutente@nomeazienda.abc')
vn.set_api_key(api_key)
vn.set_model('chinook')

vn.connect_to_sqlite('https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite')
vn.ask("What are the top 5 artists by sales?")

Le ultime due righe stabiliscono una connessione con il database SQLite Chinook ospitato su GitHub e, infine, si trasmette all’intelligenza artificiale di Vanna un quesito di alto livello: “Quali sono i primi 5 artisti che hanno venduto di più?”

L’output fornito da Vanna consiste nella complessa ed efficace query SQL così strutturata:

SELECT a.name,
       sum(il.quantity) as totalsales
FROM   artist a
    INNER JOIN album al
        ON a.artistid = al.artistid
    INNER JOIN track t
        ON al.albumid = t.albumid
    INNER JOIN invoiceline il
        ON t.trackid = il.trackid
GROUP BY a.name
ORDER BY totalsales desc limit 5;

Esempio query SQL database AI

Quando la fase di addestramento è portata a termine in maniera corretta, Vanna e il sottostante LLM diventano in grado di generare query SQL pertinenti. Per set di dati di grandi dimensioni, tuttavia, l’analista è chiamato a fornire a Vanna degli indizi utili. Il potenziale è comunque enorme e stiamo appena grattando la superficie: suggeriamo ad esempio di provare query molto complesse, che sulla carte richiedono l’utilizzo di join tra diverse tabelle.

Risparmiare tanto tempo grazie all’intelligenza artificiale

Affinché le query SQL più complesse ed articolate funzionino e restituiscano risultati attendibili, il consiglio è quello di provare – come spesso accade con i modelli generativi – più strategie di prompt. Anche Vanna, come altri strumenti capaci di generare testi, immagini o musica, assicurano risultati migliori se l’utente descrive in maniera accurata e puntuale ciò che desidera ottenere attraverso una query.

Le soluzioni basate sull’intelligenza artificiale sono sempre più “di tendenza”, in tutti i settori e in particolare per migliorare il business. Giorno dopo giorno si rincorrono nuovi strumenti che offrono funzionalità di primo livello sfruttando LLM. Possiamo facilmente annoverare Vanna tra gli strumenti più utili, in grado di assicurare un enorme risparmio di tempo.

Vanna riduce significativamente il tempo che intercorre tra la fase di brainstorming sulle basi di dati e il completamento delle attività di analisi.

Credit immagine in apertura: iStock.com – islander11

Ti consigliamo anche

Link copiato negli appunti