ChatGPT SQL Query Generator AI

Build a ChatGPT SQL Query Generator AI – Using Lanchain & Streamlit

Live ChatGPT Streamlit Lanchain SQL Generator Chat App

This chatbot will take SQL Schemas as its input & generate a SQL query. All you need to provide the chatbot is a openai API key, Database Schema & the query in natural language. The ChatGPT model will do its magic and spit out a query for you.

I have used Streamlit for the UI . Streamlit lets you build all the UI needed only using langchain

The final App Looks like this:

Here’s How I built ChatGPT SQL Query Generator AI

1) Install langchain and streamlit

pip install langchain streamlit openai streamlit_lottie requests

You should ideally install the packages in a virtual environment. Please refer to the documentation to install a venv

There is a requirments.txt file in the github link :

  • create a virtual environment – python -m venv env
  • Activate the virtual environment (Windows) – env/Scripts/Activate.ps1
  • pip install -r requirments.txt

The above should get you all set in terms of the environment setup

2) Get Openai API Key

  • Goto https://platform.openai.com/
  • Login or Signup for new account
  • Goto API keys – OpenAI API
  • Create & Save your API Key
  • Keep API handy as you will use this later

3) Clone the Code:

You can git clone the code from my repository or directly make a streamlit app using my code

git clone https://github.com/satyadevshetty/ChatGPT-SQLQueryGenerator.git

Change directory to cloned project

cd ChatGPT-SQLQueryGenerator

4) Deploy a Streamlit App:

  • Goto https://share.streamlit.io/
  • New App –> Use Existing repo

5) Test Out the App

 

Code Explanation

  • model_engine = “text-davinci-003”
    • We use this model, its a little dated , however it does the job of generating SQL queries. Also, the cost of using this older model is lower as compared to the latest GPT-3 model
  • prompt = f”Translate this natural language query into syntactically correct SQL:\n\n{query}\n\nSQL Query:”
    • We are basically telling ChatGPT to take a natural language query & generate a SQL query
openai.Completion.create(
                            engine=model_engine,
                            prompt=prompt,
                            max_tokens=2048,
                            n=1,
                            stop = “\\n”,
                            temperature=0.5,
                            frequency_penalty = 0.5,
                            presence_penalty = 0.5,
                            logprobs = 10)
  • This is what openai says about completions : “Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.”
  • For more information on completions & its parameters you can visit https://platform.openai.com/docs/api-reference/completions
  • Above is the main code which is used to send the prompt & receive response from ChatGPT
You can download the code from here on GitHub