import type { CrossEncoderCandidate, CrossEncoderProvider } from './rerank.js'; import type { OnnxDType } from '../contracts/embed.js'; /** How many (query, text) pairs go through one `model()` call. Bounds peak * memory/latency for a single batch rather than tokenizing+running all * `topK` (default 40) candidates in one shot — a compact 6-layer model on * CPU still benefits from not building one huge padded batch. Overridable * via `config.crossEncoder.onnx.batchSize`. */ export declare const DEFAULT_BATCH_SIZE = 16; /** Sigmoid — folds the model's raw single-label regression logit into * `applyRerank`'s `[0,1]` score contract. Monotonic, so rank order among * candidates is unaffected vs. using the raw logit directly; unlike a bare * clamp, it doesn't collapse every negative (irrelevant) logit to the same * 0, which would otherwise destroy relative ordering among the tail of the * candidate window. */ export declare function sigmoid(x: number): number; /** Structural subset of `@huggingface/transformers`' `Tensor` actually used * here — mirrors `OnnxTensorLike` in onnx.ts. Real call sites always receive * a real `Tensor` instance; unit tests inject a fake one via the * `loadBackend` DI seam. */ export interface OnnxCrossEncoderTensorLike { readonly dims: number[]; tolist(): unknown; } export interface OnnxCrossEncoderBatchEncoding { readonly [key: string]: unknown; } export interface OnnxCrossEncoderModelOutput { readonly logits: OnnxCrossEncoderTensorLike; } /** * Narrow surface over AutoTokenizer + AutoModelForSequenceClassification * actually used by this provider — tokenize a (query, text) pair batch -> * run -> read logits. Mirrors `OnnxBackend` (onnx.ts)'s DI-seam shape so * unit tests can inject a fake backend instead of downloading the real * model artifact. */ export interface OnnxCrossEncoderBackend { tokenize(queries: string[], texts: string[]): Promise; run(inputs: OnnxCrossEncoderBatchEncoding): Promise; } /** * Extract per-pair relevance scores from a model output — exported standalone * (not inlined) so unit tests can exercise the exact production scoring math * against hand-built synthetic tensors, with no ONNX session and no model * download (mirrors `poolMeanNormalize`'s testability rationale in onnx.ts). * `logits.tolist()` is `number[][]` shaped `[batch, num_labels]`; this * model's registry entry is always a single-label regression head * (`id2label: {0: LABEL_0}`), so row `[0]` is the relevance logit. */ export declare function extractSigmoidScores(output: OnnxCrossEncoderModelOutput): number[]; interface ResolvedOnnxRerankLoadOpts { hfModelId: string; revision: string; dtype: OnnxDType; cacheDir?: string; modelPath?: string; numThreads: number; } export interface OnnxCrossEncoderConfig { /** Canonical model label — `RERANK_MODEL_REGISTRY` lookup key, NOT the HF * repo id. Defaults to `DEFAULT_RERANK_MODEL_LABEL` * ('ms-marco-MiniLM-L-6-v2'). */ model?: string; /** HF repo id to load from. Defaults to the registry entry's `hfRepo`, * falling back to `DEFAULT_RERANK_HF_MODEL_ID` for an unregistered * model. */ hfModelId?: string; /** Pinned HF revision (commit SHA) — never `'main'`. */ revision?: string; /** ORT weight precision. */ dtype?: OnnxDType; /** Local cache directory for downloaded weights. `resolveRerankOpts` * (rerank.ts) defaults this to `/models/onnx/` — the SAME root * `providers/index.ts` uses for the embed model (ADR-018 S2); different * HF repo ids nest under distinct subdirectories, so embed and rerank * weights coexist there without collision. */ cacheDir?: string; /** Explicit local snapshot override for offline/air-gapped installs — see * `resolveLocalSnapshotDir`'s doc comment (onnx.ts) for the exact lookup * rule when this is set. */ modelPath?: string; /** ORT intraOpNumThreads cap. Defaults to `DEFAULT_NUM_THREADS`. */ numThreads?: number; /** Pairs per `model()` call. Defaults to `DEFAULT_BATCH_SIZE` (16). */ batchSize?: number; /** Test/DI seam only — overrides the default `@huggingface/transformers` * backed loader so unit tests can inject a fake backend instead of * downloading the real model. Production code never sets this. */ loadBackend?: (opts: ResolvedOnnxRerankLoadOpts) => Promise; } export declare class OnnxCrossEncoderProvider implements CrossEncoderProvider { readonly name: "onnx"; readonly model: string; private readonly hfModelId; private readonly revision; private readonly dtype; private readonly cacheDir; private readonly modelPath; private readonly numThreads; private readonly batchSize; private readonly loadBackend; private backendPromise; constructor(config?: OnnxCrossEncoderConfig); /** * Lazy, memoized session load — same posture as `OnnxEmbedProvider` * (onnx.ts): the constructor never triggers a weight download or session * load; the FIRST real `score()` call after boot (or after config * selects this provider) pays that cost inline, every subsequent call in * the process lifetime reuses the memoized session. A failed load is NOT * memoized, so a transient failure can be retried on the next call. */ private getBackend; /** * Public, idempotent wrapper around `getBackend()` — same shape as * `OnnxEmbedProvider.preload()` — lets a future caller trigger the model * load proactively at daemon boot instead of paying it on the first real * search request. Not wired into `serve.ts` by this change (out of this * worktree's scope — `serve.ts` boot wiring belongs to whichever change * next touches daemon startup); `score()` alone is fully correct without * it, just cold on the first call. */ preload(): Promise; /** * `signal`: mirrors `OnnxEmbedProvider.embed()`'s honestly-narrowed * cancellation contract — in-process ORT inference has no `fetch`-style * mid-call abort plumbing, so the signal is checked BETWEEN batches and * throws immediately once set, but a signal that fires mid-batch will not * interrupt that in-flight `run()` call. */ score(query: string, candidates: CrossEncoderCandidate[], signal?: AbortSignal): Promise>; } export {};