UNPKG

genkitx-cloud-sql-pg

Version:

Genkit AI framework plugin for Cloud SQL for PostgreSQL.

1 lines 7.75 kB
{"version":3,"sources":["../src/indexes.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nclass StrategyMixin {\n operator: string;\n searchFunction: string;\n indexFunction: string;\n\n constructor(operator: string, searchFunction: string, indexFunction: string) {\n this.operator = operator;\n this.searchFunction = searchFunction;\n this.indexFunction = indexFunction;\n }\n}\n\n/**\n * Enumerator of the Distance strategies.\n */\nexport class DistanceStrategy extends StrategyMixin {\n public static EUCLIDEAN = new StrategyMixin(\n '<->',\n 'l2_distance',\n 'vector_l2_ops'\n );\n public static COSINE_DISTANCE = new StrategyMixin(\n '<=>',\n 'cosine_distance',\n 'vector_cosine_ops'\n );\n public static INNER_PRODUCT = new StrategyMixin(\n '<#>',\n 'inner_product',\n 'vector_ip_ops'\n );\n}\n\n/**\n * The default distance strategy used for vector comparisons.\n * @type {DistanceStrategy}\n */\nexport const DEFAULT_DISTANCE_STRATEGY = DistanceStrategy.COSINE_DISTANCE;\n\n/**\n * The default suffix appended to index names.\n * @type {string}\n */\nexport const DEFAULT_INDEX_NAME_SUFFIX: string = 'genkitvectorindex';\n\n/**\n * Defines the base arguments for configuring a vector index.\n */\nexport interface BaseIndexArgs {\n name?: string;\n distanceStrategy?: DistanceStrategy;\n partialIndexes?: string[];\n}\n\n/**\n * Abstract base class for defining vector indexes.\n */\nexport abstract class BaseIndex {\n name?: string;\n indexType: string;\n distanceStrategy: DistanceStrategy;\n partialIndexes?: string[];\n\n /**\n * Constructs a new BaseIndex instance.\n * @param {string} [name] - The optional name of the index.\n * @param {string} [indexType='base'] - The type of the index. Defaults to 'base'.\n * @param {DistanceStrategy} [distanceStrategy=DistanceStrategy.COSINE_DISTANCE] - The distance strategy. Defaults to COSINE_DISTANCE.\n * @param {string[]} [partialIndexes] - Optional array of partial index definitions.\n */\n constructor(\n name?: string,\n indexType: string = 'base',\n distanceStrategy: DistanceStrategy = DistanceStrategy.COSINE_DISTANCE,\n partialIndexes?: string[]\n ) {\n this.name = name;\n this.indexType = indexType;\n this.distanceStrategy = distanceStrategy;\n this.partialIndexes = partialIndexes;\n }\n\n /**\n * Set index query options for vector store initialization.\n */\n abstract indexOptions(): string;\n}\n\n/**\n * Represents an Exact Nearest Neighbor (ENN) index.\n * This index type typically performs a brute-force search.\n */\nexport class ExactNearestNeighbor extends BaseIndex {\n constructor(baseArgs?: BaseIndexArgs) {\n super(\n baseArgs?.name,\n 'exactnearestneighbor',\n baseArgs?.distanceStrategy,\n baseArgs?.partialIndexes\n );\n }\n\n indexOptions(): string {\n throw new Error('indexOptions method must be implemented by subclass');\n }\n}\n\n/**\n * Represents a Hierarchical Navigable Small World (HNSW) index.\n * HNSW is an approximate nearest neighbor (ANN) algorithm.\n */\nexport class HNSWIndex extends BaseIndex {\n m: number;\n efConstruction: number;\n\n /**\n * Constructs a new HNSWIndex instance.\n * @param {BaseIndexArgs} [baseArgs] - Optional base arguments for the index.\n * @param {number} [m=16] - The 'm' parameter for HNSW. Defaults to 16.\n * @param {number} [efConstruction=64] - The 'ef_construction' parameter for HNSW. Defaults to 64.\n */\n constructor(baseArgs?: BaseIndexArgs, m?: number, efConstruction?: number) {\n super(\n baseArgs?.name,\n 'hnsw',\n baseArgs?.distanceStrategy,\n baseArgs?.partialIndexes\n );\n this.m = m ?? 16;\n this.efConstruction = efConstruction ?? 64;\n }\n\n indexOptions(): string {\n return `(m = ${this.m}, ef_construction = ${this.efConstruction})`;\n }\n}\n\n/**\n * Represents an Inverted File Index (IVFFlat) index.\n * IVFFlat is an approximate nearest neighbor (ANN) algorithm.\n */\nexport class IVFFlatIndex extends BaseIndex {\n lists: number;\n\n /**\n * Constructs a new IVFFlatIndex instance.\n * @param {BaseIndexArgs} baseArgs - Base arguments for the index.\n * @param {number} [lists=100] - The number of lists for IVF-Flat. Defaults to 100.\n */\n constructor(baseArgs: BaseIndexArgs, lists?: number) {\n super(\n baseArgs?.name,\n 'ivfflat',\n baseArgs?.distanceStrategy,\n baseArgs?.partialIndexes\n );\n this.lists = lists ?? 100;\n }\n\n indexOptions(): string {\n return `(lists = ${this.lists})`;\n }\n}\n\n/**\n * Convert index attributes to string.\n * Must be implemented by subclasses.\n */\nexport abstract class QueryOptions {\n abstract to_string(): string;\n}\n\n/**\n * Represents query options for an HNSW index.\n */\nexport class HNSWQueryOptions extends QueryOptions {\n efSearch: number;\n\n constructor(efSearch?: number) {\n super();\n this.efSearch = efSearch ?? 40;\n }\n\n to_string(): string {\n return `hnsw.ef_search = ${this.efSearch}`;\n }\n}\n\n/**\n * Represents query options for an IVF-Flat index.\n */\nexport class IVFFlatQueryOptions extends QueryOptions {\n readonly probes: number;\n\n constructor(probes?: number) {\n super();\n this.probes = probes ?? 1;\n }\n\n to_string(): string {\n return `ivflfat.probes = ${this.probes}`;\n }\n}\n"],"mappings":"AAgBA,MAAM,cAAc;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EAEA,YAAY,UAAkB,gBAAwB,eAAuB;AAC3E,SAAK,WAAW;AAChB,SAAK,iBAAiB;AACtB,SAAK,gBAAgB;AAAA,EACvB;AACF;AAKO,MAAM,yBAAyB,cAAc;AAAA,EAClD,OAAc,YAAY,IAAI;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,OAAc,kBAAkB,IAAI;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AAAA,EACA,OAAc,gBAAgB,IAAI;AAAA,IAChC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;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