-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmindsdb.py
More file actions
90 lines (73 loc) · 2.38 KB
/
Copy pathmindsdb.py
File metadata and controls
90 lines (73 loc) · 2.38 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from string import Template
import mindsdb_sdk
from mindsdb_sdk.server import Databases, Server
from pandas import DataFrame
from requests.exceptions import ConnectionError, HTTPError
MINDSDB_HOST = "https://cloud.mindsdb.com"
SQL_ASK_QUERY = Template(
"""
SELECT response
FROM mindsdb.gpt_model
WHERE text = "$text";
"""
)
def to_data(dataframe: DataFrame) -> str:
"""
takes a pandas `DataFrame` and returns the first value from the first column
Args:
dataframe: the dataframe returned from MindsDB that stores the answer in the first cell of column
Returns:
first value of the first column of dataframe that MindsDB responses
"""
return dataframe.iloc[:, 0].values[0]
class MindsDB:
"""
MindsDB manager class
"""
def __init__(self, email: str, password: str) -> None:
"""
initializer class.
Args:
email: MindsDB account email address (that is stored as an env var)
password: MindsDB account password
"""
self.email = email
self.password = password
self.is_authenticated: bool = False
self.database: Databases
def authenticate(self) -> None:
"""
authorizes the email and password with MindsDB's host
"""
try:
server = mindsdb_sdk.connect(
MINDSDB_HOST,
login=self.email,
password=self.password,
)
except HTTPError:
raise Exception(
"Email or password is incorrect. Make sure to enter the right credentials."
)
except ConnectionError:
raise Exception("Make sure you have access to the internet and try again.")
self.is_authenticated = True
self.database = self.collect_database(server)
@staticmethod
def collect_database(server: Server) -> Databases:
return server.list_databases()[0]
def answer(self, text: str) -> str:
"""
takes the question and queries then converts the response into `rich.Markdown`
Args:
question: the value from `ask` positional argument
Returns:
response from MindsDB in Markdown format
"""
return to_data(
self.database.query(
SQL_ASK_QUERY.substitute(
text=text,
)
).fetch()
)