segunda-feira, 18 de novembro de 2024

[ORACLE] Embedding ONNX model for Vector Search tests with Oracle Database 23ai

Hello, everyone.

How's it going?

Today, I'm going to show you how to load an ONNX model into the Oracle 23ai database for your vector search tests. 

But first, what is ONNX?1

Open Neural Network Exchange (ONNX) is an open format built to represent machine learning models. ONNX defines a common set of operators - the building blocks of machine learning and deep learning models - and a common file format to enable AI developers to use models with a variety of frameworks, tools, runtimes, and compilers.

You can find more information about ONNX here.

To begin with, we'll need to download an ONNX model. We can do this here.
  
In my example, I'm use a docker environment to tests and I need to copy the ONNX zip to this environment.

For copy:
docker cp ONNX/all_MiniLM_L12_v2_augmented.zip Orcl23aiORDS:/opt/oracle/DATA_DIR
 

Unzip the file:
 
 

Execute the grants bellow.

sqlplus / as sysdba
alter session set container=freepdb1;
    GRANT DB_DEVELOPER_ROLE, CREATE MINING MODEL TO LAOUC24;
    CREATE OR REPLACE DIRECTORY DATA_DIR AS '/opt/oracle/DATA_DIR';
    GRANT READ,WRITE ON DIRECTORY DATA_DIR TO LAOUC24;
  


Finally, load the model into the database.

sqlplus laouc24/Password123##@FREEPDB1
 
exec DBMS_VECTOR.DROP_ONNX_MODEL (
        model_name => 'ALL_MINILM_L12_V2', 
        force => true
);
 
BEGIN
        DBMS_VECTOR.LOAD_ONNX_MODEL (
                directory => 'DATA_DIR',
                file_name => 'all_MiniLM_L12_v2.onnx',
                model_name => 'ALL_MINILM_L12_V2');
END;
/  
 

 
Check that the model is loaded into the database and run the first test.

SELECT 
    model_name, 
    algorithm, 
    mining_function 
FROM user_mining_models 
WHERE 
     model_name = 'ALL_MINILM_L12_V2';


SELECT 
    VECTOR_EMBEDDING ( 
 ALL_MINILM_L12_V2 USING 'It is a pleasure to be here at LAOUC Tour 2024!' as DATA
    ) AS embedding;


Now you're ready to create your first table, load the data and vectors and start using Vector Search

CREATE TABLE laouc24.vector1 (
        id                      NUMBER PRIMARY KEY,
        name                VARCHAR2(50),
        vector_coord    VECTOR
);

INSERT INTO laouc24.vector1 (id, name, vector_coord) VALUES
        (1, 'Mr. Morcego'           , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. Bat' as data)),
        (2, 'Mr. 7 Mountains'      , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. 7 Mountains' as data)),
        (3, 'Mr. Martin'               , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. Martin' as data)),
        (4, 'Mrs. Maria Padilha' , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mrs. Maria Padilha' as data)),
        (5, 'Mr. Litle Fire'            , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. Litle Fire' as data)),
        (6, 'Mrs. Maria Quiteria' , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mrs. Maria Quiteria' as data)),
        (7, 'Mr. Seven'               , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. Seven' as data)),
        (8, 'Mr. World Spinner'   , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. World Spinner' as data)),
        (9, 'Mr. White Stone'      , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. White Stone' as data)),
        (10, 'Mr. Litle Darkness' , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. Litle Darkness' as data)),
        (11, 'Mr. Marian'             , VECTOR_EMBEDDING(ALL_MINILM_L12_V2 USING 'Mr. Marian' as data));

        
COMMIT;

SELECT * FROM  laouc24.vector1 where id = 4;


And that's it. Your database is ready for testing using Vector Search.

For more examples and information, you can access this posts here and here.

I hope this tip has helped you.


Regards
Mario




Nenhum comentário:

Postar um comentário

Isso te ajudou? Comente...

Postagem em destaque

[ORACLE] Embedding ONNX model for Vector Search tests with Oracle Database 23ai

Hello, everyone. How's it going? Today, I'm going to show you how to load an ONNX model into the Oracle 23ai database for your vecto...