/** * An ONNX encoder, loaded only if someone asks for one. * * OPTIONAL IN THE STRICT SENSE: `onnxruntime-node` is not a dependency of this * package and is imported dynamically, so a machine without it never sees an * error and never pays a byte. Nothing here runs unless a caller supplies a * model path. * * WHY THIS IS FORTY LINES OF GLUE AND NOT A FRAMEWORK. The hard part is not * calling onnxruntime -- it is that ranking happens inside synchronous engines * and inference is async. `embedding.ts` solves that with a two-phase pass; * this file only has to turn text into ids and ids into vectors. * * THE TOKENIZER IS DELIBERATELY THE CALLER'S PROBLEM. A real sentence encoder * needs the exact WordPiece or SentencePiece vocabulary it was trained with, * and shipping a guess would produce embeddings that are confidently wrong -- * the worst failure available here, because the output looks like a working * ranking. So `tokenize` is a required argument, and the built-in hashing one * is offered only for models that were trained on hashed ids. */ import type { SemanticEncoder } from './embedding.js'; /** Turns one string into the token ids the model expects. */ export type OnnxTokenizer = (text: string) => number[]; export interface OnnxEncoderOptions { /** Path to the `.onnx` file. */ readonly modelPath: string; /** Vector width the model emits. Validated against the first real output. */ readonly dimensions: number; /** Must match the vocabulary the model was trained with. */ readonly tokenize: OnnxTokenizer; /** Name of the ids input. Defaults to the model's first input. */ readonly inputName?: string; /** Name of the embedding output. Defaults to the model's first output. */ readonly outputName?: string; /** Longest token sequence per text; longer input is truncated. */ readonly maxTokens?: number; /** Texts per `session.run` call. */ readonly batchSize?: number; } /** * A tokenizer for models trained on hashed ids, and a trap for everything else. * * Hashes each word into `[0, vocabSize)`. This is correct ONLY for a model * whose training used the same hashing scheme; against a real BERT checkpoint * it produces meaningless ids and therefore meaningless vectors, which is why * it is not the default and why `tokenize` has no default at all. */ export declare function hashingTokenizer(vocabSize: number): OnnxTokenizer; /** * Loads a model and returns an encoder over it. * * Throws if `onnxruntime-node` is absent or the model will not load, because * this is called once at startup by a caller who explicitly asked for a model. * Failing loudly here is right; failing loudly per-request is not, which is * why `warmEmbeddings` swallows everything downstream of this point. */ export declare function onnxEncoder(options: OnnxEncoderOptions): Promise; /** * One `session.run` over a batch of texts, returning one vector per text. * * EXPORTED FOR THE GUARDS BELOW, which are the part of this file most worth * testing and the part hardest to reach through {@link onnxEncoder}: that path * needs `onnxruntime-node` present and a real model that misbehaves in a * specific way. Every argument here is structural, so a test can hand it a * runtime and a session that return a deliberately wrong shape and assert that * the shape is rejected rather than silently mis-sliced. */ export declare function runBatch(ort: OnnxRuntime, session: OnnxSession, inputName: string, outputName: string, texts: readonly string[], tokenize: OnnxTokenizer, maxTokens: number, dimensions: number): Promise; export interface OnnxTensorLike { readonly data: ArrayLike; readonly dims: readonly number[]; } export interface OnnxSession { readonly inputNames: readonly string[]; readonly outputNames: readonly string[]; run(feeds: Record): Promise>; } export interface OnnxRuntime { readonly InferenceSession: { create(path: string): Promise; }; readonly Tensor: new (type: string, data: BigInt64Array, dims: readonly number[]) => unknown; } //# sourceMappingURL=onnx.d.ts.map