Samagra-Development / Text2SQL

20 stars 11 forks source link

Text2SQL

Open in Gitpod

Introduction

This tool converts natural language queries into SQL commands and fetches data from a configured database. It's an intuitive way for individuals without SQL knowledge to interact with databases, or for experts to streamline and automate their work.

There are two inputs

  1. schema.sql - The schema of the database
  2. text/prompt - The text to be converted to SQL. Example - "Number of students in class 5?"

The process is

APIs

Installation

  1. Rename .env.sample file to .env
  2. Update OPENAI_API_KEY with your own openai api key. You can get your own api key by clicking here.
  3. Now execute the below commands to setup server and other services.
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    pip install -r src/server/sql_graph/requirements.txt
    docker compose -f docker-compose.gitpod.yml up -d

Running Tests

python -m unittest tests.related_tables

Running the app

python src/server/app.py

Setting up dummy education database

cd src/server/db/mock-data
sudo sh init_mock_data.sh 

Note down the Schema_ID that gets stored in the schema_id.txt file. You will have to send this as param in the prompt api.

pygraphviz installation

This will be required for sql_graph.

Linux

sudo apt update
sudo apt-get install libgraphviz-dev
sudo apt install pkg-config graphviz-dev -y
pip install pygraphviz

Mac OS

sudo apt-get install libgraphviz-dev
pip3 install --global-option=build_ext --global-option="-I/opt/homebrew/Cellar/graphviz/8.0.5/include/" --global-option="-L/opt/homebrew/Cellar/graphviz/8.0.5/lib/" pygraphviz

brew install graphviz
python -m pip install \
    --global-option=build_ext \
    --global-option="-I$(brew --prefix graphviz)/include/" \
    --global-option="-L$(brew --prefix graphviz)/lib/" \
    pygraphviz

Usage

curl --location 'https://localhost:5078/prompt/v3' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic dGVzdDp0ZXN0' \
--data '{
    "schema_id": "<Schema ID>",
    "prompt": "How many Primary School are there?"
}'

Contribution

Check our QuickStart guide if you want to contribute to this project.

You can watch this video which will help you in setting up Gitpod for the project.

Gitpod Setup Tutorial

License

This tool is released under the MIT License.