Last active
September 17, 2023 04:49
-
-
Save gongfan99/398a027460ffe2a1bfe1fffdb6b4d232 to your computer and use it in GitHub Desktop.
save embeddings as BLOB in MySQL with Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createPool } from 'mysql2/promise' | |
import 'dotenv/config' | |
const pool = createPool({ | |
host: process.env.DB_HOST, | |
user: process.env.DB_USER, | |
password: process.env.DB_PASSWORD, | |
database: process.env.DB_DATABASE, | |
waitForConnections: true, | |
connectionLimit: 5, | |
idleTimeout: 10000, // idle connections timeout, in milliseconds, the default value 60000 | |
queueLimit: 10 // 0 means no limit for queue length | |
}); | |
async function main() { | |
try { | |
const embeddings = [0.6, 0.8]; | |
// save embeddings as BLOB into the example_table | |
const arr = Float32Array.from(embeddings); | |
const buf = Buffer.from(arr.buffer, arr.byteOffset, arr.byteLength); | |
let [result] = await pool.execute('INSERT INTO example_table (embeddings) VALUES (?)', [buf]); | |
const id = result.insertId; | |
// read back embeddings from the table | |
let [rows] = await pool.execute('SELECT * FROM example_table WHERE id = ?', [id]); | |
const buf2 = rows[0].embeddings; // buf2 is a Buffer; Buffer can be treated as Uint8Array for most purposes | |
const arr2 = new Float32Array(buf2.buffer, buf2.byteOffset, Math.floor(buf2.length/Float32Array.BYTES_PER_ELEMENT)); | |
} catch(e) { | |
console.log(e) | |
} finally { | |
await pool.end(); | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment