import pymysql import os def connect_to_db(): try: connection = pymysql.connect( host="cloud.mindsdb.com", user=os.environ['LOGIN'], password=os.environ['PASSWORD'], port=3306, db="mindsdb", charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor) return connection except Exception as e: print(f"Error connecting to database: {e}") return None print('---Trying to connect---') connection = connect_to_db() print('---CONNECTED---') def ask_gpt(text): # print('ASK GPT:', text) text = text.replace("'", '"') query = f"SELECT response FROM mindsdb.gpt4 WHERE text=%s" cursor = connection.cursor() cursor.execute(query, (text, )) result = cursor.fetchone() response = result['response'] return response