Joshua Lochner commited on
Enable lazy construction of pipeline (#554)
Browse files112341 unmodified line678910910111213141516171819202122233 unmodified lines2728293031323334import type { Tensor } from "@xenova/transformers";import type { Tensor, Pipeline } from "@xenova/transformers";import { pipeline, dot } from "@xenova/transformers";
// see here: https://github.com/nmslib/hnswlib/blob/359b2ba87358224963986f709e593d799064ace6/README.md?plain=1#L341 unmodified line return 1.0 - dot(tensor1.data, tensor2.data);}
const modelId = "Xenova/gte-small";const extractor = await pipeline("feature-extraction", modelId);// Use the Singleton pattern to enable lazy construction of the pipeline.class PipelineSingleton { static modelId = "Xenova/gte-small"; static instance: Promise<Pipeline> | null = null; static async getInstance() { if (this.instance === null) { this.instance = pipeline("feature-extraction", this.modelId); } return this.instance; }}
// see https://huggingface.co/thenlper/gte-small/blob/d8e2604cadbeeda029847d19759d219e0ce2e6d8/README.md?code=true#L2625export const MAX_SEQ_LEN = 512 as const;
3 unmodified lines { topK = 5 }: { topK: number }) { const input = [query, ...sentences];
const extractor = await PipelineSingleton.getInstance(); const output: Tensor = await extractor(input, { pooling: "mean", normalize: true });
const queryTensor: Tensor = output[0];src/lib/server/websearch/sentenceSimilarity.ts+15-3