To generate database queries from the text data, we need to first train our GPT-3 model based on our needs. To train the GPT-3 model on our database queries, we will need a set of questions and answers. Here, the question represents the text query and the answer represents the database query.
First, you need to install the
openai library. Run the following command:
!pip install openai
You can now import the packages like so:
from gpt import GPT
from gpt import Example
After applying for API access, you will get a JSON file with an API key that you can import in the line of Python code below:
with open('GPT_SECRET_KEY.json') as f:
data = json.load(f)openai.api_key = data["API_KEY"]
Now, we can create an instance of GPT-3 by running:
gpt = GPT(engine="davinci",
There you go! We can now add some database queries to learn:
gpt.add_example(Example('Fetch unique values of SALARY from payment table.','Select distinct SALARY from payment;'))
This way, we can add all the queries manually to make a list of queries. You can also add them using a loop.
Finally, after adding all the data, we can call
gpt.submit_request(), which takes text as an input and provides a database query as the output.