I hate GPT
This commit is contained in:
parent
d03e7fa086
commit
13e027afdf
|
|
@ -1,12 +1,9 @@
|
||||||
import getpass
|
import getpass
|
||||||
import modules.aisql as aisql
|
import modules.aisql as aisql
|
||||||
|
|
||||||
#TODO: Clear screen after every option
|
|
||||||
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tableschema = None
|
self.tableschema = {}
|
||||||
self.dbtype = 'postgresql'
|
self.dbtype = 'postgresql'
|
||||||
self.ip = 'localhost'
|
self.ip = 'localhost'
|
||||||
self.port = '32768'
|
self.port = '32768'
|
||||||
|
|
@ -17,6 +14,7 @@ class Main:
|
||||||
self.apikey = open('apikey', 'r').read().strip('\n')
|
self.apikey = open('apikey', 'r').read().strip('\n')
|
||||||
|
|
||||||
def config(self) -> object:
|
def config(self) -> object:
|
||||||
|
print('Configure the Database')
|
||||||
print(f'Database type')
|
print(f'Database type')
|
||||||
print("postgresql - PostgreSQL")
|
print("postgresql - PostgreSQL")
|
||||||
print("mysql - MySQL (WIP)")
|
print("mysql - MySQL (WIP)")
|
||||||
|
|
@ -27,6 +25,7 @@ class Main:
|
||||||
self.username = input(f'Username (current: {self.username}): ') or self.username
|
self.username = input(f'Username (current: {self.username}): ') or self.username
|
||||||
self.password = getpass.getpass(f'Password (current: {self.password}): ') or self.password
|
self.password = getpass.getpass(f'Password (current: {self.password}): ') or self.password
|
||||||
self.database = input(f'Database (current: {self.database}): ') or self.database
|
self.database = input(f'Database (current: {self.database}): ') or self.database
|
||||||
|
print('___')
|
||||||
|
|
||||||
if self.dbtype == 'postgresql':
|
if self.dbtype == 'postgresql':
|
||||||
# TODO: Add more Database languages
|
# TODO: Add more Database languages
|
||||||
|
|
@ -42,15 +41,22 @@ class Main:
|
||||||
print('4. Get Tables Schema')
|
print('4. Get Tables Schema')
|
||||||
print('5. Exit')
|
print('5. Exit')
|
||||||
option = input('Option: ')
|
option = input('Option: ')
|
||||||
|
print('___')
|
||||||
if option == '1':
|
if option == '1':
|
||||||
self.generate_sql()
|
self.generate_sql()
|
||||||
elif option == '2':
|
elif option == '2':
|
||||||
query = input('Query: ')
|
query = input('Query: ')
|
||||||
self.run_sql(query)
|
self.run_sql(query)
|
||||||
elif option == '3':
|
elif option == '3':
|
||||||
self.db, self.tableschema = self.config()
|
self.db = self.config()
|
||||||
elif option == '4':
|
elif option == '4':
|
||||||
print(self.db.get_schema())
|
if self.db is not None:
|
||||||
|
self.tableschema = self.db.get_schema()
|
||||||
|
print(self.tableschema)
|
||||||
|
else:
|
||||||
|
self.db = self.config()
|
||||||
|
self.tableschema = self.db.get_schema()
|
||||||
|
print(self.tableschema)
|
||||||
elif option == '5':
|
elif option == '5':
|
||||||
exit()
|
exit()
|
||||||
else:
|
else:
|
||||||
|
|
@ -61,13 +67,18 @@ class Main:
|
||||||
generate = True
|
generate = True
|
||||||
while generate:
|
while generate:
|
||||||
text = input('Text: ')
|
text = input('Text: ')
|
||||||
sql = ai.humantosql(text, self.db.get_schema())
|
print('___')
|
||||||
|
if self.db is None:
|
||||||
|
self.db = self.config()
|
||||||
|
|
||||||
|
sql = ai.humantosql(text, self.dbtype, self.db.get_schema())
|
||||||
print(f'SQL:\n{sql}\n___')
|
print(f'SQL:\n{sql}\n___')
|
||||||
print('Would you like to run it?')
|
print('Would you like to run it?')
|
||||||
print('1. Yes')
|
print('1. Yes')
|
||||||
print('2. No')
|
print('2. No')
|
||||||
print('3. Edit')
|
print('3. Edit')
|
||||||
choice = input('Choice: ')
|
choice = input('Choice: ')
|
||||||
|
print('___')
|
||||||
if choice == '1':
|
if choice == '1':
|
||||||
self.run_sql(sql)
|
self.run_sql(sql)
|
||||||
generate = False
|
generate = False
|
||||||
|
|
@ -80,39 +91,31 @@ class Main:
|
||||||
|
|
||||||
def run_sql(self, query):
|
def run_sql(self, query):
|
||||||
if self.db is None:
|
if self.db is None:
|
||||||
print('You have to configure the database first')
|
self.db = self.config()
|
||||||
print('1. Configure')
|
|
||||||
print('2. Exit')
|
ai = aisql.AI(self.apikey)
|
||||||
choice = input('Choice: ')
|
function = ai.decide(query)
|
||||||
if choice == '1':
|
print(f'Using {function}')
|
||||||
self.db, self.tableschema = self.config()
|
try:
|
||||||
self.run_sql(query)
|
if "fetchall".casefold() in function.casefold():
|
||||||
elif choice == '2':
|
print(self.db.fetchall(query))
|
||||||
exit()
|
elif "fetchone".casefold() in function.casefold():
|
||||||
|
print(self.db.fetchone(query))
|
||||||
|
elif "fetchmany".casefold() in function.casefold():
|
||||||
|
size = function.split('=')[1].strip(']')
|
||||||
|
print(self.db.fetchmany(query, size))
|
||||||
|
elif "execute".casefold() in function.casefold():
|
||||||
|
self.db.execute(query)
|
||||||
|
elif "executemany".casefold() in function.casefold():
|
||||||
|
size = function.split('=')[1].strip(']')
|
||||||
|
self.db.executemany(query, size)
|
||||||
else:
|
else:
|
||||||
print('Invalid option')
|
print('Invalid option')
|
||||||
elif self.db is not None:
|
print('___')
|
||||||
ai = aisql.AI(self.apikey)
|
except Exception as e:
|
||||||
function = ai.decide(query)
|
print('Something went wrong:')
|
||||||
print(f'Using {function}')
|
print(e)
|
||||||
try:
|
print('___')
|
||||||
if "fetchall".casefold() in function.casefold():
|
|
||||||
self.db.fetchall(query)
|
|
||||||
elif "fetchone".casefold() in function.casefold():
|
|
||||||
self.db.fetchone(query)
|
|
||||||
elif "fetchmany".casefold() in function.casefold():
|
|
||||||
size = function.split('=')[1].strip(']')
|
|
||||||
self.db.fetchmany(query, size)
|
|
||||||
elif "execute".casefold() in function.casefold():
|
|
||||||
self.db.execute(query)
|
|
||||||
elif "executemany".casefold() in function.casefold():
|
|
||||||
size = function.split('=')[1].strip(']')
|
|
||||||
self.db.executemany(query, size)
|
|
||||||
else:
|
|
||||||
print('Invalid option')
|
|
||||||
except Exception as e:
|
|
||||||
print('Something went wrong:')
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main = Main()
|
main = Main()
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@ class AI:
|
||||||
openai.api_key = self.api_key
|
openai.api_key = self.api_key
|
||||||
self.convertlog = []
|
self.convertlog = []
|
||||||
|
|
||||||
def humantosql(self, text: str, tableschema: list) -> str:
|
def humantosql(self, text: str, dbtype: str, tableschema: list) -> str:
|
||||||
self.convertlog = [{"role": "system", "content": f"You convert human language to SQL. You do not add any adidtional information. You are indirectly connected to a Database, so you can Query, Execute etc, just make the SQL statement. For better context you get the tables and columns from the database: {tableschema}"}]
|
self.convertlog = [{"role": "system", "content": f"You convert Human Language to SQL. Only answer as an SQL Statement as the output of you will be the direct input into the database. Always edit the old Statement and do not create a completly new one. You are using this Database: {dbtype} and for better context here are the tables and columns: {tableschema}"}]
|
||||||
prompt = {"role": "user", "content": text}
|
prompt = {"role": "user", "content": text}
|
||||||
self.convertlog.append(prompt)
|
self.convertlog.append(prompt)
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user