A simple natural language to SQL converter
- Python
- Pandas
- OpenAI API
- SQLAlchemy / SQLite
- Pandas converts the data file (in this case,
instruments.json
) into a data frame - A temporary SQLite database is created using SQLAlchemy and the data frame is pushed to the database
- Your request from the
user_input
variable is converted into a usable prompt - The prompt is sent to OpenAI's Completion API
- Grab the resulting query and run it in the database
### sqlite SQL table, with its properties:
#
# instruments({columns in data})
#
### A query to answer: {natural language prompt}
SELECT
The SELECT keyword at the end of the prompt is meant to force the LLM to produce a SELECT statement only.
This app is unable to produce potentially harmful DROP, DELETE, and UPDATE statements because the Completion API stops producing tokens after a semi-colon, which signifies the end of the SELECT statement.
- Get an API key from OpenAI, and place it in a file called
secret.py
in the root - You can replace the data if you wish. Check the Pandas docs to see supported files.
- Create a virtual environment by running
python3 -m venv env
- Start the environment by running
source env/bin/activate
- Run
pip install -r requirements.txt
to install dependencies - Run at anytime by running
python3 app.py