// set up Nile as the vector store
const { Nile } = await import("@niledatabase/server");
const NILEDB_USER = "you need a nile user with access to a nile database";
const NILEDB_PASSWORD = "and a password for that user";
const nile = Nile({
user: NILEDB_USER,
password: NILEDB_PASSWORD,
});
// store vector in a table
await nile.db.query("INSERT INTO embeddings (embedding) values ($1)", [
JSON.stringify(doc_vec.map((v) => Number(v))),
]);
// search for similar vectors
let db_resp = await nile.db.query(
"select embedding from embeddings order by embedding<=>$1 limit 1",
[JSON.stringify(question_vec.map((v) => Number(v)))]
);
// Postgres returns an object, with array of rows each column is a
// property of the row the vector is represented as a string
let similar_str = db_resp.rows[0].embedding;
let similar = similar_str
.substring(1, similar_str.length - 1)
.split(",")
.map((v) => parseFloat(v));
// check that we got the same vector back
let same = true;
for (let i = 0; i < similar.length; i++) {
if (Math.abs(similar[i] - doc_vec[i]) > 0.000001) {
same = false;
}
}
console.log("got same vector? " + same);