|
| 1 | +declare namespace Supabase { |
| 2 | + /** |
| 3 | + * Provides AI related APIs |
| 4 | + */ |
| 5 | + export interface Ai { |
| 6 | + /** Provides an user friendly interface for the low level *onnx backend API*. |
| 7 | + * A `RawSession` can execute any *onnx* model, but we only recommend it for `tabular` or *self-made* models, where you need mode control of model execution and pre/pos-processing. |
| 8 | + * Consider a high-level implementation like `@huggingface/transformers.js` for generic tasks like `nlp`, `computer-vision` or `audio`. |
| 9 | + * |
| 10 | + * **Example:** |
| 11 | + * ```typescript |
| 12 | + * const session = await RawSession.fromHuggingFace('Supabase/gte-small'); |
| 13 | + * // const session = await RawSession.fromUrl("https://example.com/model.onnx"); |
| 14 | + * |
| 15 | + * // Prepare the input tensors |
| 16 | + * const inputs = { |
| 17 | + * input1: new Tensor("float32", [1.0, 2.0, 3.0], [3]), |
| 18 | + * input2: new Tensor("float32", [4.0, 5.0, 6.0], [3]), |
| 19 | + * }; |
| 20 | + * |
| 21 | + * // Run the model |
| 22 | + * const outputs = await session.run(inputs); |
| 23 | + * |
| 24 | + * console.log(outputs.output1); // Output tensor |
| 25 | + * ``` |
| 26 | + */ |
| 27 | + readonly RawSession: typeof RawSession; |
| 28 | + |
| 29 | + /** A low level representation of model input/output. |
| 30 | + * Supabase's `Tensor` is totally compatible with `@huggingface/transformers.js`'s `Tensor`. It means that you can use its high-level API to apply some common operations like `sum()`, `min()`, `max()`, `normalize()` etc... |
| 31 | + * |
| 32 | + * **Example: Generating embeddings from scratch** |
| 33 | + * ```typescript |
| 34 | + * import { Tensor as HFTensor } from "@huggingface/transformers.js"; |
| 35 | + * const { Tensor, RawSession } = Supabase.ai; |
| 36 | + * |
| 37 | + * const session = await RawSession.fromHuggingFace('Supabase/gte-small'); |
| 38 | + * |
| 39 | + * // Example only, in real 'feature-extraction' tensors are given from the tokenizer step. |
| 40 | + * const inputs = { |
| 41 | + * input_ids: new Tensor('float32', [...], [n, 2]), |
| 42 | + * attention_mask: new Tensor('float32', [...], [n, 2]), |
| 43 | + * token_types_ids: new Tensor('float32', [...], [n, 2]) |
| 44 | + * }; |
| 45 | + * |
| 46 | + * const { last_hidden_state } = await session.run(inputs); |
| 47 | + * |
| 48 | + * // Using `transformers.js` APIs |
| 49 | + * const hfTensor = HFTensor.mean_pooling(last_hidden_state, inputs.attention_mask).normalize(); |
| 50 | + * |
| 51 | + * return hfTensor.tolist(); |
| 52 | + * |
| 53 | + * ``` |
| 54 | + */ |
| 55 | + readonly Tensor: typeof Tensor; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Provides AI related APIs |
| 60 | + */ |
| 61 | + export const ai: Ai; |
| 62 | + |
| 63 | + export type TensorDataTypeMap = { |
| 64 | + float32: Float32Array | number[]; |
| 65 | + float64: Float64Array | number[]; |
| 66 | + string: string[]; |
| 67 | + int8: Int8Array | number[]; |
| 68 | + uint8: Uint8Array | number[]; |
| 69 | + int16: Int16Array | number[]; |
| 70 | + uint16: Uint16Array | number[]; |
| 71 | + int32: Int32Array | number[]; |
| 72 | + uint32: Uint32Array | number[]; |
| 73 | + int64: BigInt64Array | number[]; |
| 74 | + uint64: BigUint64Array | number[]; |
| 75 | + bool: Uint8Array | number[]; |
| 76 | + }; |
| 77 | + |
| 78 | + export type TensorMap = { [key: string]: Tensor<keyof TensorDataTypeMap> }; |
| 79 | + |
| 80 | + export class Tensor<T extends keyof TensorDataTypeMap> { |
| 81 | + /** Type of the tensor. */ |
| 82 | + type: T; |
| 83 | + |
| 84 | + /** The data stored in the tensor. */ |
| 85 | + data: TensorDataTypeMap[T]; |
| 86 | + |
| 87 | + /** Dimensions of the tensor. */ |
| 88 | + dims: number[]; |
| 89 | + |
| 90 | + /** The total number of elements in the tensor. */ |
| 91 | + size: number; |
| 92 | + |
| 93 | + constructor(type: T, data: TensorDataTypeMap[T], dims: number[]); |
| 94 | + } |
| 95 | + |
| 96 | + export class RawSession { |
| 97 | + /** The underline session's ID. |
| 98 | + * Session's ID are unique for each loaded model, it means that even if a session is constructed twice its will share the same ID. |
| 99 | + */ |
| 100 | + id: string; |
| 101 | + |
| 102 | + /** A list of all input keys the model expects. */ |
| 103 | + inputs: string[]; |
| 104 | + |
| 105 | + /** A list of all output keys the model will result. */ |
| 106 | + outputs: string[]; |
| 107 | + |
| 108 | + /** Loads a ONNX model session from source URL. |
| 109 | + * Sessions are loaded once, then will keep warm cross worker's requests |
| 110 | + */ |
| 111 | + static fromUrl(source: string | URL): Promise<RawSession>; |
| 112 | + |
| 113 | + /** Loads a ONNX model session from **HuggingFace** repository. |
| 114 | + * Sessions are loaded once, then will keep warm cross worker's requests |
| 115 | + */ |
| 116 | + static fromHuggingFace(repoId: string, opts?: { |
| 117 | + /** |
| 118 | + * @default 'https://huggingface.co' |
| 119 | + */ |
| 120 | + hostname?: string | URL; |
| 121 | + path?: { |
| 122 | + /** |
| 123 | + * @default '{REPO_ID}/resolve/{REVISION}/onnx/{MODEL_FILE}?donwload=true' |
| 124 | + */ |
| 125 | + template?: string; |
| 126 | + /** |
| 127 | + * @default 'main' |
| 128 | + */ |
| 129 | + revision?: string; |
| 130 | + /** |
| 131 | + * @default 'model_quantized.onnx' |
| 132 | + */ |
| 133 | + modelFile?: string; |
| 134 | + }; |
| 135 | + }): Promise<RawSession>; |
| 136 | + |
| 137 | + /** Run the current session with the given inputs. |
| 138 | + * Use `inputs` and `outputs` properties to know the required inputs and expected results for the model session. |
| 139 | + * |
| 140 | + * @param inputs The input tensors required by the model. |
| 141 | + * @returns The output tensors generated by the model. |
| 142 | + * |
| 143 | + * @example |
| 144 | + * ```typescript |
| 145 | + * const session = await RawSession.fromUrl("https://example.com/model.onnx"); |
| 146 | + * |
| 147 | + * // Prepare the input tensors |
| 148 | + * const inputs = { |
| 149 | + * input1: new Tensor("float32", [1.0, 2.0, 3.0], [3]), |
| 150 | + * input2: new Tensor("float32", [4.0, 5.0, 6.0], [3]), |
| 151 | + * }; |
| 152 | + * |
| 153 | + * // Run the model |
| 154 | + * const outputs = await session.run(inputs); |
| 155 | + * |
| 156 | + * console.log(outputs.output1); // Output tensor |
| 157 | + * ``` |
| 158 | + */ |
| 159 | + run(inputs: TensorMap): Promise<TensorMap>; |
| 160 | + } |
| 161 | +} |
0 commit comments