File size: 799 Bytes
883ac62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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