Skip to main content

Generate SQL queries from natural language

Project description

GitHub PyPI Colab Documentation
GitHub PyPI Colab Documentation

Vanna.AI

Vanna is a Python-based AI SQL co-pilot. Our initial users are data-savvy data analysts, data scientists, engineers, and similar people that use Vanna to automate writing complex SQL.

Vanna can:

Natural Language to SQL

sql = vn.generate_sql(question='Who are the top 10 customers?')

Output:

SELECT customer_name,
       total_sales
FROM   (SELECT c.c_name as customer_name,
               sum(l.l_extendedprice * (1 - l.l_discount)) as total_sales,
               row_number() OVER (ORDER BY sum(l.l_extendedprice * (1 - l.l_discount)) desc) as rank
        FROM   snowflake_sample_data.tpch_sf1.lineitem l join snowflake_sample_data.tpch_sf1.orders o
                ON l.l_orderkey = o.o_orderkey join snowflake_sample_data.tpch_sf1.customer c
                ON o.o_custkey = c.c_custkey
        GROUP BY customer_name)
WHERE  rank <= 10;

Run SQL

This function is provided as a convenience. You can choose to run your SQL however you normally do and use the rest of the downstream functions.

df = vn.get_results(cs, database, sql)

Output:

customer_name total_sales
Customer#000000001 68127.72
Customer#000000002 65898.69
...

Generate Plotly Code

plotly_code = vn.generate_plotly_code(question=my_question, sql=sql, df=df)

Output:

fig = go.Figure(go.Bar(
    x=df['CUSTOMER_NAME'],
    y=df['TOTAL_SALES'],
    marker={'color': df['TOTAL_SALES'], 'colorscale': 'Viridis'},
    text=df['TOTAL_SALES'],
    textposition='auto',
))

fig.update_layout(
    title="Top 10 Customers by Sales",
    xaxis_title="Customer",
    yaxis_title="Total Sales",
    xaxis_tickangle=-45,
    yaxis_tickprefix="$",
)

Run Plotly Code

fig = vn.get_plotly_figure(plotly_code=plotly_code, df=df)
fig.show()

Output:

Top 10 Customers by Sales

Improve Your Training Data

vn.store_sql(
    question=my_question,
    sql=sql,
)

How Vanna Works

flowchart LR
    DB[(Known Correct Question-SQL)]
    Try[Try to Use DDL/Documentation]
    SQL(SQL)
    Check{Is the SQL correct?}
    Generate[fa:fa-circle-question Use Examples to Generate]
    DB --> Find
    Question[fa:fa-circle-question Question] --> Find{fa:fa-magnifying-glass Do we have similar questions?}
    Find -- Yes --> Generate
    Find -- No --> Try
    Generate --> SQL
    Try --> SQL
    SQL --> Check
    Check -- Yes --> DB
    Check -- No --> Analyst[fa:fa-glasses Analyst Writes the SQL]
    Analyst -- Adds --> DB

Getting Started

Install Vanna from PyPI and import it:

%pip install vanna
import vanna as vn

Enter your email to set an API Key

This will send a one-time code to your email address. Copy and paste the code into the prompt.

my_email = '' # Enter your email here
vn.login(email=my_email)

Add Training Data

vn.train(
    question="Which products have the highest sales?",
    sql="...",
)

Generate SQL

sql = vn.generate_sql(question="Who are the top 10 customers?")

Documentation

Full Documentation

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vanna-0.0.14.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vanna-0.0.14-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file vanna-0.0.14.tar.gz.

File metadata

  • Download URL: vanna-0.0.14.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for vanna-0.0.14.tar.gz
Algorithm Hash digest
SHA256 f0778a8a1e6d99cef3b2952463ec23c4517ffb2cc0c90e86f041c89104e1e912
MD5 bd13ae0d401b8fd58bff3686724b1732
BLAKE2b-256 1a1178b09314e27eb4ed9cd468dacb3dfeb43a58608b31136e0c7a72a66918a4

See more details on using hashes here.

File details

Details for the file vanna-0.0.14-py3-none-any.whl.

File metadata

  • Download URL: vanna-0.0.14-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for vanna-0.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 e0dea1def216cd31c862f3b686be6fdd36f8915029d3a0820db0b037f40b42a3
MD5 23b1c885f15e1edbe6dcc227cff9c2a4
BLAKE2b-256 f83150c2336c65459e29f00d31046b2f0f662bbd8ca9e4816094d94339c47cc2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page