Welcome to the Text-to-SQL project! This repository demonstrates a simple yet powerful way to translate natural language queries into SQL statements using an LLM (Large Language Model). Let's dive into the details of this project and how you can get it up and running. π
Text-to-SQL is a Natural Language Processing (NLP) technique that translates human language queries into structured SQL (Structured Query Language) statements. This project leverages sample data on Customers and Purchases, which are stored in ClickHouse tables, and utilizes the clickhouse-migrations Python library for managing database migrations.
For the LLM, I used Ollama, which helps generate SQL queries based on user questions and returns natural language responses based on the SQL results.
.
βββ data
β βββ Customers.csv
β βββ Purschases.csv
βββ src
β βββ data
β | βββ run_migrations.py
β βββ migrations
β | βββ 001_create_customers_tbl.sql
β | βββ 001_create_purchases_tbl.sql
β βββ text_to_sql
β | βββ main.py
β βββ __init__.py
βββ .env
βββ .gitignore
βββ README.md
βββ requirements.txt
git clone git@github.com:M-Nkirote/Simple-Text-to-SQL-Solution.git
cd Simple-Text-to-SQL-Solution
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Refer to Clickhouse Installation Steps.
cd src/data
python3 run_migrations.py
tail -n +2 /Users/nkirote/Simple-Text-to-SQL-Solution/data/Customers.csv | ./clickhouse client --query="INSERT INTO simple_text_to_sql.Customers FORMAT CSV"
tail -n +2 /Users/nkirote/Simple-Text-to-SQL-Solution/data/Customers.csv | ./clickhouse client --query="INSERT INTO simple_text_to_sql.Customers FORMAT CSV"
cd src/text_to_sql
python3 main.py
: How many people are from Kiambu?
: Based on our customer database, it appears that there are two individuals who hail from Kiambu. These individuals are John Doe and Bob Johnson. They can be found in the Customers table with a city of "Kiambu" and corresponding zip codes of 62701 and 62703 respectively.
: How much has John Doe spent so far?
: John Doe has spent a total of $1,520 so far.
: Which items have Bob Johnson ever bought?
: Bob Johnson has purchased a Tablet and a Laptop.