Jake
Jake
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Worked for cosine search as well
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Legends, that worked for insert 👌🏼
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Assuming I can do the same cast for the insert as well
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Oh okay I’ll try that too when I sit back down
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
function fromSql(value) {
return value.substring(1, value.length - 1).split(',').map((v) => parseFloat(v));
}

function toSql(value) {
return JSON.stringify(value);
}
function fromSql(value) {
return value.substring(1, value.length - 1).split(',').map((v) => parseFloat(v));
}

function toSql(value) {
return JSON.stringify(value);
}
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
I mean the toSql function just called JSON.stringify on the vector, so it seems to be treating it as varchar? The message indicates that the problem is with the expression not with the column type
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
I also ended up with
const value = sql`( ${sql.raw(`'[${vector}]'`)})`;

sql`${sql.ref("embedding")} <=> ${value}`;
const value = sql`( ${sql.raw(`'[${vector}]'`)})`;

sql`${sql.ref("embedding")} <=> ${value}`;
over
cosineDistance("embedding", vector);
cosineDistance("embedding", vector);
again, because the kysely package from pgvector-node uses the toSql function:
function cosineDistance(column, value) {
return sql`${sql.ref(column)} <=> ${toSql(value)}`;
}
function cosineDistance(column, value) {
return sql`${sql.ref(column)} <=> ${toSql(value)}`;
}
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
This is what I ended up with earlier
const insertValue = sql`(
${sql.raw(`'${document.embedding}'`)},
${document.text}
)`;

const result = await sql<Document>`
INSERT INTO embedding (embedding, text)
VALUES ${insertValue}
RETURNING *
`.execute(db);

const row = result.rows[0];
const insertValue = sql`(
${sql.raw(`'${document.embedding}'`)},
${document.text}
)`;

const result = await sql<Document>`
INSERT INTO embedding (embedding, text)
VALUES ${insertValue}
RETURNING *
`.execute(db);

const row = result.rows[0];
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
using
sql`vector(3)`
sql`vector(3)`
is throwing the same error sadly
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Will try that
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Ahh okay that makes sense
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
Will share code when I get home
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
When you say vector isn’t a supported datatype, do you mean at the Postgres level or the kysely level?
30 replies
KKysely
Created by Jake on 3/7/2024 in #help
using pgvector with Kysely
I was able to keep the column as ‘vector’ type actually and use raw sql to do the operations. Only worry is surrounding injections
30 replies