Getting Started with Vanna¶
This notebook shows how to use Vanna to ask questions from a database using sample data
Install and Import Vanna¶
In [ ]:
%pip install vanna
In [1]:
import vanna
from vanna.remote import VannaDefault
Log In to Vanna¶
Vanna provides a function to get an API key. You'll get a code sent to your e-mail. You can save your API key for future usage so that you don't have to log in every time.
In [2]:
api_key = vanna.get_api_key('my-email@example.com')
Set Model¶
chinook
is a public model that refers to the Chinook sample database
In [ ]:
vanna_model_name = 'chinook' # This is the name of the RAG model. This is typically associated with a specific dataset.
vn = VannaDefault(model=vanna_model_name, api_key=api_key)
Connect to the Database¶
Here we're connecting to a SQLite database but you can connect to any SQL database.
In [4]:
vn.connect_to_sqlite('https://vanna.ai/Chinook.sqlite')
Ask Questions¶
Now we're going to use vn.ask
to ask questions and it'll generate SQL, run the SQL, show the table, and generate a chart
In [5]:
vn.ask("What are the top 5 artists by sales?")
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;
Name | totalsales | |
---|---|---|
0 | Iron Maiden | 140 |
1 | U2 | 107 |
2 | Metallica | 91 |
3 | Led Zeppelin | 87 |
4 | Os Paralamas Do Sucesso | 45 |
AI-generated follow-up questions:
- What is the total sales for each artist?
- What are the top-selling albums for each artist?
- Which genre has the highest sales?
- What is the total sales for each genre?
- What are the sales trends over the years?
- How many albums are there in the database?
- Who are the top-selling artists in each genre?
- What are the top-selling tracks?
- What is the average sales per artist?
- How does the sales distribution vary across different genres?
Now try your own question¶
For reference, these are the tables in the database
In [ ]:
vn.ask()
Launch the User Interface¶
In [ ]:
from vanna.flask import VannaFlaskApp
app = VannaFlaskApp(vn)
app.run()