記事タイトルはキャッチーにしようと某作品名に寄せて失敗した例です![]()
はじめに
OracleDB で埋め込みモデルを利用する代表的な方法と、その主なメリット・デメリットは以下の通りです。
- ONNX形式のモデルを DB にインポートして使用 (DB内)
- 埋め込みモデルを ONNX形式に変換 → OracleDB に取り込み、DB内でベクトル化する方法
- (+) ベクトルの元データ (e.g. 機密情報、個人情報) を DB の外に出す必要がない
- (+) 外部サービスや NW の障害、制限などの影響を受けづらい
- (-) モデルの選択肢が少ない
- (-) DB処理とリソース競合する可能性がある
- OCI Generative AI Service を使用 (DB外)
- OCI の Generative AI Service上の埋め込みモデルを呼び出してベクトル化する方法
- (+) OCI を既に使用している場合、OCI に統一可能
- (-) サードパーティ・サービス・プロバイダの使用 (後述) と比べると、モデルが限定される
- サードパーティ・サービス・プロバイダを使用 (DB外)
- Anthropic, Cohere 等の外部サービスを呼び出してベクトル化する方法
- (+) モデルの選択肢が多い
- (-) データガバナンスの懸念が大きい (プロバイダごとに追加のリスク評価が必要)
以降、それぞれの具体的な使用方法を解説していきます。
ONNX形式のモデルを DB にインポートして使用
埋め込みモデルの入手と ONNX変換には OML4Py (Oracle Machine Learning for Python) を使用するため、まずはこれを入手してセットアップします。
OML4Py は https://www.oracle.com/database/technologies/oml4py-downloads.html# から DL可能です。
DLした zipファイルは scp などで OML4Py を実行するサーバに移送します。(今回は OCI の VMインスタンスに移送しています)
PS C:\Users\xxx\Downloads\for_oml4py> scp -i .\ssh-key-2026-05-08.key .\V1048628-01.zip opc@xxx:/tmp
The authenticity of host 'xxx' can't be established.
ED25519 key fingerprint is SHA256:xxx.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added 'xxx' (ED25519) to the list of known hosts.
V1048628-01.zip 100% 9936KB 21.4MB/s 00:00
PS C:\Users\xxx\Downloads\for_oml4py>
OML4Py に必要な Python 3.12 と perl-Envパッケージをインストールします。
[opc@for-oml4py ~]$ sudo dnf update -y
Ksplice for Oracle Linux 8 (x86_64) 63 MB/s | 56 MB 00:00
MySQL 8.4 Server Community for Oracle Linux 8 ( 30 MB/s | 1.7 MB 00:00
MySQL 8.4 Tools Community for Oracle Linux 8 (x 21 MB/s | 906 kB 00:00
MySQL Connectors Community for Oracle Linux 8 ( 1.3 MB/s | 62 kB 00:00
Oracle Software for OCI users on Oracle Linux 8 55 MB/s | 168 MB 00:03
Oracle Linux 8 BaseOS Latest (x86_64) 73 MB/s | 140 MB 00:01
Oracle Linux 8 Application Stream (x86_64) 70 MB/s | 80 MB 00:01
Oracle Linux 8 Addons (x86_64) 71 MB/s | 67 MB 00:00
Latest Unbreakable Enterprise Kernel Release 7 80 MB/s | 95 MB 00:01
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
(snip)
Installed:
kernel-4.18.0-553.123.1.el8_10.x86_64
kernel-core-4.18.0-553.123.1.el8_10.x86_64
kernel-devel-4.18.0-553.123.1.el8_10.x86_64
kernel-modules-4.18.0-553.123.1.el8_10.x86_64
Complete!
[opc@for-oml4py ~]$
[opc@for-oml4py ~]$ sudo dnf install -y oracle-epel-release-el8
Last metadata expiration check: 0:06:45 ago on Fri 08 May 2026 05:16:21 AM GMT.
Package oracle-epel-release-el8-1.0-5.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[opc@for-oml4py ~]$ sudo dnf config-manager --set-enabled ol8_codeready_builder
[opc@for-oml4py ~]$ python3 --version
Python 3.6.8
[opc@for-oml4py ~]$ sudo dnf install -y python3.12
Oracle Linux 8 BaseOS Latest (x86_64) 363 kB/s | 4.3 kB 00:00
Oracle Linux 8 Application Stream (x86_64) 342 kB/s | 4.5 kB 00:00
Oracle Linux 8 CodeReady Builder (x86_64) - Uns 38 MB/s | 15 MB 00:00
Oracle Linux 8 Addons (x86_64) 266 kB/s | 3.5 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
(snip)
Installed:
mpdecimal-2.5.1-3.el8.x86_64
python3.12-3.12.13-2.el8_10.x86_64
python3.12-libs-3.12.13-2.el8_10.x86_64
python3.12-pip-wheel-23.2.1-4.el8.noarch
Complete!
[opc@for-oml4py ~]$ python3 --version
Python 3.6.8
[opc@for-oml4py ~]$ sudo alternatives --config python3
There are 2 programs which provide 'python3'.
Selection Command
-----------------------------------------------
*+ 1 /usr/bin/python3.6
2 /usr/bin/python3.12
Enter to keep the current selection[+], or type selection number: 2
[opc@for-oml4py ~]$ python3 --version
Python 3.12.13
[opc@for-oml4py ~]$ sudo yum install -y perl-Env
Last metadata expiration check: 0:02:38 ago on Fri 08 May 2026 05:24:09 AM GMT.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
(snip)
Installed:
perl-Env-1.04-395.el8.noarch
Complete!
Python 仮想環境を作成 → アクティベートし、OML4Py に必要なパッケージをインストールします。
[opc@for-oml4py ~]$ mkdir -p ~/oml4py
[opc@for-oml4py ~]$ cd oml4py/
[opc@for-oml4py oml4py]$ python3 -m venv .venv
[opc@for-oml4py oml4py]$ source .venv/bin/activate
((.venv) ) [opc@for-oml4py oml4py]$ cat <<EOF > requirements.txt
> --extra-index-url https://download.pytorch.org/whl/cpu
> pandas==2.2.2
> setuptools==70.0.0
> scipy==1.14.0
> matplotlib==3.8.4
> oracledb==2.4.1
> scikit-learn==1.5.1
> numpy>=2.0.1
> onnxruntime==1.20.0
> onnxruntime-extensions==0.12.0
> onnx==1.17.0
> torch==2.6.0
> transformers==4.49.0
> sentencepiece==0.2.0
> EOF
((.venv) ) [opc@for-oml4py oml4py]$
((.venv) ) [opc@for-oml4py oml4py]$ pip install -r requirements.txt
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cpu
(snip)
Successfully installed MarkupSafe-3.0.3 certifi-2026.4.22 cffi-2.0.0 charset_normalizer-3.4.7 coloredlogs-15.0.1 contourpy-1.3.3 cryptography-48.0.0 cycler-0.12.1 filelock-3.29.0 flatbuffers-25.12.19 fonttools-4.62.1 fsspec-2026.4.0 hf-xet-1.5.0 huggingface-hub-0.36.2 humanfriendly-10.0 idna-3.13 jinja2-3.1.6 joblib-1.5.3 kiwisolver-1.5.0 matplotlib-3.8.4 mpmath-1.3.0 networkx-3.6.1 numpy-2.2.6 onnx-1.17.0 onnxruntime-1.20.0 onnxruntime-extensions-0.12.0 oracledb-2.4.1 packaging-26.2 pandas-2.2.2 pillow-12.2.0 protobuf-7.34.1 pycparser-3.0 pyparsing-3.3.2 python-dateutil-2.9.0.post0 pytz-2026.2 pyyaml-6.0.3 regex-2026.4.4 requests-2.33.1 safetensors-0.7.0 scikit-learn-1.5.1 scipy-1.14.0 sentencepiece-0.2.0 setuptools-70.0.0 six-1.17.0 sympy-1.13.1 threadpoolctl-3.6.0 tokenizers-0.21.4 torch-2.6.0+cpu tqdm-4.67.3 transformers-4.49.0 typing-extensions-4.15.0 tzdata-2026.2 urllib3-2.7.0
[notice] A new release of pip is available: 23.2.1 -> 26.1.1
[notice] To update, run: pip install --upgrade pip
((.venv) ) [opc@for-oml4py oml4py]$
scpしていた OML4Py を作業ディレクトリにコピー、解凍します。
[opc@for-oml4py ~]$ cp /tmp/V1048628-01.zip /home/opc/oml4py/
[opc@for-oml4py ~]$ cd oml4py/
[opc@for-oml4py oml4py]$ unzip V1048628-01.zip
Archive: V1048628-01.zip
inflating: client/client.pl
inflating: client/OML4PInstallShared.pm
inflating: client/oml-2.1-cp312-cp312-linux_x86_64.whl
extracting: client/oml4py.ver
[opc@for-oml4py oml4py]$ ls -la
total 9940
drwxrwxr-x. 4 opc opc 80 May 8 05:45 .
drwx------. 5 opc opc 102 May 8 05:28 ..
drwxrwxr-x. 2 opc opc 114 May 8 05:45 client
-rw-rw-r--. 1 opc opc 288 May 8 05:28 requirements.txt
-rw-rw-r--. 1 opc opc 10174054 May 8 05:44 V1048628-01.zip
drwxrwxr-x. 6 opc opc 87 May 8 05:29 .venv
OML4Py client をインストールします。
((.venv) ) [opc@for-oml4py oml4py]$ perl -Iclient client/client.pl
Oracle Machine Learning for Python 2.1 Client.
Copyright (c) 2018, 2025 Oracle and/or its affiliates. All rights reserved.
Checking platform .................. Pass
Checking Python .................... Pass
Checking dependencies .............. Pass
Checking OML4P version ............. Pass
Current configuration
Python Version ................... 3.12.13
PYTHONHOME ....................... /home/opc/oml4py/.venv
Existing OML4P module version .... None
Operation ........................ Install/Upgrade
Proceed? [yes]yes
Processing ./client/oml-2.1-cp312-cp312-linux_x86_64.whl
Installing collected packages: oml
Successfully installed oml-2.1
Done
((.venv) ) [opc@for-oml4py oml4py]$
今回使用するモデルはsentence-transformers/all-MiniLM-L6-v2です。
冒頭でも記載した通り、この方法ではモデルの選択肢が限られるため、目的のモデルが使用できるか事前に確認しておきましょう。
Tasks > Sentence Similarity, Other > text-embedding-inference で絞り込むとさらに少なくなる。

((.venv) ) [opc@for-oml4py oml4py]$ python3
Python 3.12.13 (main, Apr 27 2026, 09:57:22) [GCC 8.5.0 20210514 (Red Hat 8.5.0-28.0.1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from oml.utils import EmbeddingModel, EmbeddingModelConfig
config = EmbeddingModelConfig.from_template("text",max_seq_length=512,quantize_model=True)
em = EmbeddingModel(model_name="sentence-transformers/all-MiniLM-L6-v2", config=config)
em.export2file("all-MiniLM-L6-v2",output_dir=".")
>>> config = EmbeddingModelConfig.from_template("text",max_seq_length=512,quantize_model=True)
UserWarning:EmbeddingModelConfig.from_template is deprecated and will be removed in a future release. Please use the method in ONNXPipelineConfig instead.
>>> em = EmbeddingModel(model_name="sentence-transformers/all-MiniLM-L6-v2", config=config)
UserWarning:Warning: EmbeddingModel is deprecated and will be removed in a future release. Please use ONNXPipeline instead.
>>> em.export2file("all-MiniLM-L6-v2",output_dir=".")
UserWarning:EmbeddingModel.export2file is deprecated and will be removed in a future release. Please use the method in ONNXPipeline instead.
tokenizer_config.json: 100%|███████████████████| 350/350 [00:00<00:00, 3.79MB/s]
vocab.txt: 232kB [00:00, 42.9MB/s]
special_tokens_map.json: 100%|█████████████████| 112/112 [00:00<00:00, 1.02MB/s]
tokenizer.json: 466kB [00:00, 63.5MB/s]
config.json: 100%|█████████████████████████████| 612/612 [00:00<00:00, 9.07MB/s]
model.safetensors: 100%|███████████████████| 90.9M/90.9M [00:model.safetensors: 100%|███████████████████| 90.9M/90.9M [00:01<00:00, 74.4MB/s]
UserWarning:Batch inference not supported in quantized models. Setting batch size to 1.
>>> exit()
deprecated warning が出力されているので、ONNXPipeline とやらは別途確認して記事にしたいと思います。
確認すると、ONNX形式で出力されました。
((.venv) ) [opc@for-oml4py oml4py]$ ls -la
total 32460
drwxrwxr-x. 4 opc opc 109 May 8 05:51 .
drwx------. 6 opc opc 140 May 8 05:52 ..
-rw-rw-r--. 1 opc opc 23057153 May 8 05:51 all-MiniLM-L6-v2.onnx ★
drwxrwxr-x. 2 opc opc 114 May 8 05:45 client
-rw-rw-r--. 1 opc opc 288 May 8 05:28 requirements.txt
-rw-rw-r--. 1 opc opc 10174054 May 8 05:44 V1048628-01.zip
drwxrwxr-x. 6 opc opc 87 May 8 05:29 .venv
((.venv) ) [opc@for-oml4py oml4py]$
今回はこのモデルを ADB-S にインポートするため、出力されたファイルは Object Storage に UL しておきます。
インポートにあたってディレクトリオブジェクトを作成し、作成したディレクトリオブジェクトへ Object Storage から ONNXファイルを移動します。(クレデンシャル (ここでは CRED2) の作成は割愛)
SQL> create directory temp as 'temp';
Directory TEMP created.
SQL> select * from dba_directories where directory_name = 'TEMP';
OWNER DIRECTORY_NAME DIRECTORY_PATH ORIGIN_CON_ID
________ _________________ __________________________ ________________
SYS TEMP /u03/dbfs/xxx/data/temp 109
SQL>
SQL> BEGIN
2 DBMS_CLOUD.GET_OBJECT(
3 credential_name => 'CRED2',
4 object_uri => 'https://objectstorage.ap-tokyo-1.oraclecloud.com/n/xxx/b/handson-bucket/o/all-MiniLM-L6-v2.onnx',
5 directory_name => 'temp',
6 file_name => 'all-MiniLM-L6-v2.onnx'
7 );
8 END;
9* /
PL/SQL procedure successfully completed.
SQL>
SQL> SELECT OBJECT_NAME, CREATED FROM TABLE(DBMS_CLOUD.LIST_FILES('TEMP'));
OBJECT_NAME CREATED
________________________ ______________________________________
all-MiniLM-L6-v2.onnx 08-MAY-26 08.51.35.000000000 AM GMT
SQL>
DBMS_VECTOR.LOAD_ONNX_MODELを使用して、ONNX形式のモデルを DB にインポートします。
SQL> SELECT MODEL_NAME,
2 MINING_FUNCTION,
3 ALGORITHM,
4 ALGORITHM_TYPE,
5 round(MODEL_SIZE/1024/1024) MB
6* FROM user_mining_models;
no rows selected
SQL>
SQL> BEGIN
2 DBMS_VECTOR.LOAD_ONNX_MODEL(
3 'TEMP',
4 'all-MiniLM-L6-v2.onnx',
5 'ALL_MINILM_L6_V2',
6 json('{"function":"embedding","embeddingOutput":"embedding","input":{"input":["DATA"]}}')
7 );
8 END;
9* /
PL/SQL procedure successfully completed.
SQL> SELECT MODEL_NAME,
2 MINING_FUNCTION,
3 ALGORITHM,
4 ALGORITHM_TYPE,
5 round(MODEL_SIZE/1024/1024) MB
6* FROM user_mining_models;
MODEL_NAME MINING_FUNCTION ALGORITHM ALGORITHM_TYPE MB
___________________ __________________ ____________ _________________ _____
ALL_MINILM_L6_V2 EMBEDDING ONNX NATIVE 22
SQL>
サンプル文を Embedding してみました。
vector_dimension_countで次元数を確認すると 384 となり、all-MiniLM-L6-v2 の説明通りです。
SQL> VARIABLE embed_genai_params CLOB
SQL> exec :embed_genai_params := '{"provider": "database", "model": "ALL_MINILM_L6_V2"}';
PL/SQL procedure successfully completed.
SQL> set long 999999999
Warning: This LONG setting may cause Java memory problems.
It is recommended to reduce the setting and/or increase the memory available to Java.
SQL> select dbms_vector.utl_to_embedding('埋め込みモデル、DB内で使うか?DB外で使うか?', json(:embed_genai_params)) as vecdt from dual;
VECDT
_______________________________
[1.29453791E-002,5.56292422E-002,(snip),-2.47316598E-003]
SQL> select vector_dimension_count(dbms_vector.utl_to_embedding('埋め込みモデル、DB内で使うか?DB外で使うか?', json(:embed_genai_params))) as veccnt from dual;
VECCNT
_________
384
SQL>
all-MiniLM-L6-v2
This is a sentence-transformers model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search.
OCI Generative AI Service を使用
OCI Generative AI Service の Offered Model は https://docs.oracle.com/en-us/iaas/Content/generative-ai/pretrained-models.htm から確認可能です。
(日本語マニュアルはモデル名まで翻訳されていて読みづらいので、英語マニュアルの確認がオススメ)
リージョンによっては使用が制限されているモデルもあるため、https://docs.oracle.com/en-us/iaas/Content/generative-ai/model-endpoint-regions.htm も確認しましょう。
本記事作成時点では Cohere, Google Vertex AI Platform, Meta, OpenAI, xAI Platform が Offered Model として記載されています。(埋め込みモデルは Cohere のもののみ)
Cohere の embed-multilingual-v3.0 でサンプル文を Embedding してみました。(ネットワークACL の設定とクレデンシャル (ここでは OCI_CRED) の作成は割愛)
vector_dimension_countで次元数を確認すると 1,024 となり、embed-multilingual-v3.0 の説明通りです。
SQL> exec :embed_genai_params := '{"provider": "ocigenai", "credential_name": "OCI_CRED", "url": "https://inference.generativeai.us-chicago-1.oci.oraclecloud.com/20231130/actions/embedText"
, "model": "cohere.embed-multilingual-v3.0"}';
PL/SQL procedure successfully completed.
SQL>
SQL>
SQL> select dbms_vector.utl_to_embedding('埋め込みモデル、DB内で使うか?DB外で使うか?', json(:embed_genai_params)) as vecdt from dual;
VECDT
_______________________________
[2.43377686E-002,6.53686523E-002,(snip),5.80978394E-003]
SQL> select vector_dimension_count(dbms_vector.utl_to_embedding('埋め込みモデル、DB内で使うか?DB外で使うか?', json(:embed_genai_params))) as vecdt from dual;
VECDT
________
1024
SQL>
Key Features
Works for both English and multilingual.
Model creates a 1,024-dimensional vector for each embedding.
Maximum 96 sentences per run.
Maximum 512 tokens for each input.
Best for use cases when:
Instead of English, the documents are written in one of the supported languages.
The documents are written in more than one language and those languages are one of the supported languages.
サードパーティ・サービス・プロバイダを使用
使用可能なプロバイダは、UTL_TO_EMBEDDINGなど各関数のマニュアルに記載されています。
↓UTL_TO_EMBEDDINGのマニュアルから抜粋

https://docs.oracle.com/cd/G47991_01/vecse/utl_to_embedding-and-utl_to_embeddings-dbms_vector_chain.html
関数やインプットの形式によって使用可能なプロバイダが異なるため、目的のプロバイダ・モデルが使用可能か事前に確認しましょう。
↓UTL_TO_GENERATE_TEXTのマニュアルから抜粋

https://docs.oracle.com/cd/G47991_01/vecse/utl_to_generate_text-dbms_vector_chain.html
OpenAI の text-embedding-3-small でサンプル文を Embedding してみました。(ネットワークACL の設定とクレデンシャル (ここでは OPENAI_CRED) の作成は割愛)
vector_dimension_countで次元数を確認すると 1,536 となり、text-embedding-3-small の説明通りです。
SQL> VARIABLE embed_genai_params CLOB
SQL> exec :embed_genai_params := '{"provider": "openai", "credential_name": "OPENAI_CRED", "url": "https://api.openai.com/v1/embeddings", "model": "text-embedding-3-small"}';
PL/SQL procedure successfully completed.
SQL> select dbms_vector.utl_to_embedding('埋め込みモデル、DB内で使うか?DB外で使うか?', json(:embed_genai_params)) as vecdt from dual;
VECDT
_______________________________
[5.14507294E-004,1.33132935E-002,(snip),-5.83648682E-004]
SQL> select vector_dimension_count(dbms_vector.utl_to_embedding('埋め込みモデル、DB内で使うか?DB外で使うか?', json(:embed_genai_params))) as vecdt from dual;
VECDT
________
1536
SQL>
By default, the length of the embedding vector is 1536 for text-embedding-3-small or 3072 for text-embedding-3-large.
OpenAI側を確認すると、text-embedding-3-small が呼び出されていることがわかります。
データの特性やシステム全体のアーキテクチャ構想を踏まえ、適切な方法を選択しましょう✅



