|
| 1 | +/* Copyright (c) 2024, Oracle and/or its affiliates. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * This software is dual-licensed to you under the Universal Permissive License |
| 6 | + * (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 7 | + * 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 8 | + * either license. |
| 9 | + * |
| 10 | + * If you elect to accept the software under the Apache License, Version 2.0, |
| 11 | + * the following applies: |
| 12 | + * |
| 13 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 | + * you may not use this file except in compliance with the License. |
| 15 | + * You may obtain a copy of the License at |
| 16 | + * |
| 17 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 18 | + * |
| 19 | + * Unless required by applicable law or agreed to in writing, software |
| 20 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 21 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | + * See the License for the specific language governing permissions and |
| 23 | + * limitations under the License. |
| 24 | + * |
| 25 | + * NAME |
| 26 | + * vectortype2.js |
| 27 | + * |
| 28 | + * DESCRIPTION |
| 29 | + * Insert data into VECTOR columns and verify vector operations. |
| 30 | + * |
| 31 | + *****************************************************************************/ |
| 32 | + |
| 33 | +'use strict'; |
| 34 | + |
| 35 | +Error.stackTraceLimit = 50; |
| 36 | + |
| 37 | + |
| 38 | +const oracledb = require('oracledb'); |
| 39 | +const dbConfig = require('./dbconfig.js'); |
| 40 | +const tableName = 'testvectornodejs2'; |
| 41 | +const assert = require('assert'); |
| 42 | + |
| 43 | +if (process.env.NODE_ORACLEDB_DRIVER_MODE === 'thick') { |
| 44 | + let clientOpts = {}; |
| 45 | + // On Windows and macOS Intel platforms, set the environment |
| 46 | + // variable NODE_ORACLEDB_CLIENT_LIB_DIR to the Oracle Client library path |
| 47 | + if (process.platform === 'win32' || (process.platform === 'darwin' && process.arch === 'x64')) { |
| 48 | + clientOpts = { libDir: process.env.NODE_ORACLEDB_CLIENT_LIB_DIR }; |
| 49 | + } |
| 50 | + oracledb.initOracleClient(clientOpts); // enable node-oracledb Thick mode |
| 51 | +} |
| 52 | + |
| 53 | +oracledb.outFormat = oracledb.OUT_FORMAT_OBJECT; |
| 54 | + |
| 55 | +// By Default typed arrays are returned. output fetch handler like |
| 56 | +// below can be used to convert to Array objects. |
| 57 | +oracledb.fetchTypeHandler = function(metadata) { |
| 58 | + if (metadata.dbType === oracledb.DB_TYPE_VECTOR) { |
| 59 | + const myConverter = (v) => { |
| 60 | + if (v !== null) { |
| 61 | + return Array.from(v); |
| 62 | + } |
| 63 | + return v; |
| 64 | + }; |
| 65 | + return {converter: myConverter}; |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +async function run() { |
| 70 | + |
| 71 | + const connection = await oracledb.getConnection(dbConfig); |
| 72 | + |
| 73 | + try { |
| 74 | + let result; |
| 75 | + |
| 76 | + const serverVersion = connection.oracleServerVersion; |
| 77 | + if (serverVersion < 2304000000) { |
| 78 | + console.log(`DB version ${serverVersion} does not support VECTOR type`); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + console.log('Creating table'); |
| 83 | + await connection.execute(`DROP TABLE if exists ${tableName}`); |
| 84 | + await connection.execute(`CREATE TABLE ${tableName} (id NUMBER, |
| 85 | + embedding VECTOR(3))`); |
| 86 | + |
| 87 | + let i = 0; |
| 88 | + const binds = [], num = 4; |
| 89 | + const expectedArrays = [ |
| 90 | + [1, 2, 3], |
| 91 | + [4, 5, 6], |
| 92 | + [42, 52, 613], |
| 93 | + [-1, -2, -3] |
| 94 | + ]; |
| 95 | + for (i = 0; i < num; i++) { |
| 96 | + binds.push({id: i, vec: expectedArrays[i]}); |
| 97 | + } |
| 98 | + |
| 99 | + console.log('Inserting Rows ', binds); |
| 100 | + const options = { |
| 101 | + bindDefs: { |
| 102 | + id: { type: oracledb.DB_TYPE_NUMBER }, |
| 103 | + vec: { type: oracledb.DB_TYPE_VECTOR } |
| 104 | + } |
| 105 | + }; |
| 106 | + result = await connection.executeMany(`insert into ${tableName} values(:id, :vec)`, binds, options); |
| 107 | + console.log('Rows inserted: ' + result.rowsAffected); |
| 108 | + |
| 109 | + console.log('Query Results and Verify values returned:'); |
| 110 | + result = await connection.execute( |
| 111 | + `select id, embedding from ${tableName} ORDER BY id`); |
| 112 | + console.log(result.rows); |
| 113 | + for (i = 0; i < num; i++) { |
| 114 | + assert.deepStrictEqual(result.rows[i], {ID: i, EMBEDDING: expectedArrays[i]}); |
| 115 | + } |
| 116 | + |
| 117 | + // Calculate distance to a given embedding |
| 118 | + const vecQuery = new Float64Array([3, 1, 2]); |
| 119 | + let expectedValues = [6, 33, 377443, 50]; |
| 120 | + result = await connection.execute( |
| 121 | + `select vector_distance (embedding, :1) from ${tableName}`, [vecQuery]); |
| 122 | + console.log(`Distance from Input Embedding `, [3, 1, 2]); |
| 123 | + console.log(result.rows); |
| 124 | + for (i = 0; i < num; i++) { |
| 125 | + assert.deepStrictEqual(result.rows[i], {"VECTOR_DISTANCE(EMBEDDING,:1)": expectedValues[i]}); |
| 126 | + } |
| 127 | + |
| 128 | + // Calculate top 3 similarity search to a given embedding |
| 129 | + result = await connection.execute( |
| 130 | + `select embedding, vector_distance (embedding, :1) as distance from ${tableName} |
| 131 | + order by distance FETCH FIRST 3 ROWS ONLY`, [vecQuery]); |
| 132 | + console.log(`Top 3 vectors for Input Embedding `, [3, 1, 2]); |
| 133 | + console.log(result.rows); |
| 134 | + assert.deepStrictEqual(result.rows[0].EMBEDDING, expectedArrays[0]); |
| 135 | + assert.deepStrictEqual(result.rows[1].EMBEDDING, expectedArrays[1]); |
| 136 | + assert.deepStrictEqual(result.rows[2].EMBEDDING, expectedArrays[3]); |
| 137 | + |
| 138 | + // Nearest Neighbours (distance < 34) for a given embedding [3,1,2] |
| 139 | + // gives [1,2,3] and [4,5,6] |
| 140 | + console.log('Nearest Neighbours with distance < 34:'); |
| 141 | + result = await connection.execute( |
| 142 | + `select * from ${tableName} where vector_distance (embedding, vector('[3,1,2]', 3)) < 34 `); |
| 143 | + console.log(result.rows); |
| 144 | + assert.deepStrictEqual(result.rows[0].EMBEDDING, expectedArrays[0]); |
| 145 | + assert.deepStrictEqual(result.rows[1].EMBEDDING, expectedArrays[1]); |
| 146 | + |
| 147 | + // Cosine Distance |
| 148 | + result = await connection.execute( |
| 149 | + `select cosine_distance (embedding, vector('[3,1,2]', 3)) as cosdistance from ${tableName}`); |
| 150 | + expectedValues = [0.2142857142857143, 0.11673988938389968, 0.3914785344302253, 1.7857142857142856]; |
| 151 | + for (i = 0; i < num; i++) { |
| 152 | + assert.deepStrictEqual(result.rows[i], {"COSDISTANCE": expectedValues[i]}); |
| 153 | + } |
| 154 | + console.log(`Cosine Distance from Input Embedding `, [3, 1, 2]); |
| 155 | + console.log(result.rows); |
| 156 | + |
| 157 | + // inner product |
| 158 | + result = await connection.execute( |
| 159 | + `select inner_product (embedding, vector('[3,1,2]', 3)) from ${tableName}`); |
| 160 | + console.log(`Inner product with Input Embedding `, [3, 1, 2]); |
| 161 | + console.log(result.rows); |
| 162 | + expectedValues = [11, 29, 1404, -11]; |
| 163 | + for (i = 0; i < num; i++) { |
| 164 | + assert.deepStrictEqual(result.rows[i], {"INNER_PRODUCT(EMBEDDING,VECTOR('[3,1,2]',3))": expectedValues[i]}); |
| 165 | + } |
| 166 | + } catch (err) { |
| 167 | + console.error(err); |
| 168 | + } finally { |
| 169 | + if (connection) { |
| 170 | + try { |
| 171 | + await connection.close(); |
| 172 | + } catch (err) { |
| 173 | + console.error(err); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +run(); |
0 commit comments