Skip to content

Commit b1aed9d

Browse files
Merge pull request #1173 from oracle-devrel/uschwinn-patch-23
Uschwinn patch 23
2 parents 1aa4531 + 7ddd84d commit b1aed9d

File tree

7 files changed

+141
-1
lines changed

7 files changed

+141
-1
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+
-- dataset_200K.txt is located in: 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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
/*
4+
The ONNX file in this example was generated by Oracle OML4Py utility from the all-MiniLM-L6-v2 sentence transformer model.
5+
It can be downloaded from https://objectstorage.eu-frankfurt-1.oraclecloud.com/n/fro8fl9kuqli/b/AIVECTORS/o/all-MiniLM-L6-v2.onnx
6+
Copy the all-MiniLM-L6-v2.onnx file to the path corresponding to the DM_DUMP directory.
7+
*/ 
8+
9+
-- connect to the VECTOR_USER
10+
11+
connect vector_user/Oracle_4U@FREEPDB1
12+
13+
14+
-- load the ONNX model in the database
15+
16+
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
REM Calculate embeddings directly in the database
2+
3+
-- connect to the VECTOR_USER
4+
5+
connect vector_user/Oracle_4U@FREEPDB1
6+
7+
-- create table CCNEWS
8+
create table if not exists CCNEWS (
9+
id number(10) not null,
10+
info VARCHAR2(4000),
11+
vec VECTOR
12+
);
13+
14+
15+
-- Use the doc_model previously loaded to calculate the vector embeddings
16+
-- Please plan additional time to complete this statement
17+
18+
insert into CCNEWS (id, info, vec)
19+
select rownum,
20+
sentence,
21+
TO_VECTOR(VECTOR_EMBEDDING(doc_model USING sentence as data))
22+
from CCNEWS_TMP;
23+
24+
25+
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;

data-platform/core-converged-db/ai-vector-search/files/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Use the scripts to learn basics about AI Vector Search.
1010

1111
# How to use this asset?
1212

13-
See the README in the files folder.
13+
Please use the posting [Getting started with vectors in 23ai](https://blogs.oracle.com/coretec/post/getting-started-with-vectors-in-23ai) for information about the scripts.
1414

1515
# Useful Links
1616

0 commit comments

Comments
 (0)