Chat with your SQL database using Claude 3

A blog post detailing how to use Claude for chatting with your database via Vanna.ai

Arslan Shahid
4 min readMar 29, 2024
Image by the Author

A few weeks Anthropic released Claude 3, a series of multimodal models set to compete with OpenAI ChatGPT models. In their initial demo, Anthropic showed how Claude can be used for doing quantitative & analytical work. SQL is quintessential for any data analytics work if Claude is set to automate many analytical work, it must be great at generating SQL queries specific to any organization’s database.

Vanna is a free open-source data science Python library that allows users to use any LLM, vector store, and SQL database to translate natural language queries into SQL. It allows you to build a retrieval augment generation pipeline over your database in a few lines of code.

Getting started with Vanna

Vanna is designed so that you can integrate any LLM. It also has pre-configured LLMs like OpenAI ChatGPT 3.5, Mistral & GPT 4, etc. However, Claude’s implementation is not pre-configured, so we must implement it ourselves.

Before getting into the code you would need to do a few things.

  1. Anthropic API Key: You can get your api_key by going here
  2. Vanna API key and model: You would need to sign up at Vanna and create a model here
# we have to import Vanna and the anthropic Python library
from vanna.vannadb.vannadb_vector import VannaDB_VectorStore
from vanna.base import VannaBase
from vanna.anthropic.anthropic_chat import Anthropic_Chat


class MyVanna(VannaDB_VectorStore, Anthropic_Chat):
def __init__(self, config=None):
MY_VANNA_MODEL = # Your model name from https://vanna.ai/account/profile
VannaDB_VectorStore.__init__(self, vanna_model=MY_VANNA_MODEL, vanna_api_key= #insert Vanna_API key, config=config)
Anthropic_Chat.__init__(self, config=config)

api_key = # insert your API key
# You can change models to any Anthropic model
# You can use Sonnet and Haiku but this post uses Opus
model = "claude-3-opus-20240229"
config = {'api_key':api_key,'model':model}

# Passes the configuration to MyVanna object
vn = MyVanna(config=config)

# connects to free sample Vanna database Chinook
vn.connect_to_sqlite('https://vanna.ai/Chinook.sqlite')

Training Vanna

Vanna allows you to train on DDL, question-SQL pairs, and documentation (additional context). Here are code snippets you can use to train on each type.

Data Definition Language (DDL)


# You can train based on the DDL of the tables in the dB
# Chinook
vn.train(ddl="""CREATE TABLE [Invoice]
(
[InvoiceId] INTEGER NOT NULL,
[CustomerId] INTEGER NOT NULL,
[InvoiceDate] DATETIME NOT NULL,
[BillingAddress] NVARCHAR(70),
[BillingCity] NVARCHAR(40),
[BillingState] NVARCHAR(40),
[BillingCountry] NVARCHAR(40),
[BillingPostalCode] NVARCHAR(10),
[Total] NUMERIC(10,2) NOT NULL,
CONSTRAINT [PK_Invoice] PRIMARY KEY ([InvoiceId]),
FOREIGN KEY ([CustomerId]) REFERENCES [Customer] ([CustomerId])
ON DELETE NO ACTION ON UPDATE NO ACTION
)""")

Question-SQL Pair

# You can train on SQL question pairs, which tells the type of generated SQL you prefer
# This shows a basic example
vn.train(question = "How many albums are there?"
, sql ="SELECT COUNT(*) AS TotalAlbums
FROM Album")

Documentation

# Documentation includes additional natural language information 
# that you would give to human, like business definitions, metrics etc
# Or information about the DB syntax
vn.train(documentation ="This is a SQLite database. For dates rememeber to use SQLite syntax.")

Training Data

# You can see your training data here
vn.get_training_data()
Image by the Author — Shows the training data for pre-trained sample model Chinook

Asking Questions

You can ask questions directly in Python or launch the built-in Flask app.

vn.ask("Calculate the sales per album?")
Image by the Author. SQL generated
Image by the Author, Shows the dataframe
Image by the Author, shows the Plotly Figure.

Launching Flask App

You can also use the UI of the built-in Vanna Flask App.

# code snippet to launch Vanna Flask App
from vanna.flask import VannaFlaskApp
app = VannaFlaskApp(vn)
app.run()
Image by the Author
Image by the Author, Vanna Flask App enables you to ask SQL queries directly

Please follow as I plan to work on a post comparing other LLMs and Claude. Benchmarking it against all other available LLMs!

--

--

Arslan Shahid

Life has the Markov property, the future is independent of the past, given the present