Graphorin API reference v0.1.0
Graphorin API reference / @graphorin/reranker-transformersjs
@graphorin/reranker-transformersjs
Cross-encoder reranker adapter for the Graphorin framework. Wraps
@huggingface/transformersto score(query, passage)pairs with a BGE cross-encoder running entirely in-process. Implements theReRankercontract from@graphorin/memory/search.Project Graphorin · v0.1.0 · MIT License · © 2026 Oleksiy Stepurenko · https://github.com/o-stepper/graphorin
Status
- Published: v0.1.0 (optional sub-pack)
- Default model: locale-aware (
Xenova/bge-reranker-basefor English,BAAI/bge-reranker-v2-m3for everything else). - Default precision: FP16 (
'fp16'dtype). - Default device: CPU.
Install
pnpm add @graphorin/reranker-transformersjs @huggingface/transformersThe first call to rerank(...) lazily downloads the reranker weights into the Hugging Face cache directory (~/.cache/huggingface/hub by default; honours GRAPHORIN_CACHE_DIR). To pre-warm in CI:
node -e "import('@graphorin/reranker-transformersjs').then(m => m.createCrossEncoderReranker({ locale: 'en' }).rerank('warmup', [[]], {}))"Usage
Drop-in replacement for the built-in RRF reranker
import { createMemory } from '@graphorin/memory';
import { createCrossEncoderReranker } from '@graphorin/reranker-transformersjs';
const memory = createMemory({
store,
embedder,
reranker: createCrossEncoderReranker({ locale: 'en' }),
});Multilingual deployment
const reranker = createCrossEncoderReranker({
locale: 'pt-BR',
// Falls back to BAAI/bge-reranker-v2-m3 (568M, multilingual).
});Override the model explicitly
For narrow language-pair scenarios (e.g. CJK, low-resource European languages) plug in a specialised cross-encoder of your choice:
const reranker = createCrossEncoderReranker({
model: 'cross-encoder/ms-marco-MiniLM-L-6-v2',
dtype: 'q8',
});Idle eviction (bound resident memory)
// Drop the loaded ONNX session after 10 minutes of inactivity.
const reranker = createCrossEncoderReranker({
locale: 'en',
idleEvictionMs: 10 * 60 * 1000,
});Custom passage extractor
The default extractor walks text → summary → value → label → id. Override when your custom MemoryRecord schema attaches the canonical text elsewhere:
const reranker = createCrossEncoderReranker<MyRecord>({
passageExtractor: (record) => `${record.title}. ${record.body}`,
});Acceptance criteria
- [x] Reranks fixture results.
- [x] Locale auto-pick verified (
'en' → bge-reranker-base, every other locale →bge-reranker-v2-m3). - [x] Lazy load + idle unload works.
- [x] Implements
ReRankerfrom@graphorin/memory/search.
Related decisions
- ADR-024 — Reciprocal Rank Fusion default + pluggable rerankers.
License
MIT © 2026 Oleksiy Stepurenko
Project Graphorin · v0.1.0 · MIT License · © 2026 Oleksiy Stepurenko · https://github.com/o-stepper/graphorin
@graphorin/reranker-transformersjs — cross-encoder reranker adapter for the Graphorin framework.
Wraps @huggingface/transformers@^4.1.0 to score (query, passage) pairs in-process. Plug into the memory hybrid-search pipeline as a drop-in replacement for the built-in RRFReranker:
import { createMemory } from '@graphorin/memory';
import { createCrossEncoderReranker } from '@graphorin/reranker-transformersjs';
const memory = createMemory({
store,
embedder,
reranker: createCrossEncoderReranker({ locale: 'en' }),
});Locale-aware default model:
'en'/'en-*'→Xenova/bge-reranker-base(278M parameters, FP16 quantized).- Every other locale →
BAAI/bge-reranker-v2-m3(568M parameters, multilingual baseline).
Operators that want a narrower / language-specific cross-encoder pass an explicit model option — the package's defaults deliberately avoid privileging any single language pair.
Classes
| Class | Description |
|---|---|
| CrossEncoderLoadError | Raised when the @huggingface/transformers peer is missing or the configured cross-encoder model fails to load. |
| TransformersJsReRanker | ReRanker implementation. Matches the contract from @graphorin/memory/search. |
Interfaces
| Interface | Description |
|---|---|
| ClassifierResult | Output shape returned by @huggingface/transformers' text-classification pipeline. Each pair returns either a single { label, score } object (top-k = 1) or an array of them. We normalise on the array form upstream so the cross-encoder always sees a consistent shape. |
| CrossEncoderRerankerOptions | Options accepted by createCrossEncoderReranker. |
Type Aliases
| Type Alias | Description |
|---|---|
| CrossEncoderPipeline | - |
| CrossEncoderPipelineFactory | - |
| LocaleTag | BCP 47 locale tag (e.g. 'en', 'en-GB', 'pt-BR', 'zh-Hans-CN'). |
| PassageExtractor | Caller-supplied passage extractor. Receives the record + the surrounding metadata (kind, sensitivity, tags) and returns the passage to feed into the cross-encoder. |
| RerankerDtype | Numeric dtype hint. Default 'fp16' per Phase 16 § @graphorin/reranker-transformersjs. |
Variables
| Variable | Description |
|---|---|
| DEFAULT_ENGLISH_MODEL | - |
| DEFAULT_MULTILINGUAL_MODEL | - |
| RERANKER_ID | - |
| VERSION | Canonical version constant. Mirrors the package.json version. |
Functions
| Function | Description |
|---|---|
| _resetPipelineFactoryCacheForTesting | Test-only helper. Drops the cached pipeline factory so the next loader call re-imports the peer. |
| createCrossEncoderReranker | Build a cross-encoder reranker. Lazy: the pipeline is constructed on the first rerank() call so packaging the reranker pays no model-load cost. |
| defaultPassageExtractor | Returns the best-effort passage text for a MemoryRecord. The order of preference, top-down: |
| extractPairScores | Normalises the raw pipeline output to a flat score[] aligned with the input pair order. Cross-encoder classifiers return either a single-best {label, score} per pair or an array of topk entries — we collapse on the highest-scoring positive label. |
| loadDefaultPipelineFactory | - |
| mergeAndDedupe | Merge the per-source lists into a single deduplicated array, preserving the highest initial score per record id and the first-seen order for stable tie-breaking. Pure function; exported for the unit test fixture. |
| pickRerankerModel | Pick a reranker model from the agent locale. Pure function so callers (and tests) can pre-resolve the choice without constructing the reranker. |