Skip to content

M-Nkirote/Simple-Text-to-SQL-Solution

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Text-to-SQL Project πŸ“ŠπŸ§ 

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. πŸš€

Project Overview πŸ“‹

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.

Project Structure πŸ—‚οΈ

.
β”œβ”€β”€ 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

Set Up πŸ› οΈ

Clone the Repo

git clone git@github.com:M-Nkirote/Simple-Text-to-SQL-Solution.git

cd Simple-Text-to-SQL-Solution

Set up virtual environment

python -m venv venv

source venv/bin/activate

Install dependencies

pip install -r requirements.txt

Install ClickHouse

Refer to Clickhouse Installation Steps.

Run migrations

cd src/data

python3 run_migrations.py

To insert csv data into the ClickHouse table, you can run the following commands on terminal

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"

To run the text-to-sql script

cd src/text_to_sql

python3 main.py

Sample Questions and Answers πŸ—‚οΈ πŸ’¬

Question : How many people are from Kiambu?

Answer : 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.

Question : How much has John Doe spent so far?

Answer : John Doe has spent a total of $1,520 so far.

Question : Which items have Bob Johnson ever bought?

Answer : Bob Johnson has purchased a Tablet and a Laptop.

About

A simple Text-to-SQL flow using Ollama.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages