This README outlines the steps to load the HuggingFace sentence-transformers model 'multilingual-e5-small' into an Oracle Database 23ai on-premises or Oracle Database cloud instance. This model is augmented to include pre- and post-processing steps for producing embeddings using the in-database ONNX Runtime engine.

Instructions for loading the augmented multilingual-e5-small model into Oracle Database. In this example, we are logging into a pluggable database named ORCLPDB.

1. Unzip the file

$ unzip multilingual_e5_small_augmented.zip
Archive:  multilingual_e5_small_augmented.zip
   creating: multilingual_e5_small_augmented/
  inflating: multilingual_e5_small_augmented/multilingual_e5_small.onnx  
  inflating: multilingual_e5_small_augmented/README_MULTILINGUAL_E5_SMALL_augmented.txt

$ sqlplus / as sysdba;

SQL> alter session set container=ORCLPDB;

3. Apply grants and define the data dump directory as the path where the ONNX model was unzipped. 

Note, in this example, we are using the OMLUSER schema. Replace OMLUSER with your schema name.

SQL> GRANT DB_DEVELOPER_ROLE, CREATE MINING MODEL TO OMLUSER;
SQL> CREATE OR REPLACE DIRECTORY DM_DUMP AS '<path to ONNX model>';
SQL> GRANT READ ON DIRECTORY DM_DUMP TO OMLUSER;
SQL> GRANT WRITE ON DIRECTORY DM_DUMP TO OMLUSER;
SQL> exit

4. Log into your schema.

$ sqlplus omluser/omluser@ORCLPDB;

Load the ONNX model. Optionally drop the model first if a model with the same name already exists in the database.

SQL> exec DBMS_VECTOR.DROP_ONNX_MODEL(model_name => 'MULTILINGUAL_E5_SMALL', force => true);

BEGIN
   DBMS_VECTOR.LOAD_ONNX_MODEL(
        directory => 'DM_DUMP',
		file_name => 'multilingual_e5_small.onnx',
        model_name => 'MULTILINGUAL_E5_SMALL',
        metadata => JSON('{"function" : "embedding", "embeddingOutput" : "embedding", "input": {"input": ["DATA"]}}'));
END;
/

PL/SQL procedure successfully completed.

5. Validate that the model was imported to the database.

SQL> SELECT model_name, algorithm, mining_function from user_mining_models WHERE model_name='MULTILINGUAL_E5_SMALL';

MODEL_NAME         ALGORITHM     MINING_FUNCTION
----------         ---------     ---------------
MULTILINGUAL       ONNX          EMBEDDING

6. Generate embedding vectors using the VECTOR_EMBEDDING SQL scoring function. Partial output is shown below.

SQL> SELECT VECTOR_EMBEDDING(MULTILINGUAL_E5_SMALL USING 'la veloce volpe marrone saltò' as DATA) AS embedding;

EMBEDDING
--------------------------------------------------------------------------
[7.07456172E-002,-4.48292121E-003,-2.73464732E-002,-7.10269287E-002,6.6634208E-002,-3.21255997E-002,2.24859919E-002,-5.96923474E-003,3.45743485E-002,-2.03785393E-002,5.54870293E-002,5.00155427E-003,4.81907576E-002,...

7. An Alternate method to import ONNX Models is to use the DBMS_DATA_MINING.IMPORT_ONNX_MODEL procedure. 

This example sets up a BLOB object and a BFILE locator, creates a temporary BLOB for storing the ONNX file from the DM_DUMP directory, and reads its contents into the BLOB. It then closes the file and uses the content to import an ONNX model into the database with specified metadata, before releasing the temporary BLOB resources.

SQL> exec DBMS_VECTOR.DROP_ONNX_MODEL(model_name => 'MULTILINGUAL_E5_SMALL', force => true);

DECLARE
    m_blob BLOB default empty_blob();
    m_src_loc BFILE ;
    BEGIN
    DBMS_LOB.createtemporary (m_blob, FALSE);
    m_src_loc := BFILENAME('DM_DUMP', 'multilingual_e5_small.onnx');
    DBMS_LOB.fileopen (m_src_loc, DBMS_LOB.file_readonly);
    DBMS_LOB.loadfromfile (m_blob, m_src_loc, DBMS_LOB.getlength (m_src_loc));
    DBMS_LOB.CLOSE(m_src_loc);
    DBMS_DATA_MINING.import_onnx_model ('MULTILINGUAL_E5_SMALL', 
                                        m_blob, 
                                        JSON('{"function":"embedding", "embeddingOutput":"embedding", "input":{"input": ["DATA"]}}'));
    DBMS_LOB.freetemporary (m_blob);
    END;
    /

Follow steps 5 and 6 above to validate the model was imported to the database and to generate embedding vectors.

For more information, refer to the Oracle AI Vector Search User's Guide
https://docs.oracle.com/en/database/oracle/oracle-database/23/vecse/import-onnx-models-oracle-database-end-end-example.html