Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini256 committed Feb 29, 2024
1 parent 1a8e8dc commit 912a452
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,51 @@ import { ExpressionBuilder, ReferenceExpression } from 'kysely';

type VectorLike = Float32Array | number[];

export function l1Distance<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
return eb.fn<number>('VEC_L1_DISTANCE', [
column,
eb => eb.val(vectorToSql(value))],
);
}

export function l2Distance<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
return eb.fn<number>('VEC_L2_DISTANCE', [
column,
eb => eb.val(vectorToSql(value))],
);
}

export function negativeInnerProduct<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
return eb.fn<number>('VEC_NEGATIVE_INNER_PRODUCT', [
column,
eb => eb.val(vectorToSql(value))],
);
}

export function innerProduct<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
export function l1Distance<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(
eb: ExpressionBuilder<DB, TB>,
column: RE,
value: VectorLike
) {
return eb.fn<number>('VEC_L1_DISTANCE', [column, (eb) => eb.val(vectorToSql(value))]);
}

export function l2Distance<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(
eb: ExpressionBuilder<DB, TB>,
column: RE,
value: VectorLike
) {
return eb.fn<number>('VEC_L2_DISTANCE', [column, (eb) => eb.val(vectorToSql(value))]);
}

export function negativeInnerProduct<
DB,
TB extends keyof DB,
RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>
>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
return eb.fn<number>('VEC_NEGATIVE_INNER_PRODUCT', [column, (eb) => eb.val(vectorToSql(value))]);
}

export function innerProduct<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(
eb: ExpressionBuilder<DB, TB>,
column: RE,
value: VectorLike
) {
return eb.neg(negativeInnerProduct(eb, column, value));
}

export function cosineDistance<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
return eb.fn<number>('VEC_COSINE_DISTANCE', [
column,
eb => eb.val(vectorToSql(value))],
);
export function cosineDistance<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(
eb: ExpressionBuilder<DB, TB>,
column: RE,
value: VectorLike
) {
return eb.fn<number>('VEC_COSINE_DISTANCE', [column, (eb) => eb.val(vectorToSql(value))]);
}

export function cosineSimilarity<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(eb: ExpressionBuilder<DB, TB>, column: RE, value: VectorLike) {
export function cosineSimilarity<DB, TB extends keyof DB, RE extends ReferenceExpression<DB, TB> = ReferenceExpression<DB, TB>>(
eb: ExpressionBuilder<DB, TB>,
column: RE,
value: VectorLike
) {
return eb(eb.lit<number>(1), '-', cosineDistance(eb, column, value));
}

Expand Down

0 comments on commit 912a452

Please sign in to comment.