I hate GPT

This commit is contained in:
crennis 2023-05-04 10:27:54 +02:00
parent d03e7fa086
commit 13e027afdf
2 changed files with 43 additions and 40 deletions

View File

@ -1,12 +1,9 @@
import getpass
import modules.aisql as aisql
#TODO: Clear screen after every option
class Main:
def __init__(self):
self.tableschema = None
self.tableschema = {}
self.dbtype = 'postgresql'
self.ip = 'localhost'
self.port = '32768'
@ -17,6 +14,7 @@ class Main:
self.apikey = open('apikey', 'r').read().strip('\n')
def config(self) -> object:
print('Configure the Database')
print(f'Database type')
print("postgresql - PostgreSQL")
print("mysql - MySQL (WIP)")
@ -27,6 +25,7 @@ class Main:
self.username = input(f'Username (current: {self.username}): ') or self.username
self.password = getpass.getpass(f'Password (current: {self.password}): ') or self.password
self.database = input(f'Database (current: {self.database}): ') or self.database
print('___')
if self.dbtype == 'postgresql':
# TODO: Add more Database languages
@ -42,15 +41,22 @@ class Main:
print('4. Get Tables Schema')
print('5. Exit')
option = input('Option: ')
print('___')
if option == '1':
self.generate_sql()
elif option == '2':
query = input('Query: ')
self.run_sql(query)
elif option == '3':
self.db, self.tableschema = self.config()
self.db = self.config()
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':
exit()
else:
@ -61,13 +67,18 @@ class Main:
generate = True
while generate:
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('Would you like to run it?')
print('1. Yes')
print('2. No')
print('3. Edit')
choice = input('Choice: ')
print('___')
if choice == '1':
self.run_sql(sql)
generate = False
@ -80,29 +91,19 @@ class Main:
def run_sql(self, query):
if self.db is None:
print('You have to configure the database first')
print('1. Configure')
print('2. Exit')
choice = input('Choice: ')
if choice == '1':
self.db, self.tableschema = self.config()
self.run_sql(query)
elif choice == '2':
exit()
else:
print('Invalid option')
elif self.db is not None:
self.db = self.config()
ai = aisql.AI(self.apikey)
function = ai.decide(query)
print(f'Using {function}')
try:
if "fetchall".casefold() in function.casefold():
self.db.fetchall(query)
print(self.db.fetchall(query))
elif "fetchone".casefold() in function.casefold():
self.db.fetchone(query)
print(self.db.fetchone(query))
elif "fetchmany".casefold() in function.casefold():
size = function.split('=')[1].strip(']')
self.db.fetchmany(query, size)
print(self.db.fetchmany(query, size))
elif "execute".casefold() in function.casefold():
self.db.execute(query)
elif "executemany".casefold() in function.casefold():
@ -110,9 +111,11 @@ class Main:
self.db.executemany(query, size)
else:
print('Invalid option')
print('___')
except Exception as e:
print('Something went wrong:')
print(e)
print('___')
if __name__ == '__main__':
main = Main()

View File

@ -7,8 +7,8 @@ class AI:
openai.api_key = self.api_key
self.convertlog = []
def humantosql(self, text: 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}"}]
def humantosql(self, text: str, dbtype: str, tableschema: list) -> str:
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}
self.convertlog.append(prompt)
response = openai.ChatCompletion.create(