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/__init__.py +59 -0
- select_ai/_abc.py +77 -0
- select_ai/_enums.py +14 -0
- select_ai/_validations.py +123 -0
- select_ai/action.py +21 -0
- select_ai/async_profile.py +534 -0
- select_ai/base_profile.py +184 -0
- select_ai/conversation.py +274 -0
- select_ai/credential.py +135 -0
- select_ai/db.py +186 -0
- select_ai/errors.py +73 -0
- select_ai/profile.py +460 -0
- select_ai/provider.py +293 -0
- select_ai/sql.py +105 -0
- select_ai/synthetic_data.py +90 -0
- select_ai/vector_index.py +562 -0
- select_ai/version.py +8 -0
- select_ai-1.0.0.dist-info/METADATA +125 -0
- select_ai-1.0.0.dist-info/RECORD +22 -0
- select_ai-1.0.0.dist-info/WHEEL +5 -0
- select_ai-1.0.0.dist-info/licenses/LICENSE.txt +35 -0
- select_ai-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,562 @@
|
|
|
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 abc import ABC
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import AsyncGenerator, Iterator, Optional, Union
|
|
12
|
+
|
|
13
|
+
import oracledb
|
|
14
|
+
|
|
15
|
+
from select_ai import BaseProfile
|
|
16
|
+
from select_ai._abc import SelectAIDataClass
|
|
17
|
+
from select_ai._enums import StrEnum
|
|
18
|
+
from select_ai.async_profile import AsyncProfile
|
|
19
|
+
from select_ai.db import async_cursor, cursor
|
|
20
|
+
from select_ai.errors import VectorIndexNotFoundError
|
|
21
|
+
from select_ai.profile import Profile
|
|
22
|
+
from select_ai.sql import (
|
|
23
|
+
GET_USER_VECTOR_INDEX_ATTRIBUTES,
|
|
24
|
+
LIST_USER_VECTOR_INDEXES,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class VectorDBProvider(StrEnum):
|
|
29
|
+
ORACLE = "oracle"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class VectorDistanceMetric(StrEnum):
|
|
33
|
+
EUCLIDEAN = "EUCLIDEAN"
|
|
34
|
+
L2_SQUARED = "L2_SQUARED"
|
|
35
|
+
COSINE = "COSINE"
|
|
36
|
+
DOT = "DOT"
|
|
37
|
+
MANHATTAN = "MANHATTAN"
|
|
38
|
+
HAMMING = "HAMMING"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class VectorIndexAttributes(SelectAIDataClass):
|
|
43
|
+
"""
|
|
44
|
+
Attributes of a vector index help to manage and configure the behavior of
|
|
45
|
+
the vector index.
|
|
46
|
+
|
|
47
|
+
:param int chunk_size: Text size of chunking the input data.
|
|
48
|
+
:param int chunk_overlap: Specifies the amount of overlapping
|
|
49
|
+
characters between adjacent chunks of text.
|
|
50
|
+
:param str location: Location of the object store.
|
|
51
|
+
:param int match_limit: Specifies the maximum number of results to return
|
|
52
|
+
in a vector search query
|
|
53
|
+
:param str object_storage_credential_name: Name of the credentials for
|
|
54
|
+
accessing object storage.
|
|
55
|
+
:param str profile_name: Name of the AI profile which is used for
|
|
56
|
+
embedding source data and user prompts.
|
|
57
|
+
:param int refresh_rate: Interval of updating data in the vector store.
|
|
58
|
+
The unit is minutes.
|
|
59
|
+
:param float similarity_threshold: Defines the minimum level of similarity
|
|
60
|
+
required for two items to be considered a match
|
|
61
|
+
:param VectorDistanceMetric vector_distance_metric: Specifies the type of
|
|
62
|
+
distance calculation used to compare vectors in a database
|
|
63
|
+
:param VectorDBProvider vector_db_provider: Name of the Vector database
|
|
64
|
+
provider. Default value is "oracle"
|
|
65
|
+
:param str vector_db_endpoint: Endpoint to access the Vector database
|
|
66
|
+
:param str vector_db_credential_name: Name of the credentials for accessing
|
|
67
|
+
Vector database
|
|
68
|
+
:param int vector_dimension: Specifies the number of elements in each
|
|
69
|
+
vector within the vector store
|
|
70
|
+
:param str vector_table_name: Specifies the name of the table or collection
|
|
71
|
+
to store vector embeddings and chunked data
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
chunk_size: Optional[int] = 1024
|
|
75
|
+
chunk_overlap: Optional[int] = 128
|
|
76
|
+
location: Optional[str] = None
|
|
77
|
+
match_limit: Optional[int] = 5
|
|
78
|
+
object_storage_credential_name: Optional[str] = None
|
|
79
|
+
profile_name: Optional[str] = None
|
|
80
|
+
refresh_rate: Optional[int] = 1440
|
|
81
|
+
similarity_threshold: Optional[float] = 0
|
|
82
|
+
vector_distance_metric: Optional[VectorDistanceMetric] = (
|
|
83
|
+
VectorDistanceMetric.COSINE
|
|
84
|
+
)
|
|
85
|
+
vector_db_endpoint: Optional[str] = None
|
|
86
|
+
vector_db_credential_name: Optional[str] = None
|
|
87
|
+
vector_db_provider: Optional[VectorDBProvider] = None
|
|
88
|
+
vector_dimension: Optional[int] = None
|
|
89
|
+
vector_table_name: Optional[str] = None
|
|
90
|
+
pipeline_name: Optional[str] = None
|
|
91
|
+
|
|
92
|
+
def json(self, exclude_null=True):
|
|
93
|
+
attributes = self.dict(exclude_null=exclude_null)
|
|
94
|
+
attributes.pop("pipeline_name", None)
|
|
95
|
+
return json.dumps(attributes)
|
|
96
|
+
|
|
97
|
+
@classmethod
|
|
98
|
+
def create(cls, *, vector_db_provider: Optional[str] = None, **kwargs):
|
|
99
|
+
for subclass in cls.__subclasses__():
|
|
100
|
+
if subclass.vector_db_provider == vector_db_provider:
|
|
101
|
+
return subclass(**kwargs)
|
|
102
|
+
return cls(**kwargs)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass
|
|
106
|
+
class OracleVectorIndexAttributes(VectorIndexAttributes):
|
|
107
|
+
"""Oracle specific vector index attributes"""
|
|
108
|
+
|
|
109
|
+
vector_db_provider: Optional[VectorDBProvider] = VectorDBProvider.ORACLE
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class _BaseVectorIndex(ABC):
|
|
113
|
+
|
|
114
|
+
def __init__(
|
|
115
|
+
self,
|
|
116
|
+
profile: BaseProfile = None,
|
|
117
|
+
index_name: Optional[str] = None,
|
|
118
|
+
description: Optional[str] = None,
|
|
119
|
+
attributes: Optional[VectorIndexAttributes] = None,
|
|
120
|
+
):
|
|
121
|
+
"""Initialize a Vector Index"""
|
|
122
|
+
if attributes and not isinstance(attributes, VectorIndexAttributes):
|
|
123
|
+
raise TypeError(
|
|
124
|
+
"'attributes' must be an object of type "
|
|
125
|
+
"select_ai.VectorIndexAttributes"
|
|
126
|
+
)
|
|
127
|
+
if profile and not isinstance(profile, BaseProfile):
|
|
128
|
+
raise TypeError(
|
|
129
|
+
"'profile' must be an object of type "
|
|
130
|
+
"select_ai.Profile or select_ai.AsyncProfile"
|
|
131
|
+
)
|
|
132
|
+
self.profile = profile
|
|
133
|
+
self.index_name = index_name
|
|
134
|
+
self.attributes = attributes
|
|
135
|
+
self.description = description
|
|
136
|
+
|
|
137
|
+
def __repr__(self):
|
|
138
|
+
return (
|
|
139
|
+
f"{self.__class__.__name__}(profile={self.profile}, "
|
|
140
|
+
f"index_name={self.index_name}, "
|
|
141
|
+
f"attributes={self.attributes}, description={self.description})"
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class VectorIndex(_BaseVectorIndex):
|
|
146
|
+
"""
|
|
147
|
+
VectorIndex objects let you manage vector indexes
|
|
148
|
+
|
|
149
|
+
:param str index_name: The name of the vector index
|
|
150
|
+
:param str description: The description of the vector index
|
|
151
|
+
:param select_ai.VectorIndexAttributes attributes: The attributes of the vector index
|
|
152
|
+
"""
|
|
153
|
+
|
|
154
|
+
@staticmethod
|
|
155
|
+
def _get_attributes(index_name: str) -> VectorIndexAttributes:
|
|
156
|
+
"""Get attributes of a vector index
|
|
157
|
+
|
|
158
|
+
:return: select_ai.VectorIndexAttributes
|
|
159
|
+
:raises: VectorIndexNotFoundError
|
|
160
|
+
"""
|
|
161
|
+
with cursor() as cr:
|
|
162
|
+
cr.execute(GET_USER_VECTOR_INDEX_ATTRIBUTES, index_name=index_name)
|
|
163
|
+
attributes = cr.fetchall()
|
|
164
|
+
if attributes:
|
|
165
|
+
post_processed_attributes = {}
|
|
166
|
+
for k, v in attributes:
|
|
167
|
+
if isinstance(v, oracledb.LOB):
|
|
168
|
+
post_processed_attributes[k] = v.read()
|
|
169
|
+
else:
|
|
170
|
+
post_processed_attributes[k] = v
|
|
171
|
+
return VectorIndexAttributes.create(
|
|
172
|
+
**post_processed_attributes
|
|
173
|
+
)
|
|
174
|
+
else:
|
|
175
|
+
raise VectorIndexNotFoundError(index_name=index_name)
|
|
176
|
+
|
|
177
|
+
def create(self, replace: Optional[bool] = False):
|
|
178
|
+
"""Create a vector index in the database and populates the index
|
|
179
|
+
with data from an object store bucket using an async scheduler job
|
|
180
|
+
|
|
181
|
+
:param bool replace: Replace vector index if it exists
|
|
182
|
+
:return: None
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
if self.attributes.profile_name is None:
|
|
186
|
+
self.attributes.profile_name = self.profile.profile_name
|
|
187
|
+
|
|
188
|
+
parameters = {
|
|
189
|
+
"index_name": self.index_name,
|
|
190
|
+
"attributes": self.attributes.json(),
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if self.description:
|
|
194
|
+
parameters["description"] = self.description
|
|
195
|
+
|
|
196
|
+
with cursor() as cr:
|
|
197
|
+
try:
|
|
198
|
+
cr.callproc(
|
|
199
|
+
"DBMS_CLOUD_AI.CREATE_VECTOR_INDEX",
|
|
200
|
+
keyword_parameters=parameters,
|
|
201
|
+
)
|
|
202
|
+
except oracledb.DatabaseError as e:
|
|
203
|
+
(error,) = e.args
|
|
204
|
+
# If already exists and replace is True then drop and recreate
|
|
205
|
+
if error.code == 20048 and replace:
|
|
206
|
+
self.delete(force=True)
|
|
207
|
+
cr.callproc(
|
|
208
|
+
"DBMS_CLOUD_AI.CREATE_VECTOR_INDEX",
|
|
209
|
+
keyword_parameters=parameters,
|
|
210
|
+
)
|
|
211
|
+
else:
|
|
212
|
+
raise
|
|
213
|
+
self.profile.set_attribute("vector_index_name", self.index_name)
|
|
214
|
+
|
|
215
|
+
def delete(
|
|
216
|
+
self,
|
|
217
|
+
include_data: Optional[bool] = True,
|
|
218
|
+
force: Optional[bool] = False,
|
|
219
|
+
):
|
|
220
|
+
"""This procedure removes a vector store index
|
|
221
|
+
|
|
222
|
+
:param bool include_data: Indicates whether to delete
|
|
223
|
+
both the customer's vector store and vector index
|
|
224
|
+
along with the vector index object
|
|
225
|
+
:param bool force: Indicates whether to ignore errors
|
|
226
|
+
that occur if the vector index does not exist
|
|
227
|
+
:return: None
|
|
228
|
+
:raises: oracledb.DatabaseError
|
|
229
|
+
"""
|
|
230
|
+
with cursor() as cr:
|
|
231
|
+
cr.callproc(
|
|
232
|
+
"DBMS_CLOUD_AI.DROP_VECTOR_INDEX",
|
|
233
|
+
keyword_parameters={
|
|
234
|
+
"index_name": self.index_name,
|
|
235
|
+
"include_data": include_data,
|
|
236
|
+
"force": force,
|
|
237
|
+
},
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
def enable(self):
|
|
241
|
+
"""This procedure enables or activates a previously disabled vector
|
|
242
|
+
index object. Generally, when you create a vector index, by default
|
|
243
|
+
it is enabled such that the AI profile can use it to perform indexing
|
|
244
|
+
and searching.
|
|
245
|
+
|
|
246
|
+
:return: None
|
|
247
|
+
:raises: oracledb.DatabaseError
|
|
248
|
+
|
|
249
|
+
"""
|
|
250
|
+
with cursor() as cr:
|
|
251
|
+
cr.callproc(
|
|
252
|
+
"DBMS_CLOUD_AI.ENABLE_VECTOR_INDEX",
|
|
253
|
+
keyword_parameters={"index_name": self.index_name},
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def disable(self):
|
|
257
|
+
"""This procedure disables a vector index object in the current
|
|
258
|
+
database. When disabled, an AI profile cannot use the vector index,
|
|
259
|
+
and the system does not load data into the vector store as new data
|
|
260
|
+
is added to the object store and does not perform indexing, searching
|
|
261
|
+
or querying based on the index.
|
|
262
|
+
|
|
263
|
+
:return: None
|
|
264
|
+
:raises: oracledb.DatabaseError
|
|
265
|
+
"""
|
|
266
|
+
with cursor() as cr:
|
|
267
|
+
cr.callproc(
|
|
268
|
+
"DBMS_CLOUD_AI.DISABLE_VECTOR_INDEX",
|
|
269
|
+
keyword_parameters={"index_name": self.index_name},
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
def set_attributes(
|
|
273
|
+
self,
|
|
274
|
+
attribute_name: str,
|
|
275
|
+
attribute_value: Union[str, int, float],
|
|
276
|
+
attributes: VectorIndexAttributes = None,
|
|
277
|
+
):
|
|
278
|
+
"""
|
|
279
|
+
This procedure updates an existing vector store index with a specified
|
|
280
|
+
value of the vector index attribute. You can specify a single attribute
|
|
281
|
+
or multiple attributes by passing an object of type
|
|
282
|
+
:class `VectorIndexAttributes`
|
|
283
|
+
|
|
284
|
+
:param str attribute_name: Custom attribute name
|
|
285
|
+
:param Union[str, int, float] attribute_value: Attribute Value
|
|
286
|
+
:param VectorIndexAttributes attributes: Specify multiple attributes
|
|
287
|
+
to update in a single API invocation
|
|
288
|
+
:return: None
|
|
289
|
+
:raises: oracledb.DatabaseError
|
|
290
|
+
"""
|
|
291
|
+
if attribute_name and attribute_value and attributes:
|
|
292
|
+
raise ValueError(
|
|
293
|
+
"Either specify a single attribute using "
|
|
294
|
+
"attribute_name and attribute_value or "
|
|
295
|
+
"pass an object of type VectorIndexAttributes"
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
parameters = {"index_name": self.index_name}
|
|
299
|
+
if attributes:
|
|
300
|
+
parameters["attributes"] = attributes.json()
|
|
301
|
+
self.attributes = attributes
|
|
302
|
+
else:
|
|
303
|
+
setattr(self.attributes, attribute_name, attribute_value)
|
|
304
|
+
parameters["attributes_name"] = attribute_name
|
|
305
|
+
parameters["attributes_value"] = attribute_value
|
|
306
|
+
|
|
307
|
+
with cursor() as cr:
|
|
308
|
+
cr.callproc(
|
|
309
|
+
"DBMS_CLOUD_AI.UPDATE_VECTOR_INDEX",
|
|
310
|
+
keyword_parameters=parameters,
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
def get_attributes(self) -> VectorIndexAttributes:
|
|
314
|
+
"""Get attributes of this vector index
|
|
315
|
+
|
|
316
|
+
:return: select_ai.VectorIndexAttributes
|
|
317
|
+
:raises: VectorIndexNotFoundError
|
|
318
|
+
"""
|
|
319
|
+
return self._get_attributes(self.index_name)
|
|
320
|
+
|
|
321
|
+
@classmethod
|
|
322
|
+
def list(cls, index_name_pattern: str = ".*") -> Iterator["VectorIndex"]:
|
|
323
|
+
"""List Vector Indexes
|
|
324
|
+
|
|
325
|
+
:param str index_name_pattern: Regular expressions can be used
|
|
326
|
+
to specify a pattern. Function REGEXP_LIKE is used to perform the
|
|
327
|
+
match. Default value is ".*" i.e. match all vector indexes.
|
|
328
|
+
|
|
329
|
+
:return: Iterator[VectorIndex]
|
|
330
|
+
"""
|
|
331
|
+
with cursor() as cr:
|
|
332
|
+
cr.execute(
|
|
333
|
+
LIST_USER_VECTOR_INDEXES,
|
|
334
|
+
index_name_pattern=index_name_pattern,
|
|
335
|
+
)
|
|
336
|
+
for row in cr.fetchall():
|
|
337
|
+
index_name = row[0]
|
|
338
|
+
if row[1]:
|
|
339
|
+
description = row[1].read() # Oracle.LOB
|
|
340
|
+
else:
|
|
341
|
+
description = None
|
|
342
|
+
attributes = cls._get_attributes(index_name=index_name)
|
|
343
|
+
yield cls(
|
|
344
|
+
index_name=index_name,
|
|
345
|
+
description=description,
|
|
346
|
+
attributes=attributes,
|
|
347
|
+
profile=Profile(profile_name=attributes.profile_name),
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
class AsyncVectorIndex(_BaseVectorIndex):
|
|
352
|
+
"""
|
|
353
|
+
AsyncVectorIndex objects let you manage vector indexes
|
|
354
|
+
using async APIs. Use this for non-blocking concurrent
|
|
355
|
+
requests
|
|
356
|
+
|
|
357
|
+
:param str index_name: The name of the vector index
|
|
358
|
+
:param str description: The description of the vector index
|
|
359
|
+
:param VectorIndexAttributes attributes: The attributes of the vector index
|
|
360
|
+
"""
|
|
361
|
+
|
|
362
|
+
@staticmethod
|
|
363
|
+
async def _get_attributes(index_name: str) -> VectorIndexAttributes:
|
|
364
|
+
"""Get attributes of a vector index
|
|
365
|
+
|
|
366
|
+
:return: select_ai.VectorIndexAttributes
|
|
367
|
+
:raises: VectorIndexNotFoundError
|
|
368
|
+
"""
|
|
369
|
+
async with async_cursor() as cr:
|
|
370
|
+
await cr.execute(
|
|
371
|
+
GET_USER_VECTOR_INDEX_ATTRIBUTES, index_name=index_name
|
|
372
|
+
)
|
|
373
|
+
attributes = await cr.fetchall()
|
|
374
|
+
if attributes:
|
|
375
|
+
post_processed_attributes = {}
|
|
376
|
+
for k, v in attributes:
|
|
377
|
+
if isinstance(v, oracledb.AsyncLOB):
|
|
378
|
+
post_processed_attributes[k] = await v.read()
|
|
379
|
+
else:
|
|
380
|
+
post_processed_attributes[k] = v
|
|
381
|
+
return VectorIndexAttributes.create(
|
|
382
|
+
**post_processed_attributes
|
|
383
|
+
)
|
|
384
|
+
else:
|
|
385
|
+
raise VectorIndexNotFoundError(index_name=index_name)
|
|
386
|
+
|
|
387
|
+
async def create(self, replace: Optional[bool] = False) -> None:
|
|
388
|
+
"""Create a vector index in the database and populates it with data
|
|
389
|
+
from an object store bucket using an async scheduler job
|
|
390
|
+
|
|
391
|
+
:param bool replace: True to replace existing vector index
|
|
392
|
+
|
|
393
|
+
"""
|
|
394
|
+
|
|
395
|
+
if self.attributes.profile_name is None:
|
|
396
|
+
self.attributes.profile_name = self.profile.profile_name
|
|
397
|
+
parameters = {
|
|
398
|
+
"index_name": self.index_name,
|
|
399
|
+
"attributes": self.attributes.json(),
|
|
400
|
+
}
|
|
401
|
+
if self.description:
|
|
402
|
+
parameters["description"] = self.description
|
|
403
|
+
async with async_cursor() as cr:
|
|
404
|
+
try:
|
|
405
|
+
await cr.callproc(
|
|
406
|
+
"DBMS_CLOUD_AI.CREATE_VECTOR_INDEX",
|
|
407
|
+
keyword_parameters=parameters,
|
|
408
|
+
)
|
|
409
|
+
except oracledb.DatabaseError as e:
|
|
410
|
+
(error,) = e.args
|
|
411
|
+
# If already exists and replace is True then drop and recreate
|
|
412
|
+
if error.code == 20048 and replace:
|
|
413
|
+
await self.delete(force=True)
|
|
414
|
+
await cr.callproc(
|
|
415
|
+
"DBMS_CLOUD_AI.CREATE_VECTOR_INDEX",
|
|
416
|
+
keyword_parameters=parameters,
|
|
417
|
+
)
|
|
418
|
+
else:
|
|
419
|
+
raise
|
|
420
|
+
|
|
421
|
+
await self.profile.set_attribute("vector_index_name", self.index_name)
|
|
422
|
+
|
|
423
|
+
async def delete(
|
|
424
|
+
self,
|
|
425
|
+
include_data: Optional[bool] = True,
|
|
426
|
+
force: Optional[bool] = False,
|
|
427
|
+
) -> None:
|
|
428
|
+
"""This procedure removes a vector store index.
|
|
429
|
+
|
|
430
|
+
:param bool include_data: Indicates whether to delete
|
|
431
|
+
both the customer's vector store and vector index
|
|
432
|
+
along with the vector index object.
|
|
433
|
+
:param bool force: Indicates whether to ignore errors
|
|
434
|
+
that occur if the vector index does not exist.
|
|
435
|
+
:return: None
|
|
436
|
+
:raises: oracledb.DatabaseError
|
|
437
|
+
|
|
438
|
+
"""
|
|
439
|
+
async with async_cursor() as cr:
|
|
440
|
+
await cr.callproc(
|
|
441
|
+
"DBMS_CLOUD_AI.DROP_VECTOR_INDEX",
|
|
442
|
+
keyword_parameters={
|
|
443
|
+
"index_name": self.index_name,
|
|
444
|
+
"include_data": include_data,
|
|
445
|
+
"force": force,
|
|
446
|
+
},
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
async def enable(self) -> None:
|
|
450
|
+
"""This procedure enables or activates a previously disabled vector
|
|
451
|
+
index object. Generally, when you create a vector index, by default
|
|
452
|
+
it is enabled such that the AI profile can use it to perform indexing
|
|
453
|
+
and searching.
|
|
454
|
+
|
|
455
|
+
:return: None
|
|
456
|
+
:raises: oracledb.DatabaseError
|
|
457
|
+
|
|
458
|
+
"""
|
|
459
|
+
async with async_cursor() as cr:
|
|
460
|
+
await cr.callproc(
|
|
461
|
+
"DBMS_CLOUD_AI.ENABLE_VECTOR_INDEX",
|
|
462
|
+
keyword_parameters={"index_name": self.index_name},
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
async def disable(self) -> None:
|
|
466
|
+
"""This procedure disables a vector index object in the current
|
|
467
|
+
database. When disabled, an AI profile cannot use the vector index,
|
|
468
|
+
and the system does not load data into the vector store as new data
|
|
469
|
+
is added to the object store and does not perform indexing, searching
|
|
470
|
+
or querying based on the index.
|
|
471
|
+
|
|
472
|
+
:return: None
|
|
473
|
+
:raises: oracledb.DatabaseError
|
|
474
|
+
"""
|
|
475
|
+
async with async_cursor() as cr:
|
|
476
|
+
await cr.callproc(
|
|
477
|
+
"DBMS_CLOUD_AI.DISABLE_VECTOR_INDEX",
|
|
478
|
+
keyword_parameters={"index_name": self.index_name},
|
|
479
|
+
)
|
|
480
|
+
|
|
481
|
+
async def set_attributes(
|
|
482
|
+
self,
|
|
483
|
+
attribute_name: str,
|
|
484
|
+
attribute_value: Union[str, int],
|
|
485
|
+
attributes: VectorIndexAttributes = None,
|
|
486
|
+
) -> None:
|
|
487
|
+
"""
|
|
488
|
+
This procedure updates an existing vector store index with a specified
|
|
489
|
+
value of the vector index attribute. You can specify a single attribute
|
|
490
|
+
or multiple attributes by passing an object of type
|
|
491
|
+
:class `VectorIndexAttributes`
|
|
492
|
+
|
|
493
|
+
:param str attribute_name: Custom attribute name
|
|
494
|
+
:param Union[str, int, float] attribute_value: Attribute Value
|
|
495
|
+
:param VectorIndexAttributes attributes: Specify multiple attributes
|
|
496
|
+
to update in a single API invocation
|
|
497
|
+
:return: None
|
|
498
|
+
:raises: oracledb.DatabaseError
|
|
499
|
+
"""
|
|
500
|
+
if attribute_name and attribute_value and attributes:
|
|
501
|
+
raise ValueError(
|
|
502
|
+
"Either specify a single attribute using "
|
|
503
|
+
"attribute_name and attribute_value or "
|
|
504
|
+
"pass an object of type VectorIndexAttributes"
|
|
505
|
+
)
|
|
506
|
+
parameters = {"index_name": self.index_name}
|
|
507
|
+
if attributes:
|
|
508
|
+
self.attributes = attributes
|
|
509
|
+
parameters["attributes"] = attributes.json()
|
|
510
|
+
else:
|
|
511
|
+
setattr(self.attributes, attribute_name, attribute_value)
|
|
512
|
+
parameters["attributes_name"] = attribute_name
|
|
513
|
+
parameters["attributes_value"] = attribute_value
|
|
514
|
+
|
|
515
|
+
async with async_cursor() as cr:
|
|
516
|
+
await cr.callproc(
|
|
517
|
+
"DBMS_CLOUD_AI.UPDATE_VECTOR_INDEX",
|
|
518
|
+
keyword_parameters=parameters,
|
|
519
|
+
)
|
|
520
|
+
|
|
521
|
+
async def get_attributes(self) -> VectorIndexAttributes:
|
|
522
|
+
"""Get attributes of a vector index
|
|
523
|
+
|
|
524
|
+
:return: select_ai.VectorIndexAttributes
|
|
525
|
+
:raises: VectorIndexNotFoundError
|
|
526
|
+
"""
|
|
527
|
+
return await self._get_attributes(index_name=self.index_name)
|
|
528
|
+
|
|
529
|
+
@classmethod
|
|
530
|
+
async def list(
|
|
531
|
+
cls, index_name_pattern: str = ".*"
|
|
532
|
+
) -> AsyncGenerator[VectorIndex, None]:
|
|
533
|
+
"""List Vector Indexes.
|
|
534
|
+
|
|
535
|
+
:param str index_name_pattern: Regular expressions can be used
|
|
536
|
+
to specify a pattern. Function REGEXP_LIKE is used to perform the
|
|
537
|
+
match. Default value is ".*" i.e. match all vector indexes.
|
|
538
|
+
|
|
539
|
+
:return: AsyncGenerator[VectorIndex]
|
|
540
|
+
|
|
541
|
+
"""
|
|
542
|
+
async with async_cursor() as cr:
|
|
543
|
+
await cr.execute(
|
|
544
|
+
LIST_USER_VECTOR_INDEXES,
|
|
545
|
+
index_name_pattern=index_name_pattern,
|
|
546
|
+
)
|
|
547
|
+
rows = await cr.fetchall()
|
|
548
|
+
for row in rows:
|
|
549
|
+
index_name = row[0]
|
|
550
|
+
if row[1]:
|
|
551
|
+
description = await row[1].read() # AsyncLOB
|
|
552
|
+
else:
|
|
553
|
+
description = None
|
|
554
|
+
attributes = await cls._get_attributes(index_name=index_name)
|
|
555
|
+
yield VectorIndex(
|
|
556
|
+
index_name=index_name,
|
|
557
|
+
description=description,
|
|
558
|
+
attributes=attributes,
|
|
559
|
+
profile=await AsyncProfile(
|
|
560
|
+
profile_name=attributes.profile_name
|
|
561
|
+
),
|
|
562
|
+
)
|
select_ai/version.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
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
|
+
__version__ = "1.0.0"
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: select_ai
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Select AI for Python
|
|
5
|
+
Author-email: Abhishek Singh <abhishek.o.singh@oracle.com>
|
|
6
|
+
Maintainer-email: Abhishek Singh <abhishek.o.singh@oracle.com>
|
|
7
|
+
License-Expression: UPL-1.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/oracle/python-select-ai
|
|
9
|
+
Project-URL: Repository, https://github.com/oracle/python-select-ai
|
|
10
|
+
Project-URL: Issues, https://github.com/oracle/python-select-ai/issues
|
|
11
|
+
Project-URL: Documentation, https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/pysai/
|
|
12
|
+
Keywords: oracle,select-ai,adbs,autonomous database serverless
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Natural Language :: English
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
24
|
+
Classifier: Topic :: Database
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Requires-Python: >=3.9
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE.txt
|
|
30
|
+
Requires-Dist: oracledb
|
|
31
|
+
Requires-Dist: pandas==2.2.3
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# Select AI for Python
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
Select AI for Python enables you to ask questions of your database data using natural language (text-to-SQL), get generative AI responses using your trusted content (retrieval augmented generation), generate synthetic data using large language models, and other features – all from Python. With the general availability of Select AI Python, Python developers have access to the functionality of Select AI on Oracle Autonomous Database.
|
|
38
|
+
|
|
39
|
+
Select AI for Python enables you to leverage the broader Python ecosystem in combination with generative AI and database functionality - bridging the gap between the DBMS_CLOUD_AI PL/SQL package and Python's rich ecosystem. It provides intuitive objects and methods for AI model interaction.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
Run
|
|
45
|
+
```bash
|
|
46
|
+
python3 -m pip install select_ai
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Documentation
|
|
50
|
+
|
|
51
|
+
See [Select AI for Python documentation][documentation]
|
|
52
|
+
|
|
53
|
+
## Samples
|
|
54
|
+
|
|
55
|
+
Examples can be found in the [/samples][samples] directory
|
|
56
|
+
|
|
57
|
+
### Basic Example
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import select_ai
|
|
61
|
+
|
|
62
|
+
user = "<your_select_ai_user>"
|
|
63
|
+
password = "<your_select_ai_password>"
|
|
64
|
+
dsn = "<your_select_ai_db_connect_string>"
|
|
65
|
+
|
|
66
|
+
select_ai.connect(user=user, password=password, dsn=dsn)
|
|
67
|
+
profile = select_ai.Profile(profile_name="oci_ai_profile")
|
|
68
|
+
# run_sql returns a pandas dataframe
|
|
69
|
+
df = profile.run_sql(prompt="How many promotions?")
|
|
70
|
+
print(df.columns)
|
|
71
|
+
print(df)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Async Example
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
|
|
78
|
+
import asyncio
|
|
79
|
+
|
|
80
|
+
import select_ai
|
|
81
|
+
|
|
82
|
+
user = "<your_select_ai_user>"
|
|
83
|
+
password = "<your_select_ai_password>"
|
|
84
|
+
dsn = "<your_select_ai_db_connect_string>"
|
|
85
|
+
|
|
86
|
+
# This example shows how to asynchronously run sql
|
|
87
|
+
async def main():
|
|
88
|
+
await select_ai.async_connect(user=user, password=password, dsn=dsn)
|
|
89
|
+
async_profile = await select_ai.AsyncProfile(
|
|
90
|
+
profile_name="async_oci_ai_profile",
|
|
91
|
+
)
|
|
92
|
+
# run_sql returns a pandas df
|
|
93
|
+
df = await async_profile.run_sql("How many promotions?")
|
|
94
|
+
print(df)
|
|
95
|
+
|
|
96
|
+
asyncio.run(main())
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
## Help
|
|
100
|
+
|
|
101
|
+
Questions can be asked in [GitHub Discussions][ghdiscussions].
|
|
102
|
+
|
|
103
|
+
Problem reports can be raised in [GitHub Issues][ghissues].
|
|
104
|
+
|
|
105
|
+
## Contributing
|
|
106
|
+
|
|
107
|
+
This project welcomes contributions from the community. Before submitting a pull request, please [review our contribution guide][contributing]
|
|
108
|
+
|
|
109
|
+
## Security
|
|
110
|
+
|
|
111
|
+
Please consult the [security guide][security] for our responsible security vulnerability disclosure process
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
Copyright (c) 2025 Oracle and/or its affiliates.
|
|
116
|
+
|
|
117
|
+
Released under the Universal Permissive License v1.0 as shown at
|
|
118
|
+
<https://oss.oracle.com/licenses/upl/>.
|
|
119
|
+
|
|
120
|
+
[contributing]: https://github.com/oracle/python-select-ai/blob/main/CONTRIBUTING.md
|
|
121
|
+
[documentation]: https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/pysai/
|
|
122
|
+
[ghdiscussions]: https://github.com/oracle/python-select-ai/discussions
|
|
123
|
+
[ghissues]: https://github.com/oracle/python-select-ai/issues
|
|
124
|
+
[samples]: https://github.com/oracle/python-select-ai/tree/main/samples
|
|
125
|
+
[security]: https://github.com/oracle/python-select-ai/blob/main/SECURITY.md
|