├── README.md └── gpt3.py /README.md: -------------------------------------------------------------------------------- 1 | # Text-To-SQL-Using-GPT-3 2 | Taking Normal Text as Input and Generating SQL commands using the OpenAI's GPT-3 3 | -------------------------------------------------------------------------------- /gpt3.py: -------------------------------------------------------------------------------- 1 | import json 2 | import openai 3 | 4 | with open('GPT_SECRET_KEY.json') as f: 5 | data = json.load(f) 6 | 7 | openai.api_key = data["API_KEY"] 8 | 9 | from gpt import GPT 10 | from gpt import Example 11 | 12 | gpt = GPT(engine="davinci", 13 | temperature=0.5, 14 | max_tokens=100) 15 | 16 | gpt.add_example(Example('Fetch unique values of DEPARTMENT from Worker table.', 17 | 'Select distinct DEPARTMENT from Worker;')) 18 | gpt.add_example(Example('Print the first three characters of FIRST_NAME from Worker table.', 19 | 'Select substring(FIRST_NAME,1,3) from Worker;')) 20 | gpt.add_example(Example("Find the position of the alphabet ('a') in the first name column 'Amitabh' from Worker table.", 21 | "Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Amitabh';")) 22 | gpt.add_example(Example("Print the FIRST_NAME from Worker table after replacing 'a' with 'A'.", 23 | "Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;")) 24 | gpt.add_example(Example("Display the second highest salary from the Worker table.", 25 | "Select max(Salary) from Worker where Salary not in (Select max(Salary) from Worker);")) 26 | gpt.add_example(Example("Display the highest salary from the Worker table.", 27 | "Select max(Salary) from Worker;")) 28 | gpt.add_example(Example("Fetch the count of employees working in the department Admin.", 29 | "SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';")) 30 | gpt.add_example(Example("Get all details of the Workers whose SALARY lies between 100000 and 500000.", 31 | "Select * from Worker where SALARY between 100000 and 500000;")) 32 | gpt.add_example(Example("Get Salary details of the Workers", 33 | "Select Salary from Worker")) 34 | 35 | 36 | prompt = "Display the lowest salary from the Worker table." 37 | output = gpt.submit_request(prompt) 38 | output.choices[0].text --------------------------------------------------------------------------------