Welcome to simple-text-to-query ! This project demonstrates how to leverage OpenAI's ChatGPT as well as a local LLM to convert natural language text into SQL queries using SQLite3.
This repository contains a Jupyter notebook that showcases a practical approach to translating natural language into SQL queries. By integrating ChatGPT with SQLite3, users can input natural language text and receive corresponding SQL queries that can be executed against an SQLite database. We also demonstrate how injecting SQL schema information can prevent hallucination by LLM. Finally, if you want your database information to be private, you can just load the LLM weights locally and run queries againts them.
- SQLite3 Integration: Setup a local SQLite3 DB. Execute the generated SQL queries on an SQLite3 database.
- Translate to SQL: Utilizes ChatGPT or a local LLM to understand and convert user questions written in plain languageto proper SQL syntax.
- Load a LLM locally: Showcases how to download and use the LLM weights locally.
- Prevent hallucination: Learn how to modify your prompts to avoid hallucinations.
To run this notebook, you'll need:
- Python 3.7 or higher
- Jupyter Notebook or JupyterLab
- Required Python libraries, which can be installed using
pip
You can install the required dependencies using pip. Here’s a sample requirements.txt
:
openai
sqlite3
huggingface_hub
torch
transformers
Install the dependencies with:
pip install -r requirements.txt
-
Clone the Repository:
https://github.com/saketd403/simple-text-to-query.git cd simple-text-to-query
-
Obtain API Key:
Sign up for OpenAI and obtain an API key. Note that this might require you to pay for subscription.
-
Open the Notebook:
Launch Jupyter Notebook:
jupyter notebook
Navigate to
simple-text-to-query.ipynb
and open it.
-
Load the Notebook:
Open the
simple-text-to-query.ipynb
notebook in Jupyter. -
Input Natural Language Queries:
In the notebook, you will find cells where you can enter natural language text. The notebook will use ChatGPT to convert this text into an SQL query.
-
Execute the SQL Query:
The generated SQL query is then executed against an SQLite3 database. The results are displayed within the notebook.
Here's an example of how a natural language query might be converted:
- Input: "Show me all users who signed up in the last month."
- Generated SQL Query:
SELECT * FROM users WHERE signup_date >= DATE('now', '-1 month');
Contributions are welcome! Please follow these steps if you want to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature
). - Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature/YourFeature
). - Create a new Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.