genkitx-cloud-sql-pg
Version:
Genkit AI framework plugin for Cloud SQL for PostgreSQL.
139 lines • 3.69 kB
JavaScript
class StrategyMixin {
operator;
searchFunction;
indexFunction;
constructor(operator, searchFunction, indexFunction) {
this.operator = operator;
this.searchFunction = searchFunction;
this.indexFunction = indexFunction;
}
}
class DistanceStrategy extends StrategyMixin {
static EUCLIDEAN = new StrategyMixin(
"<->",
"l2_distance",
"vector_l2_ops"
);
static COSINE_DISTANCE = new StrategyMixin(
"<=>",
"cosine_distance",
"vector_cosine_ops"
);
static INNER_PRODUCT = new StrategyMixin(
"<#>",
"inner_product",
"vector_ip_ops"
);
}
const DEFAULT_DISTANCE_STRATEGY = DistanceStrategy.COSINE_DISTANCE;
const DEFAULT_INDEX_NAME_SUFFIX = "genkitvectorindex";
class BaseIndex {
name;
indexType;
distanceStrategy;
partialIndexes;
/**
* Constructs a new BaseIndex instance.
* @param {string} [name] - The optional name of the index.
* @param {string} [indexType='base'] - The type of the index. Defaults to 'base'.
* @param {DistanceStrategy} [distanceStrategy=DistanceStrategy.COSINE_DISTANCE] - The distance strategy. Defaults to COSINE_DISTANCE.
* @param {string[]} [partialIndexes] - Optional array of partial index definitions.
*/
constructor(name, indexType = "base", distanceStrategy = DistanceStrategy.COSINE_DISTANCE, partialIndexes) {
this.name = name;
this.indexType = indexType;
this.distanceStrategy = distanceStrategy;
this.partialIndexes = partialIndexes;
}
}
class ExactNearestNeighbor extends BaseIndex {
constructor(baseArgs) {
super(
baseArgs?.name,
"exactnearestneighbor",
baseArgs?.distanceStrategy,
baseArgs?.partialIndexes
);
}
indexOptions() {
throw new Error("indexOptions method must be implemented by subclass");
}
}
class HNSWIndex extends BaseIndex {
m;
efConstruction;
/**
* Constructs a new HNSWIndex instance.
* @param {BaseIndexArgs} [baseArgs] - Optional base arguments for the index.
* @param {number} [m=16] - The 'm' parameter for HNSW. Defaults to 16.
* @param {number} [efConstruction=64] - The 'ef_construction' parameter for HNSW. Defaults to 64.
*/
constructor(baseArgs, m, efConstruction) {
super(
baseArgs?.name,
"hnsw",
baseArgs?.distanceStrategy,
baseArgs?.partialIndexes
);
this.m = m ?? 16;
this.efConstruction = efConstruction ?? 64;
}
indexOptions() {
return `(m = ${this.m}, ef_construction = ${this.efConstruction})`;
}
}
class IVFFlatIndex extends BaseIndex {
lists;
/**
* Constructs a new IVFFlatIndex instance.
* @param {BaseIndexArgs} baseArgs - Base arguments for the index.
* @param {number} [lists=100] - The number of lists for IVF-Flat. Defaults to 100.
*/
constructor(baseArgs, lists) {
super(
baseArgs?.name,
"ivfflat",
baseArgs?.distanceStrategy,
baseArgs?.partialIndexes
);
this.lists = lists ?? 100;
}
indexOptions() {
return `(lists = ${this.lists})`;
}
}
class QueryOptions {
}
class HNSWQueryOptions extends QueryOptions {
efSearch;
constructor(efSearch) {
super();
this.efSearch = efSearch ?? 40;
}
to_string() {
return `hnsw.ef_search = ${this.efSearch}`;
}
}
class IVFFlatQueryOptions extends QueryOptions {
probes;
constructor(probes) {
super();
this.probes = probes ?? 1;
}
to_string() {
return `ivflfat.probes = ${this.probes}`;
}
}
export {
BaseIndex,
DEFAULT_DISTANCE_STRATEGY,
DEFAULT_INDEX_NAME_SUFFIX,
DistanceStrategy,
ExactNearestNeighbor,
HNSWIndex,
HNSWQueryOptions,
IVFFlatIndex,
IVFFlatQueryOptions,
QueryOptions
};
//# sourceMappingURL=indexes.mjs.map