Skip to content

Commit a916a75

Browse files
authored
Add files via upload
Files for the blog tutorial
1 parent 1aa4531 commit a916a75

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
REM Creation of tablespace, user and directory
2+
3+
-- connect as user sys to FREEPDB1
4+
5+
-- create tablespace
6+
7+
create bigfile tablespace TBS_VECTOR datafile size 256M autoextend on maxsize 2G;
8+
9+
-- create user with the new role DB_DEVELOPER_ROLE
10+
DROP USER vector_user cascade;
11+
12+
create user vector_user identified by "Oracle_4U"
13+
default tablespace TBS_VECTOR temporary tablespace TEMP
14+
quota unlimited on TBS_VECTOR;
15+
16+
grant create mining model to vector_user;
17+
grant DB_DEVELOPER_ROLE to vector_user;
18+
19+
-- create directory
20+
21+
CREATE OR REPLACE DIRECTORY dm_dump as '&directorypath';
22+
GRANT all ON DIRECTORY dm_dump TO vector_user;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
REM Create an external table and check the data set
2+
3+
-- copy the data set in the directory DM_DUMP
4+
-- Data set is located [here](https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/fro8fl9kuqli/b/AIVECTORS/o/dataset_200K.txt)
5+
6+
-- connect as user VECTOR_USER and create an external table
7+
8+
connect vector_user/Oracle_4U@FREEPDB1
9+
10+
CREATE TABLE if not exists CCNEWS_TMP (sentence VARCHAR2(4000))
11+
ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY dm_dump
12+
ACCESS PARAMETERS
13+
(RECORDS DELIMITED BY 0x'0A'
14+
READSIZE 100000000
15+
FIELDS (sentence CHAR(4000)))
16+
LOCATION (sys.dm_dump:'dataset_200K.txt'))
17+
PARALLEL
18+
REJECT LIMIT UNLIMITED;
19+
20+
-- Check that the external table is correct
21+
22+
select count(*) from CCNEWS_TMP;
23+
24+
-- Check the 4 first rows
25+
26+
select * from CCNEWS_TMP where rownum < 4;
27+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
REM load a vector embedding model into the database: we can load a ONNX model inside the database using DBMS_VECTOR package
2+
3+
/* The ONNX file in this example was generated by Oracle OML4Py utility from the all-MiniLM-L6-v2 sentence transformer model.
4+
It can be downloaded [here](https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/fro8fl9kuqli/b/AIVECTORS/o/all-MiniLM-L6-v2.onnx)
5+
Copy the all-MiniLM-L6-v2.onnx file to the path corresponding to the DM_DUMP directory.*/ 
6+
7+
8+
-- connect to the VECTOR_USER
9+
10+
connect vector_user/Oracle_4U@FREEPDB1
11+
12+
13+
-- load the ONNX model in the database
14+
15+
EXECUTE DBMS_VECTOR.LOAD_ONNX_MODEL('DM_DUMP','all-MiniLM-L6-v2.onnx','doc_model');
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
REM Load the ONNX model into the database with DBMS_VECTOR
2+
3+
connect vector_user/Oracle_4U@FREEPDB1
4+
5+
col model_name format a12
6+
col mining_function format a12
7+
col algorithm format a12
8+
col attribute_name format a20
9+
col data_type format a30
10+
col vector_info format a30
11+
set lines 120
12+
13+
SELECT model_name, mining_function, algorithm,
14+
algorithm_type, model_size
15+
FROM user_mining_models
16+
WHERE model_name = 'DOC_MODEL'
17+
ORDER BY model_name;
18+
19+
20+
SELECT model_name, attribute_name, attribute_type, data_type, vector_info
21+
FROM user_mining_model_attributes
22+
WHERE model_name = 'DOC_MODEL'
23+
ORDER BY attribute_name;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
REM Calculate embeddings directly in the database
2+
3+
/* The ONNX file in this example was generated by Oracle OML4Py utility from the all-MiniLM-L6-v2 sentence transformer model.
4+
It can be downloaded [here](https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/fro8fl9kuqli/b/AIVECTORS/o/all-MiniLM-L6-v2.onnx)
5+
Copy the all-MiniLM-L6-v2.onnx file to the path corresponding to the DM_DUMP directory.*/ 
6+
7+
8+
-- connect to the VECTOR_USER
9+
10+
connect vector_user/Oracle_4U@FREEPDB1
11+
12+
13+
-- create table CCNEWS
14+
15+
create table if not exists CCNEWS (
16+
id number(10) not null,
17+
info VARCHAR2(4000),
18+
vec VECTOR
19+
);
20+
21+
22+
-- Use the doc_model previously loaded to calculate the vector embeddings
23+
-- Please plan additional time to complete this statement
24+
25+
insert into CCNEWS (id, info, vec)
26+
select rownum,
27+
sentence,
28+
TO_VECTOR(VECTOR_EMBEDDING(doc_model USING sentence as data))
29+
from CCNEWS_TMP;
30+
31+
32+
commit;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
REM Use semantic search query using vectors
2+
3+
4+
-- connect to the VECTOR_USER
5+
6+
connect vector_user/Oracle_4U@FREEPDB1
7+
8+
-- use "COSINE" parameter in VECTOR_DISTANCE to calculate the distance between two vectors
9+
10+
set timing on
11+
col info format a90
12+
set lines 120
13+
14+
select id, info
15+
from CCNEWS
16+
order by vector_distance(vec, TO_VECTOR(VECTOR_EMBEDDING(doc_model USING 'little red corvette' as data)), COSINE)
17+
fetch approx first 5 rows only;
18+
19+
20+
-- Check the execution plan
21+
22+
set autotrace traceonly explain
23+
24+
select id, info
25+
from CCNEWS
26+
order by vector_distance(vec, TO_VECTOR(VECTOR_EMBEDDING(doc_model USING 'little red corvette' as data)), COSINE)
27+
fetch approx first 5 rows only;

0 commit comments

Comments
 (0)