はじめに
Oracle CloudのAutonomous Databaseで提供される自然言語によるクエリ実行(SELECT AI)のPythonモジュール「select_ai」の v1.3.0が提供されました。インストールし追加された機能のいくつかを動作確認してみました。
Select AI for Python v1.3.0 Update
- 接続プールのサポート
- select_ai.create_pool()
- select_ai.create_pool_async()
- Vector Indexの拡張
- ベクトル索引の次のスケジュール済リフレッシュのUTCタイムスタンプのフェッチ
- vector_index.get_next_refresh_timestamp()
- VectorIndex.create() に wait_for_completion 引数を追加
- ベクトル索引の次のスケジュール済リフレッシュのUTCタイムスタンプのフェッチ
- 通知ツールの改修
- メール通知ツール:APIシグネチャに subject パラメータを追加
- Slack通知ツール:チャンネル指定パラメータを slack_channel から channel に変更
- ツール作成APIの改善
- すべてのツール作成APIに instruction パラメータを追加
- セッション内カーソル再利用によるフィードバック精度向上(内部改善)
- エラーハンドリングの強化
- Select AIオブジェクトの空の属性を検出するカスタムエラーを実装
事前準備
- Python環境
- Select AI Python API(select_aiモジュール)は、Pythonバージョン3.9、3.10、3.11、3.12、3.13および3.14をサポートしています。
- PIP
- uv 環境での準備
$ curl -LsSf https://astral.sh/uv/install.sh | sh
downloading uv 0.9.15 x86_64-unknown-linux-gnu
no checksums to verify
installing to /home/opc/.local/bin
uv
uvx
everything's installed!
$ mkdir /home/opc/selectai1.3
$ cd /home/opc/selectai1.3
$ uv venv --python 3.14
Using CPython 3.14.1
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate
$ source .venv/bin/activate
$ python3 -m ensurepip --upgrade
Looking in links: /tmp/tmpchhan0re
Processing /tmp/tmpchhan0re/pip-25.3-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-25.3
-- C++ コンパイラをインストール
$ sudo dnf install gcc-c++
Ksplice for Oracle Linux 9 (x86_64) 186 kB/s | 3.5 kB 00:00
Ksplice for Oracle Linux 9 (x86_64) 38 MB/s | 18 MB 00:00
Oracle Linux 9 OCI Included Packages (x86_64) 160 kB/s | 3.5 kB 00:00
Oracle Linux 9 OCI Included Packages (x86_64) 49 MB/s | 215 MB 00:04
Oracle Linux 9 BaseOS Latest (x86_64) 199 kB/s | 4.3 kB 00:00
Oracle Linux 9 BaseOS Latest (x86_64) 75 MB/s | 113 MB 00:01
Oracle Linux 9 Application Stream Packages (x86 261 kB/s | 4.5 kB 00:00
Oracle Linux 9 Application Stream Packages (x86 75 MB/s | 85 MB 00:01
Oracle Linux 9 Addons (x86_64) 167 kB/s | 3.5 kB 00:00
Oracle Linux 9 Addons (x86_64) 381 kB/s | 843 kB 00:02
Oracle Linux 9 UEK Release 8 (x86_64) 145 kB/s | 3.5 kB 00:00
Oracle Linux 9 UEK Release 8 (x86_64) 73 MB/s | 31 MB 00:00
Last metadata expiration check: 0:00:03 ago on Tue 17 Mar 2026 08:29:30 AM GMT.
Package gcc-c++-11.5.0-11.0.1.el9.x86_64 is already installed.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Upgrading:
cpp x86_64 11.5.0-11.0.2.el9 ol9_appstream 11 M
gcc x86_64 11.5.0-11.0.2.el9 ol9_appstream 33 M
gcc-c++ x86_64 11.5.0-11.0.2.el9 ol9_appstream 13 M
libgcc x86_64 11.5.0-11.0.2.el9 ol9_baseos_latest 94 k
libgomp x86_64 11.5.0-11.0.2.el9 ol9_baseos_latest 263 k
libstdc++ x86_64 11.5.0-11.0.2.el9 ol9_baseos_latest 757 k
libstdc++-devel x86_64 11.5.0-11.0.2.el9 ol9_appstream 3.1 M
Transaction Summary
================================================================================
Upgrade 7 Packages
Total download size: 60 M
Is this ok [y/N]: y
Downloading Packages:
(1/7): libgcc-11.5.0-11.0.2.el9.x86_64.rpm 265 kB/s | 94 kB 00:00
(2/7): libgomp-11.5.0-11.0.2.el9.x86_64.rpm 519 kB/s | 263 kB 00:00
(3/7): libstdc++-11.5.0-11.0.2.el9.x86_64.rpm 912 kB/s | 757 kB 00:00
(4/7): cpp-11.5.0-11.0.2.el9.x86_64.rpm 14 MB/s | 11 MB 00:00
(5/7): libstdc++-devel-11.5.0-11.0.2.el9.x86_64 18 MB/s | 3.1 MB 00:00
(6/7): gcc-c++-11.5.0-11.0.2.el9.x86_64.rpm 18 MB/s | 13 MB 00:00
(7/7): gcc-11.5.0-11.0.2.el9.x86_64.rpm 20 MB/s | 33 MB 00:01
--------------------------------------------------------------------------------
Total 29 MB/s | 60 MB 00:02
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Upgrading : libgcc-11.5.0-11.0.2.el9.x86_64 1/14
warning: waiting for transaction lock on /var/lib/rpm/.rpm.lock
Running scriptlet: libgcc-11.5.0-11.0.2.el9.x86_64 1/14
Upgrading : libstdc++-11.5.0-11.0.2.el9.x86_64 2/14
Upgrading : libstdc++-devel-11.5.0-11.0.2.el9.x86_64 3/14
Upgrading : cpp-11.5.0-11.0.2.el9.x86_64 4/14
Upgrading : libgomp-11.5.0-11.0.2.el9.x86_64 5/14
Upgrading : gcc-11.5.0-11.0.2.el9.x86_64 6/14
Upgrading : gcc-c++-11.5.0-11.0.2.el9.x86_64 7/14
Cleanup : gcc-c++-11.5.0-11.0.1.el9.x86_64 8/14
Cleanup : libstdc++-devel-11.5.0-11.0.1.el9.x86_64 9/14
Cleanup : libstdc++-11.5.0-11.0.1.el9.x86_64 10/14
Cleanup : gcc-11.5.0-11.0.1.el9.x86_64 11/14
Cleanup : cpp-11.5.0-11.0.1.el9.x86_64 12/14
Cleanup : libgcc-11.5.0-11.0.1.el9.x86_64 13/14
Running scriptlet: libgcc-11.5.0-11.0.1.el9.x86_64 13/14
Cleanup : libgomp-11.5.0-11.0.1.el9.x86_64 14/14
Running scriptlet: libgomp-11.5.0-11.0.1.el9.x86_64 14/14
Verifying : libgcc-11.5.0-11.0.2.el9.x86_64 1/14
Verifying : libgcc-11.5.0-11.0.1.el9.x86_64 2/14
Verifying : libgomp-11.5.0-11.0.2.el9.x86_64 3/14
Verifying : libgomp-11.5.0-11.0.1.el9.x86_64 4/14
Verifying : libstdc++-11.5.0-11.0.2.el9.x86_64 5/14
Verifying : libstdc++-11.5.0-11.0.1.el9.x86_64 6/14
Verifying : cpp-11.5.0-11.0.2.el9.x86_64 7/14
Verifying : cpp-11.5.0-11.0.1.el9.x86_64 8/14
Verifying : gcc-11.5.0-11.0.2.el9.x86_64 9/14
Verifying : gcc-11.5.0-11.0.1.el9.x86_64 10/14
Verifying : gcc-c++-11.5.0-11.0.2.el9.x86_64 11/14
Verifying : gcc-c++-11.5.0-11.0.1.el9.x86_64 12/14
Verifying : libstdc++-devel-11.5.0-11.0.2.el9.x86_64 13/14
Verifying : libstdc++-devel-11.5.0-11.0.1.el9.x86_64 14/14
Upgraded:
cpp-11.5.0-11.0.2.el9.x86_64 gcc-11.5.0-11.0.2.el9.x86_64
gcc-c++-11.5.0-11.0.2.el9.x86_64 libgcc-11.5.0-11.0.2.el9.x86_64
libgomp-11.5.0-11.0.2.el9.x86_64 libstdc++-11.5.0-11.0.2.el9.x86_64
libstdc++-devel-11.5.0-11.0.2.el9.x86_64
Complete!
- Oracle Cloud Infrastructure CLI構成ファイルでのプロファイル
- 参考)CLI構成ファイル
- oci cliのインストールは必須ではありません。
- Autonomous Databaseに接続用のtnsnames.oraやウォレット
- AI クレデンシャル/AI プロファイルの作成
- 今回の動作確認は、「Select AI for Python v1.2.0」を使ってみた。こちらの記事の内容で作成しています。
「select_ai」モジュールのインストール
Github より「select_ai-1.3.0」を入手し、「select_ai」モジュールのインストール
$ python3 -m pip install select_ai --upgrade
Collecting select_ai
Downloading select_ai-1.3.0-py3-none-any.whl.metadata (4.6 kB)
Collecting oracledb (from select_ai)
Downloading oracledb-3.4.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.metadata (7.7 kB)
Collecting pandas==2.2.3 (from select_ai)
Using cached pandas-2.2.3-cp314-cp314-linux_x86_64.whl
Collecting numpy>=1.26.0 (from pandas==2.2.3->select_ai)
Downloading numpy-2.4.3-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (6.6 kB)
Collecting python-dateutil>=2.8.2 (from pandas==2.2.3->select_ai)
Using cached python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB)
Collecting pytz>=2020.1 (from pandas==2.2.3->select_ai)
Downloading pytz-2026.1.post1-py2.py3-none-any.whl.metadata (22 kB)
Collecting tzdata>=2022.7 (from pandas==2.2.3->select_ai)
Downloading tzdata-2025.3-py2.py3-none-any.whl.metadata (1.4 kB)
Collecting six>=1.5 (from python-dateutil>=2.8.2->pandas==2.2.3->select_ai)
Using cached six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Collecting cryptography>=3.2.1 (from oracledb->select_ai)
Downloading cryptography-46.0.5-cp311-abi3-manylinux_2_34_x86_64.whl.metadata (5.7 kB)
Collecting typing_extensions>=4.14.0 (from oracledb->select_ai)
Using cached typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB)
Collecting cffi>=2.0.0 (from cryptography>=3.2.1->oracledb->select_ai)
Using cached cffi-2.0.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (2.6 kB)
Collecting pycparser (from cffi>=2.0.0->cryptography>=3.2.1->oracledb->select_ai)
Downloading pycparser-3.0-py3-none-any.whl.metadata (8.2 kB)
Downloading select_ai-1.3.0-py3-none-any.whl (53 kB)
Downloading numpy-2.4.3-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (16.6 MB)
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 16.6/16.6 MB 93.0 MB/s 0:00:00
Using cached python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
Downloading pytz-2026.1.post1-py2.py3-none-any.whl (510 kB)
Using cached six-1.17.0-py2.py3-none-any.whl (11 kB)
Downloading tzdata-2025.3-py2.py3-none-any.whl (348 kB)
Downloading oracledb-3.4.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (2.5 MB)
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 2.5/2.5 MB 85.2 MB/s 0:00:00
Downloading cryptography-46.0.5-cp311-abi3-manylinux_2_34_x86_64.whl (4.5 MB)
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 4.5/4.5 MB 102.0 MB/s 0:00:00
Using cached cffi-2.0.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (219 kB)
Using cached typing_extensions-4.15.0-py3-none-any.whl (44 kB)
Downloading pycparser-3.0-py3-none-any.whl (48 kB)
Installing collected packages: pytz, tzdata, typing_extensions, six, pycparser, numpy, python-dateutil, cffi, pandas, cryptography, oracledb, select_ai
Successfully installed cffi-2.0.0 cryptography-46.0.5 numpy-2.4.3 oracledb-3.4.2 pandas-2.2.3 pycparser-3.0 python-dateutil-2.9.0.post0 pytz-2026.1.post1 select_ai-1.3.0 six-1.17.0 typing_extensions-4.15.0 tzdata-2025.3
[notice] A new release of pip is available: 25.3 -> 26.0.1
[notice] To update, run: pip3 install --upgrade pip
データベース接続の確認
環境変数の設定
データベース管理者ユーザとパスワード、SELECT AI実行データベースユーザとパスワード、データベース接続情報を環境変数として設定
export SELECT_AI_ADMIN_USER=admin
export SELECT_AI_ADMIN_PASSWORD=<PASSWORD>
export SELECT_AI_USER=PYTHONAI
export SELECT_AI_PASSWORD=<PASSWORD>
export SELECT_AI_DB_CONNECT_STRING=atp23ai_medium
export TNS_ADMIN=/home/opc/wallet
以下のような select_ai_connection_test.py を作成しデータベース接続を確認。
import select_ai
user = "<your_db_user>"
password = "<your_db_password>"
dsn = "<your_db_dsn>"
select_ai.connect(user=user, password=password, dsn=dsn)
print("Connected to the Database")
実行
$ python3 select_ai_connection_test.py
Connected to the Database
SELECT AI v1.3.0 機能の動作確認
接続プールの作成
v1.3.0で追加された create_pool() を使って共有接続プールを作成します。複数スレッドや非同期処理でデータベース接続を効率的に再利用できます。
import os
import select_ai
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
pool = select_ai.create_pool(
user=user,
password=password,
dsn=dsn
)
profile = select_ai.Profile(profile_name='oci_ai_profile')
df = profile.run_sql(prompt='How many promotions?')
print(df)
実行例
$ python3 create_pool.py
COUNT(*)
0 503
$
非同期接続プールの作成
非同期版の create_pool_async() を使うと、FastAPI などの非同期アプリケーションでも同様にプールを共有できます。
import os
import asyncio
import select_ai
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
async def main():
pool = select_ai.create_pool_async(
user=user,
password=password,
dsn=dsn
)
profile = await select_ai.AsyncProfile(profile_name='oci_ai_profile')
df = await profile.run_sql('How many promotions?')
print(df)
asyncio.run(main())
実行例
$ python3 create_pool_async.py
COUNT(*)
0 503
$
ベクトルインデックスの次回更新時刻を取得
v1.3.0で追加された get_next_refresh_timestamp() を使うと、ベクトルインデックスの次回自動更新がいつ実行されるかをUTCで確認できます。
import os
import select_ai
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
select_ai.connect(user=user, password=password, dsn=dsn)
vector_index = select_ai.VectorIndex(index_name='test_vector_index')
next_refresh = vector_index.get_next_refresh_timestamp()
print(f'Next refresh (UTC): {next_refresh}')
実行例
$ python3 vector_index_next_refresh.py
Next refresh (UTC): 2026-03-18 00:01:02.879408+00:00
$
ベクトルインデックス作成時に完了を待機
VectorIndex.create() に wait_for_completion 引数が追加されました。True を指定するとインデックス構築が完了するまで処理をブロックします。
「location」は、利用する環境のObject Storageバケットの値に修正します。
import os
import select_ai
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
select_ai.connect(user=user, password=password, dsn=dsn)
provider = select_ai.OCIGenAIProvider(
region='us-chicago-1',
oci_apiformat='GENERIC',
embedding_model='cohere.embed-v4.0'
)
profile_attributes = select_ai.ProfileAttributes(
provider=provider,
credential_name='my_oci_ai_profile_key',
vector_index_name='test_vector_index2'
)
profile = select_ai.Profile(
profile_name='oci_vector_ai_profile2',
attributes=profile_attributes,
replace=True
)
index_attributes = select_ai.OracleVectorIndexAttributes(
location='https://XXXXX.objectstorage.us-chicago-1.oci.customer-oci.com/n/XXXXX/b/XXXXX/o/',
object_storage_credential_name='my_oci_ai_profile_key',
profile_name='oci_vector_ai_profile2',
refresh_rate=1440
)
vector_index = select_ai.VectorIndex(
index_name='test_vector_index2',
attributes=index_attributes,
profile=profile
)
vector_index.create(wait_for_completion=True)
print('Vector index created successfully')
実行例
$ python3 vector_index_create_wait.py
Vector index created successfully
$
ツール作成APIへの instruction パラメータ追加
v1.3.0では、すべてのツール作成APIに instruction パラメータが追加されました。LLMへのプロンプトに含まれ、ツールをいつどのように使うかをエージェントに指示できます。
import os
import select_ai
import select_ai.agent
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
select_ai.connect(user=user, password=password, dsn=dsn)
sql_tool = select_ai.agent.Tool.create_built_in_tool(
tool_name='MOVIE_SQL_TOOL',
tool_type='SQL',
tool_params=select_ai.agent.ToolParams(profile_name='oci_ai_profile'),
instruction='Use this tool only for questions related to movie sales data.',
replace=True
)
print('Tool created: ', sql_tool.tool_name)
実行例
$ python3 agent_tool_create_with_instruction.py
Tool created: MOVIE_SQL_TOOL
$
メール通知ツールの subject パラメータ追加
v1.3.0でメール通知ツールのAPIシグネチャに subject パラメータが追加されました。
import os
import select_ai
import select_ai.agent
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
select_ai.connect(user=user, password=password, dsn=dsn)
email_tool = select_ai.agent.Tool.create_built_in_tool(
tool_name='ALERT_EMAIL_TOOL',
tool_type='NOTIFICATION',
tool_params=select_ai.agent.ToolParams(
notification_type='EMAIL',
credential_name='my_email_credential',
recipient='admin@example.com',
sender='noreply@example.com',
smtp_host='smtp.example.com',
subject='Select AI Alert Notification'
),
instruction='Send an email when a critical alert is detected.',
replace=True
)
print('Tool created: ', email_tool.tool_name)
実行例
$ python3 agent_email_tool_create.py
Tool created: ALERT_EMAIL_TOOL
$
Slack通知ツールのパラメータ修正
v1.3.0でSlack通知ツールのチャンネル指定パラメータが slack_channel から channel に修正されました。v1.2.x 以前のコードをお使いの場合はパラメータ名の変更が必要です。
import os
import select_ai
import select_ai.agent
user = os.getenv('SELECT_AI_USER')
password = os.getenv('SELECT_AI_PASSWORD')
dsn = os.getenv('SELECT_AI_DB_CONNECT_STRING')
select_ai.connect(user=user, password=password, dsn=dsn)
slack_tool = select_ai.agent.Tool.create_built_in_tool(
tool_name='ALERT_SLACK_TOOL',
tool_type='NOTIFICATION',
tool_params=select_ai.agent.ToolParams(
notification_type='SLACK',
credential_name='my_slack_credential',
channel='#alert-channel' # v1.3.0: slack_channel から channel に変更
),
instruction='Notify the Slack channel when an incident occurs.',
replace=True
)
print('Tool created: ', slack_tool.tool_name)
実行例
$ python3 agent_slack_tool_create.py
Tool created: ALERT_SLACK_TOOL
$
さいごに
「Select AI for Python v1.3.0」で接続プールや Vector Index の拡張など、本番利用に向けた機能強化が行われました。
特に create_pool() の追加により、Web APIバックエンドや並列バッチ処理といったユースケースへの適用がより現実的になっています。