|
| 1 | +import { |
| 2 | + type ClientOptions, |
| 3 | + type Requester, |
| 4 | + type Input, |
| 5 | + type Datatype, |
| 6 | + getOutputSparseContents, |
| 7 | +} from '@clinia/models-client-common'; |
| 8 | + |
| 9 | +export type SparseEmbedRequest = { |
| 10 | + /** |
| 11 | + * The unique identifier for the request. |
| 12 | + */ |
| 13 | + id: string; |
| 14 | + /** |
| 15 | + * The list of texts to be embedded. |
| 16 | + */ |
| 17 | + texts: string[]; |
| 18 | +}; |
| 19 | + |
| 20 | +export type SparseEmbedResponse = { |
| 21 | + /** |
| 22 | + * The unique identifier for the response,corresponding to that of the request. |
| 23 | + */ |
| 24 | + id: string; |
| 25 | + /** |
| 26 | + * The list of sparse embeddings for each text. Each embedding is a map from feature names to their corresponding values. |
| 27 | + */ |
| 28 | + embeddings: Record<string, number>[]; |
| 29 | +}; |
| 30 | + |
| 31 | +const SPARSE_EMBEDDER_INPUT_KEY = 'text'; |
| 32 | +const SPARSE_EMBEDDER_OUTPUT_KEY = 'embedding'; |
| 33 | +const SPARSE_EMBEDDER_INPUT_DATATYPE: Datatype = 'BYTES'; |
| 34 | + |
| 35 | +export class SparseEmbedder { |
| 36 | + private _requester: Requester; |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the underlying requester instance. |
| 40 | + */ |
| 41 | + get requester(): Requester { |
| 42 | + return this._requester; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Creates an instance of SparseEmbedder. |
| 47 | + * @param options - The client options containing the requester. |
| 48 | + */ |
| 49 | + constructor(options: ClientOptions) { |
| 50 | + this._requester = options.requester; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Asynchronously generate embeddings using a specified model. |
| 55 | + * @param modelName - The name of the model to use. |
| 56 | + * @param modelVersion - The version of the model to use. |
| 57 | + * @param request - The request containing texts to be embedded. |
| 58 | + * @returns The response containing the embeddings. |
| 59 | + */ |
| 60 | + async embed( |
| 61 | + modelName: string, |
| 62 | + modelVersion: string, |
| 63 | + request: SparseEmbedRequest, |
| 64 | + ): Promise<SparseEmbedResponse> { |
| 65 | + if (!request.texts) { |
| 66 | + throw new Error('Request texts must be provided.'); |
| 67 | + } |
| 68 | + |
| 69 | + if (request.texts.length === 0) { |
| 70 | + throw new Error('Request texts cannot be empty.'); |
| 71 | + } |
| 72 | + |
| 73 | + const inputs: Input[] = [ |
| 74 | + { |
| 75 | + name: SPARSE_EMBEDDER_INPUT_KEY, |
| 76 | + shape: [request.texts.length], |
| 77 | + datatype: SPARSE_EMBEDDER_INPUT_DATATYPE, |
| 78 | + contents: [ |
| 79 | + { |
| 80 | + stringContents: request.texts, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + ]; |
| 85 | + |
| 86 | + // The embedder model has only one input and one output |
| 87 | + const outputKeys = [SPARSE_EMBEDDER_OUTPUT_KEY]; |
| 88 | + |
| 89 | + const outputs = await this._requester.infer( |
| 90 | + modelName, |
| 91 | + modelVersion, |
| 92 | + inputs, |
| 93 | + outputKeys, |
| 94 | + request.id, |
| 95 | + ); |
| 96 | + |
| 97 | + const embeddings = getOutputSparseContents(outputs[0]); |
| 98 | + |
| 99 | + return { |
| 100 | + id: request.id, |
| 101 | + embeddings, |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Checks the readiness status of the model. |
| 107 | + * @throws {Error} If the model is not ready. |
| 108 | + */ |
| 109 | + async ready(modelName: string, modelVersion: string): Promise<void> { |
| 110 | + await this._requester.ready(modelName, modelVersion); |
| 111 | + } |
| 112 | +} |
0 commit comments