UNPKG

genkitx-cloud-sql-pg

Version:

Genkit AI framework plugin for Cloud SQL for PostgreSQL.

172 lines 5.12 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var indexes_exports = {}; __export(indexes_exports, { BaseIndex: () => BaseIndex, DEFAULT_DISTANCE_STRATEGY: () => DEFAULT_DISTANCE_STRATEGY, DEFAULT_INDEX_NAME_SUFFIX: () => DEFAULT_INDEX_NAME_SUFFIX, DistanceStrategy: () => DistanceStrategy, ExactNearestNeighbor: () => ExactNearestNeighbor, HNSWIndex: () => HNSWIndex, HNSWQueryOptions: () => HNSWQueryOptions, IVFFlatIndex: () => IVFFlatIndex, IVFFlatQueryOptions: () => IVFFlatQueryOptions, QueryOptions: () => QueryOptions }); module.exports = __toCommonJS(indexes_exports); 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}`; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BaseIndex, DEFAULT_DISTANCE_STRATEGY, DEFAULT_INDEX_NAME_SUFFIX, DistanceStrategy, ExactNearestNeighbor, HNSWIndex, HNSWQueryOptions, IVFFlatIndex, IVFFlatQueryOptions, QueryOptions }); //# sourceMappingURL=indexes.js.map