select-ai 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of select-ai might be problematic. Click here for more details.

select_ai/profile.py ADDED
@@ -0,0 +1,460 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Copyright (c) 2025, Oracle and/or its affiliates.
3
+ #
4
+ # Licensed under the Universal Permissive License v 1.0 as shown at
5
+ # http://oss.oracle.com/licenses/upl.
6
+ # -----------------------------------------------------------------------------
7
+
8
+ import json
9
+ from contextlib import contextmanager
10
+ from dataclasses import replace as dataclass_replace
11
+ from typing import Generator, Iterator, Mapping, Optional, Union
12
+
13
+ import oracledb
14
+ import pandas
15
+
16
+ from select_ai import Conversation
17
+ from select_ai.action import Action
18
+ from select_ai.base_profile import BaseProfile, ProfileAttributes
19
+ from select_ai.db import cursor
20
+ from select_ai.errors import ProfileExistsError, ProfileNotFoundError
21
+ from select_ai.provider import Provider
22
+ from select_ai.sql import (
23
+ GET_USER_AI_PROFILE,
24
+ GET_USER_AI_PROFILE_ATTRIBUTES,
25
+ LIST_USER_AI_PROFILES,
26
+ )
27
+ from select_ai.synthetic_data import SyntheticDataAttributes
28
+
29
+
30
+ class Profile(BaseProfile):
31
+ """Profile class represents an AI Profile. It defines
32
+ attributes and methods to interact with the underlying
33
+ AI Provider. All methods in this class are synchronous
34
+ or blocking
35
+ """
36
+
37
+ def __init__(self, *args, **kwargs):
38
+ super().__init__(*args, **kwargs)
39
+ self._init_profile()
40
+
41
+ def _init_profile(self) -> None:
42
+ """Initializes AI profile based on the passed attributes
43
+
44
+ :return: None
45
+ :raises: oracledb.DatabaseError
46
+ """
47
+ if self.profile_name:
48
+ profile_exists = False
49
+ try:
50
+ saved_attributes = self._get_attributes(
51
+ profile_name=self.profile_name
52
+ )
53
+ profile_exists = True
54
+ if not self.replace and not self.merge:
55
+ if (
56
+ self.attributes is not None
57
+ or self.description is not None
58
+ ):
59
+ if self.raise_error_if_exists:
60
+ raise ProfileExistsError(self.profile_name)
61
+
62
+ if self.description is None:
63
+ self.description = self._get_profile_description(
64
+ profile_name=self.profile_name
65
+ )
66
+ except ProfileNotFoundError:
67
+ if self.attributes is None and self.description is None:
68
+ raise
69
+ else:
70
+ if self.attributes is None:
71
+ self.attributes = saved_attributes
72
+ if self.merge:
73
+ self.replace = True
74
+ if self.attributes is not None:
75
+ self.attributes = dataclass_replace(
76
+ saved_attributes,
77
+ **self.attributes.dict(exclude_null=True),
78
+ )
79
+ if self.replace or not profile_exists:
80
+ self.create(replace=self.replace)
81
+ else: # profile name is None
82
+ if self.attributes is not None or self.description is not None:
83
+ raise ValueError(
84
+ "Attribute 'profile_name' cannot be empty or None"
85
+ )
86
+
87
+ @staticmethod
88
+ def _get_profile_description(profile_name) -> Union[str, None]:
89
+ """Get description of profile from USER_CLOUD_AI_PROFILES
90
+
91
+ :param str profile_name:
92
+ :return: Union[str, None] profile description
93
+ :raises: ProfileNotFoundError
94
+ """
95
+ with cursor() as cr:
96
+ cr.execute(GET_USER_AI_PROFILE, profile_name=profile_name.upper())
97
+ profile = cr.fetchone()
98
+ if profile:
99
+ if profile[1] is not None:
100
+ return profile[1].read()
101
+ else:
102
+ return None
103
+ else:
104
+ raise ProfileNotFoundError(profile_name)
105
+
106
+ @staticmethod
107
+ def _get_attributes(profile_name) -> ProfileAttributes:
108
+ """Get AI profile attributes from the Database
109
+
110
+ :param str profile_name: Name of the profile
111
+ :return: select_ai.ProfileAttributes
112
+ :raises: ProfileNotFoundError
113
+ """
114
+ with cursor() as cr:
115
+ cr.execute(
116
+ GET_USER_AI_PROFILE_ATTRIBUTES,
117
+ profile_name=profile_name.upper(),
118
+ )
119
+ attributes = cr.fetchall()
120
+ if attributes:
121
+ return ProfileAttributes.create(**dict(attributes))
122
+ else:
123
+ raise ProfileNotFoundError(profile_name=profile_name)
124
+
125
+ def get_attributes(self) -> ProfileAttributes:
126
+ """Get AI profile attributes from the Database
127
+
128
+ :return: select_ai.ProfileAttributes
129
+ """
130
+ return self._get_attributes(profile_name=self.profile_name)
131
+
132
+ def _set_attribute(
133
+ self,
134
+ attribute_name: str,
135
+ attribute_value: Union[bool, str, int, float],
136
+ ):
137
+ parameters = {
138
+ "profile_name": self.profile_name,
139
+ "attribute_name": attribute_name,
140
+ "attribute_value": attribute_value,
141
+ }
142
+ with cursor() as cr:
143
+ cr.callproc(
144
+ "DBMS_CLOUD_AI.SET_ATTRIBUTE", keyword_parameters=parameters
145
+ )
146
+
147
+ def set_attribute(
148
+ self,
149
+ attribute_name: str,
150
+ attribute_value: Union[bool, str, int, float, Provider],
151
+ ):
152
+ """Updates AI profile attribute on the Python object and also
153
+ saves it in the database
154
+
155
+ :param str attribute_name: Name of the AI profile attribute
156
+ :param Union[bool, str, int, float, Provider] attribute_value: Value of
157
+ the profile attribute
158
+ :return: None
159
+
160
+ """
161
+ self.attributes.set_attribute(attribute_name, attribute_value)
162
+ if isinstance(attribute_value, Provider):
163
+ for k, v in attribute_value.dict().items():
164
+ self._set_attribute(k, v)
165
+ else:
166
+ self._set_attribute(attribute_name, attribute_value)
167
+
168
+ def set_attributes(self, attributes: ProfileAttributes):
169
+ """Updates AI profile attributes on the Python object and also
170
+ saves it in the database
171
+
172
+ :param ProviderAttributes attributes: Object specifying AI profile
173
+ attributes
174
+ :return: None
175
+ """
176
+ if not isinstance(attributes, ProfileAttributes):
177
+ raise TypeError(
178
+ "'attributes' must be an object of type"
179
+ " select_ai.ProfileAttributes"
180
+ )
181
+ self.attributes = attributes
182
+ parameters = {
183
+ "profile_name": self.profile_name,
184
+ "attributes": self.attributes.json(),
185
+ }
186
+ with cursor() as cr:
187
+ cr.callproc(
188
+ "DBMS_CLOUD_AI.SET_ATTRIBUTES", keyword_parameters=parameters
189
+ )
190
+
191
+ def create(self, replace: Optional[int] = False) -> None:
192
+ """Create an AI Profile in the Database
193
+
194
+ :param bool replace: Set True to replace else False
195
+ :return: None
196
+ :raises: oracledb.DatabaseError
197
+ """
198
+ if self.attributes is None:
199
+ raise AttributeError("Profile attributes cannot be None")
200
+ parameters = {
201
+ "profile_name": self.profile_name,
202
+ "attributes": self.attributes.json(),
203
+ }
204
+ if self.description:
205
+ parameters["description"] = self.description
206
+
207
+ with cursor() as cr:
208
+ try:
209
+ cr.callproc(
210
+ "DBMS_CLOUD_AI.CREATE_PROFILE",
211
+ keyword_parameters=parameters,
212
+ )
213
+ except oracledb.DatabaseError as e:
214
+ (error,) = e.args
215
+ # If already exists and replace is True then drop and recreate
216
+ if error.code == 20046 and replace:
217
+ self.delete(force=True)
218
+ cr.callproc(
219
+ "DBMS_CLOUD_AI.CREATE_PROFILE",
220
+ keyword_parameters=parameters,
221
+ )
222
+ else:
223
+ raise
224
+
225
+ def delete(self, force=False) -> None:
226
+ """Deletes an AI profile from the database
227
+
228
+ :param bool force: Ignores errors if AI profile does not exist.
229
+ :return: None
230
+ :raises: oracledb.DatabaseError
231
+ """
232
+ with cursor() as cr:
233
+ cr.callproc(
234
+ "DBMS_CLOUD_AI.DROP_PROFILE",
235
+ keyword_parameters={
236
+ "profile_name": self.profile_name,
237
+ "force": force,
238
+ },
239
+ )
240
+
241
+ @classmethod
242
+ def _from_db(cls, profile_name: str) -> "Profile":
243
+ """Create a Profile object from attributes saved in the database
244
+
245
+ :param str profile_name:
246
+ :return: select_ai.Profile
247
+ :raises: ProfileNotFoundError
248
+ """
249
+ with cursor() as cr:
250
+ cr.execute(
251
+ GET_USER_AI_PROFILE_ATTRIBUTES, profile_name=profile_name
252
+ )
253
+ attributes = cr.fetchall()
254
+ if attributes:
255
+ attributes = ProfileAttributes.create(**dict(attributes))
256
+ return cls(profile_name=profile_name, attributes=attributes)
257
+ else:
258
+ raise ProfileNotFoundError(profile_name=profile_name)
259
+
260
+ @classmethod
261
+ def list(
262
+ cls, profile_name_pattern: str = ".*"
263
+ ) -> Generator["Profile", None, None]:
264
+ """List AI Profiles saved in the database.
265
+
266
+ :param str profile_name_pattern: Regular expressions can be used
267
+ to specify a pattern. Function REGEXP_LIKE is used to perform the
268
+ match. Default value is ".*" i.e. match all AI profiles.
269
+
270
+ :return: Iterator[Profile]
271
+ """
272
+ with cursor() as cr:
273
+ cr.execute(
274
+ LIST_USER_AI_PROFILES,
275
+ profile_name_pattern=profile_name_pattern,
276
+ )
277
+ for row in cr.fetchall():
278
+ profile_name = row[0]
279
+ description = row[1]
280
+ attributes = cls._get_attributes(profile_name=profile_name)
281
+ yield cls(
282
+ profile_name=profile_name,
283
+ description=description,
284
+ attributes=attributes,
285
+ raise_error_if_exists=False,
286
+ )
287
+
288
+ def generate(
289
+ self,
290
+ prompt: str,
291
+ action: Optional[Action] = Action.RUNSQL,
292
+ params: Mapping = None,
293
+ ) -> Union[pandas.DataFrame, str, None]:
294
+ """Perform AI translation using this profile
295
+
296
+ :param str prompt: Natural language prompt to translate
297
+ :param select_ai.profile.Action action:
298
+ :param params: Parameters to include in the LLM request. For e.g.
299
+ conversation_id for context-aware chats
300
+ :return: Union[pandas.DataFrame, str]
301
+ """
302
+ if not prompt:
303
+ raise ValueError("prompt cannot be empty or None")
304
+ parameters = {
305
+ "prompt": prompt,
306
+ "action": action,
307
+ "profile_name": self.profile_name,
308
+ # "attributes": self.attributes.json(),
309
+ }
310
+ if params:
311
+ parameters["params"] = json.dumps(params)
312
+ with cursor() as cr:
313
+ data = cr.callfunc(
314
+ "DBMS_CLOUD_AI.GENERATE",
315
+ oracledb.DB_TYPE_CLOB,
316
+ keyword_parameters=parameters,
317
+ )
318
+ if data is not None:
319
+ result = data.read()
320
+ else:
321
+ result = None
322
+ if action == Action.RUNSQL and result:
323
+ return pandas.DataFrame(json.loads(result))
324
+ elif action == Action.RUNSQL:
325
+ return pandas.DataFrame()
326
+ else:
327
+ return result
328
+
329
+ def chat(self, prompt: str, params: Mapping = None) -> str:
330
+ """Chat with the LLM
331
+
332
+ :param str prompt: Natural language prompt
333
+ :param params: Parameters to include in the LLM request
334
+ :return: str
335
+ """
336
+ return self.generate(prompt, action=Action.CHAT, params=params)
337
+
338
+ @contextmanager
339
+ def chat_session(self, conversation: Conversation, delete: bool = False):
340
+ """Starts a new chat session for context-aware conversations
341
+
342
+ :param Conversation conversation: Conversation object to use for this
343
+ chat session
344
+ :param bool delete: Delete conversation after session ends
345
+
346
+ :return:
347
+ """
348
+ try:
349
+ if (
350
+ conversation.conversation_id is None
351
+ and conversation.attributes is not None
352
+ ):
353
+ conversation.create()
354
+ params = {"conversation_id": conversation.conversation_id}
355
+ session = Session(profile=self, params=params)
356
+ yield session
357
+ finally:
358
+ if delete:
359
+ conversation.delete()
360
+
361
+ def narrate(self, prompt: str, params: Mapping = None) -> str:
362
+ """Narrate the result of the SQL
363
+
364
+ :param str prompt: Natural language prompt
365
+ :param params: Parameters to include in the LLM request
366
+ :return: str
367
+ """
368
+ return self.generate(prompt, action=Action.NARRATE, params=params)
369
+
370
+ def explain_sql(self, prompt: str, params: Mapping = None) -> str:
371
+ """Explain the generated SQL
372
+
373
+ :param str prompt: Natural language prompt
374
+ :param params: Parameters to include in the LLM request
375
+ :return: str
376
+ """
377
+ return self.generate(prompt, action=Action.EXPLAINSQL, params=params)
378
+
379
+ def run_sql(self, prompt: str, params: Mapping = None) -> pandas.DataFrame:
380
+ """Run the generate SQL statement and return a pandas Dataframe built
381
+ using the result set
382
+
383
+ :param str prompt: Natural language prompt
384
+ :param params: Parameters to include in the LLM request
385
+ :return: pandas.DataFrame
386
+ """
387
+ return self.generate(prompt, action=Action.RUNSQL, params=params)
388
+
389
+ def show_sql(self, prompt: str, params: Mapping = None) -> str:
390
+ """Show the generated SQL
391
+
392
+ :param str prompt: Natural language prompt
393
+ :param params: Parameters to include in the LLM request
394
+ :return: str
395
+ """
396
+ return self.generate(prompt, action=Action.SHOWSQL, params=params)
397
+
398
+ def show_prompt(self, prompt: str, params: Mapping = None) -> str:
399
+ """Show the prompt sent to LLM
400
+
401
+ :param str prompt: Natural language prompt
402
+ :param params: Parameters to include in the LLM request
403
+ :return: str
404
+ """
405
+ return self.generate(prompt, action=Action.SHOWPROMPT, params=params)
406
+
407
+ def generate_synthetic_data(
408
+ self, synthetic_data_attributes: SyntheticDataAttributes
409
+ ) -> None:
410
+ """Generate synthetic data for a single table, multiple tables or a
411
+ full schema.
412
+
413
+ :param select_ai.SyntheticDataAttributes synthetic_data_attributes:
414
+ :return: None
415
+ :raises: oracledb.DatabaseError
416
+
417
+ """
418
+ if synthetic_data_attributes is None:
419
+ raise ValueError(
420
+ "Param 'synthetic_data_attributes' cannot be None"
421
+ )
422
+
423
+ if not isinstance(synthetic_data_attributes, SyntheticDataAttributes):
424
+ raise TypeError(
425
+ "'synthetic_data_attributes' must be an object "
426
+ "of type select_ai.SyntheticDataAttributes"
427
+ )
428
+
429
+ keyword_parameters = synthetic_data_attributes.prepare()
430
+ keyword_parameters["profile_name"] = self.profile_name
431
+ with cursor() as cr:
432
+ cr.callproc(
433
+ "DBMS_CLOUD_AI.GENERATE_SYNTHETIC_DATA",
434
+ keyword_parameters=keyword_parameters,
435
+ )
436
+
437
+
438
+ class Session:
439
+ """Session lets you persist request parameters across DBMS_CLOUD_AI
440
+ requests. This is useful in context-aware conversations
441
+ """
442
+
443
+ def __init__(self, profile: Profile, params: Mapping):
444
+ """
445
+
446
+ :param profile: An AI Profile to use in this session
447
+ :param params: Parameters to be persisted across requests
448
+ """
449
+ self.params = params
450
+ self.profile = profile
451
+
452
+ def chat(self, prompt: str):
453
+ # params = {"conversation_id": self.conversation_id}
454
+ return self.profile.chat(prompt=prompt, params=self.params)
455
+
456
+ def __enter__(self):
457
+ return self
458
+
459
+ def __exit__(self, exc_type, exc_val, exc_tb):
460
+ pass