From 13e027afdf0e55e05509190d3481c593a4636d33 Mon Sep 17 00:00:00 2001 From: crennis Date: Thu, 4 May 2023 10:27:54 +0200 Subject: [PATCH] I hate GPT --- nogui/main.py | 79 ++++++++++++++++++++++-------------------- nogui/modules/aisql.py | 4 +-- 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/nogui/main.py b/nogui/main.py index 4a621c1..2543752 100644 --- a/nogui/main.py +++ b/nogui/main.py @@ -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,39 +91,31 @@ 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() + self.db = self.config() + + ai = aisql.AI(self.apikey) + function = ai.decide(query) + print(f'Using {function}') + try: + if "fetchall".casefold() in function.casefold(): + print(self.db.fetchall(query)) + 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: print('Invalid option') - elif self.db is not None: - ai = aisql.AI(self.apikey) - function = ai.decide(query) - print(f'Using {function}') - try: - 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) + print('___') + except Exception as e: + print('Something went wrong:') + print(e) + print('___') if __name__ == '__main__': main = Main() diff --git a/nogui/modules/aisql.py b/nogui/modules/aisql.py index ca45085..0442d45 100644 --- a/nogui/modules/aisql.py +++ b/nogui/modules/aisql.py @@ -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(