-
Notifications
You must be signed in to change notification settings - Fork 189
/
sql.py
265 lines (224 loc) · 8.79 KB
/
sql.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import json
import logging
from typing import Dict, List, Optional, Union
from pydantic import Field
from typing_extensions import Literal
from llmstack.base.models import Profile
from llmstack.common.blocks.base.schema import BaseSchema as _Schema
from llmstack.common.blocks.data.store.database.constants import DatabaseEngineType
from llmstack.common.blocks.data.store.database.database_reader import (
DatabaseReader,
DatabaseReaderInput,
)
from llmstack.common.blocks.data.store.database.utils import (
get_database_configuration_class,
)
from llmstack.common.blocks.data.store.vectorstore import Document
from llmstack.common.utils.models import Config
from llmstack.connections.models import ConnectionType
from llmstack.datasources.handlers.datasource_processor import (
DataSourceEntryItem,
DataSourceProcessor,
DataSourceSchema,
)
from llmstack.datasources.models import DataSource
logger = logging.getLogger(__name__)
class PostgreSQLConnection(_Schema):
engine: Literal[DatabaseEngineType.POSTGRESQL] = DatabaseEngineType.POSTGRESQL
host: str = Field(description="Host of the PostgreSQL instance")
port: int = Field(
description="Port number to connect to the PostgreSQL instance",
)
database_name: str = Field(description="PostgreSQL database name")
class Config:
title = "PostgreSQL"
class MySQLConnection(_Schema):
engine: Literal[DatabaseEngineType.MYSQL] = DatabaseEngineType.MYSQL
host: str = Field(description="Host of the MySQL instance")
port: int = Field(
description="Port number to connect to the MySQL instance",
)
database_name: str = Field(description="MySQL database name")
class Config:
title = "MySQL"
class SQLiteConnection(_Schema):
engine: Literal[DatabaseEngineType.SQLITE] = DatabaseEngineType.SQLITE
database_path: str = Field(description="SQLite database file path")
class Config:
title = "SQLite"
SQLConnection = Union[PostgreSQLConnection, MySQLConnection, SQLiteConnection]
class SQLDatabaseSchema(DataSourceSchema):
connection: Optional[SQLConnection] = Field(
title="Database",
# description="Database details",
)
connection_id: Optional[str] = Field(
widget="connection",
advanced_parameter=False,
description="Use your authenticated connection to the database",
# Filters is a list of strings, each formed by the combination of the connection attributes 'base_connection_type', 'provider_slug', and 'connection_type_slug', separated by '/'.
# The pattern followed is: base_connection_type/provider_slug/connection_type_slug. We may skip provider_slug or connection_type_slug if they are not present in the filter string.
filters=[ConnectionType.CREDENTIALS + "/basic_authentication"],
)
class SQLConnectionConfiguration(Config):
config_type: Optional[str] = "sql_connection"
is_encrypted = True
config: Optional[Dict]
class SQLDataSource(DataSourceProcessor[SQLDatabaseSchema]):
# Initializer for the class.
# It requires a datasource object as input, checks if it has a 'data'
# configuration, and sets up Weaviate Database Configuration.
def __init__(self, datasource: DataSource):
self.datasource = datasource
self.profile = Profile.objects.get(user=self.datasource.owner)
self._env = self.profile.get_vendor_env()
if self.datasource.config and "data" in self.datasource.config:
config_dict = SQLConnectionConfiguration().from_dict(
self.datasource.config,
self.datasource.profile.decrypt_value,
)
self._configuration = SQLDatabaseSchema(
**config_dict["config"],
)
database_configuration_class = get_database_configuration_class(self._configuration.connection.engine)
if self._configuration.connection.engine == DatabaseEngineType.SQLITE:
self._reader_configuration = database_configuration_class(
engine=self._configuration.connection.engine,
dbpath=self._configuration.connection.database_path,
)
else:
username = password = None
connection = (
self._env["connections"].get(
self._configuration.connection_id,
None,
)
if self._configuration.connection_id
else None
)
if connection and connection["base_connection_type"] == ConnectionType.CREDENTIALS:
username = connection["configuration"]["username"]
password = connection["configuration"]["password"]
self._reader_configuration = database_configuration_class(
engine=self._configuration.connection.engine,
user=username,
password=password,
host=self._configuration.connection.host,
port=self._configuration.connection.port,
dbname=self._configuration.connection.database_name,
use_ssl=False,
)
self._source_name = self.datasource.name
@staticmethod
def name() -> str:
return "SQL"
@staticmethod
def slug() -> str:
return "sql"
@staticmethod
def description() -> str:
return "Connect to a SQL Database"
# This static method takes a dictionary for configuration and a DataSource object as inputs.
# Validation of these inputs is performed and a dictionary containing the
# Database Connection Configuration is returned.
@staticmethod
def process_validate_config(
config_data: dict,
datasource: DataSource,
) -> dict:
return SQLConnectionConfiguration(
config=config_data,
).to_dict(
encrypt_fn=datasource.profile.encrypt_value,
)
@staticmethod
def provider_slug() -> str:
return "promptly"
def validate_and_process(self, data: dict) -> List[DataSourceEntryItem]:
raise NotImplementedError
def get_data_documents(self, data: dict) -> List[Document]:
raise NotImplementedError
def add_entry(self, data: dict) -> Optional[DataSourceEntryItem]:
raise NotImplementedError
def similarity_search(self, query: str, **kwargs) -> List[dict]:
client = DatabaseReader()
result = (
client.process(
DatabaseReaderInput(
sql=query,
),
configuration=self._reader_configuration,
)
.documents[0]
.content_text
)
json_result = json.loads(result)
# JSON to csv
csv_result = ""
for row in json_result["rows"]:
csv_result += (
",".join(
list(
map(
lambda entry: str(entry),
row.values(),
),
),
)
+ "\n"
)
return [
Document(
page_content_key="content",
page_content=csv_result,
metadata={
"score": 0,
"source": self._source_name,
},
),
]
def hybrid_search(self, query: str, **kwargs) -> List[dict]:
client = DatabaseReader()
result = (
client.process(
DatabaseReaderInput(
sql=query,
),
configuration=self._reader_configuration,
)
.documents[0]
.content_text
)
json_result = json.loads(result)
# JSON to csv
csv_result = ""
for row in json_result["rows"]:
csv_result += (
",".join(
list(
map(
lambda entry: str(entry),
row.values(),
),
),
)
+ "\n"
)
return [
Document(
page_content_key="content",
page_content=csv_result,
metadata={
"score": 0,
"source": self._source_name,
},
),
]
def delete_entry(self, data: dict) -> None:
raise NotImplementedError
def resync_entry(self, data: dict) -> Optional[DataSourceEntryItem]:
raise NotImplementedError
def delete_all_entries(self) -> None:
raise NotImplementedError
def get_entry_text(self, data: dict) -> str:
return None, self._configuration.json()