-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const utils = require('./utils'); | ||
|
||
class Vector { | ||
constructor(vec) { | ||
if (Array.isArray(vec)) { | ||
this.vec = vec; | ||
} else { | ||
this.vec = utils.fromSql(vec); | ||
} | ||
} | ||
|
||
toArray() { | ||
return this.vec; | ||
} | ||
|
||
toPostgres() { | ||
return this.toString(); | ||
} | ||
|
||
toString() { | ||
return utils.toSql(this.vec); | ||
} | ||
} | ||
|
||
function vector(vec) { | ||
return new Vector(vec); | ||
} | ||
|
||
module.exports = {vector}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
const { sql } = require('kysely'); | ||
const { fromSql, toSql } = require('../utils'); | ||
const { vector } = require('..'); | ||
|
||
function l2Distance(column, value) { | ||
return sql`${sql.ref(column)} <-> ${toSql(value)}`; | ||
return sql`${sql.ref(column)} <-> ${vector(value)}`; | ||
} | ||
|
||
function maxInnerProduct(column, value) { | ||
return sql`${sql.ref(column)} <#> ${toSql(value)}`; | ||
return sql`${sql.ref(column)} <#> ${vector(value)}`; | ||
} | ||
|
||
function cosineDistance(column, value) { | ||
return sql`${sql.ref(column)} <=> ${toSql(value)}`; | ||
return sql`${sql.ref(column)} <=> ${vector(value)}`; | ||
} | ||
|
||
module.exports = {fromSql, toSql, l2Distance, maxInnerProduct, cosineDistance}; | ||
module.exports = {vector, l2Distance, maxInnerProduct, cosineDistance}; |
This file contains 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