| 1 | // Copyright 2023 Lance Developers. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import { type Float } from 'apache-arrow' |
| 16 | |
| 17 | /** |
| 18 | * An embedding function that automatically creates vector representation for a given column. |
| 19 | */ |
| 20 | export interface EmbeddingFunction<T> { |
| 21 | /** |
| 22 | * The name of the column that will be used as input for the Embedding Function. |
| 23 | */ |
| 24 | sourceColumn: string |
| 25 | |
| 26 | /** |
| 27 | * The data type of the embedding |
| 28 | * |
| 29 | * The embedding function should return `number`. This will be converted into |
| 30 | * an Arrow float array. By default this will be Float32 but this property can |
| 31 | * be used to control the conversion. |
| 32 | */ |
| 33 | embeddingDataType?: Float |
| 34 | |
| 35 | /** |
| 36 | * The dimension of the embedding |
| 37 | * |
| 38 | * This is optional, normally this can be determined by looking at the results of |
| 39 | * `embed`. If this is not specified, and there is an attempt to apply the embedding |
| 40 | * to an empty table, then that process will fail. |
| 41 | */ |
| 42 | embeddingDimension?: number |
| 43 | |
| 44 | /** |
| 45 | * The name of the column that will contain the embedding |
| 46 | * |
| 47 | * By default this is "vector" |
| 48 | */ |
| 49 | destColumn?: string |
| 50 | |
| 51 | /** |
| 52 | * Should the source column be excluded from the resulting table |
| 53 | * |
| 54 | * By default the source column is included. Set this to true and |
| 55 | * only the embedding will be stored. |
| 56 | */ |
| 57 | excludeSource?: boolean |
| 58 | |
| 59 | /** |
| 60 | * Creates a vector representation for the given values. |
| 61 | */ |
| 62 | embed: (data: T[]) => Promise<number[][]> |
| 63 | } |
| 64 | |
| 65 | export function isEmbeddingFunction<T> (value: any): value is EmbeddingFunction<T> { |
| 66 | return typeof value.sourceColumn === 'string' && |
| 67 | typeof value.embed === 'function' |
| 68 | } |