This program is designed to translate natural language statements into SQL queries using OpenAI API, pandas library, and sqlalchemy. It is useful for those who are not familiar with SQL syntax and want to query databases using natural language.
- OpenAI API (https://beta.openai.com/docs/)
- pandas library (https://pandas.pydata.org/)
- sqlalchemy (https://www.sqlalchemy.org/)
You can install these dependencies using pip by running the following command:
pip install openai pandas sqlalchemy
openai.api_key
dataset
- To set up an OpenAI API key. You can sign up for an API key on the OpenAI website: https://beta.openai.com/signup/.
Note that it is very important that this API Key is kept secure. Do not share it with anyone else. - Once you have an API key, create a file named .env in the root directory of this project and add the following line:
OPENAI_API_KEY="YOURAPIKEY"
- Then, set the variable in the
main.py
file:openai.api_key = environ.get('OPENAI_API_KEY')
- Make sure that you have downloaded your dataset to somewhere on your computer
- Copy the file path of your dataset and set it to the variable in the
main.py
file: dataset=r"YOURFILEPATH"
main.py
file and enter a natural language statement when prompted.
The program will use the OpenAI API to generate a SQL query based on your statement, which will then be executed using sqlalchemy.