Classification
Hooks for text classification, zero-shot classification, NER, and reranking.
Classification Hooks
See it in action
Try the Text Insights blocks for working demos of these hooks — the Sentiment Analyzer drives useClassify and the Text Classifier drives useClassifyZeroShot, both over on-device models.
useClassify
Classify text into predefined categories.
import { useClassify } from '@localmode/react';
import { transformers } from '@localmode/transformers';
const model = transformers.classifier('Xenova/distilbert-base-uncased-finetuned-sst-2-english');
function Demo() {
const { data, isLoading, execute } = useClassify({ model });
return (
<div>
<button onClick={() => execute('I love this!')}>Classify</button>
{data && <p>{data.label}: {(data.score * 100).toFixed(1)}%</p>}
</div>
);
}useClassifyZeroShot
Classify text with custom labels — no model fine-tuning required. Hook-level multiLabel and hypothesisTemplate apply to every call and can be overridden per call via the execute input.
import { useClassifyZeroShot } from '@localmode/react';
const { data, execute } = useClassifyZeroShot({
model,
multiLabel: true, // allow multiple labels per text
hypothesisTemplate: 'This text is about {}.', // custom NLI hypothesis
});
await execute({ text: 'The server is down', candidateLabels: ['bug', 'feature', 'question'] });
// data.labels = [{ label: 'bug', score: 0.89 }, ...]
// Per-call overrides win over hook-level options:
await execute({
text: 'New phone camera review',
candidateLabels: ['tech', 'food'],
multiLabel: false,
hypothesisTemplate: 'This review covers {}.',
});Options
| Option | Type | Description |
|---|---|---|
model | ZeroShotClassificationModel | The zero-shot classification model (required) |
multiLabel | boolean | Allow multiple labels per text (default: provider default, usually false) |
hypothesisTemplate | string | Hypothesis template, e.g. "This text is about {}." — delivered at the model boundary |
useExtractEntities
Extract named entities (NER) from text.
import { useExtractEntities } from '@localmode/react';
const { data, execute } = useExtractEntities({ model });
await execute('John works at Google in Seattle');
// data.entities = [{ entity: 'PER', word: 'John' }, { entity: 'ORG', word: 'Google' }, ...]useRerank
Rerank documents by relevance to a query with a cross-encoder model. Hook-level topK applies to every call and can be overridden per call via the execute input; when neither is set, all documents are returned ranked.
import { useRerank } from '@localmode/react';
import { transformers } from '@localmode/transformers';
const model = transformers.reranker('Xenova/ms-marco-MiniLM-L-6-v2');
function Demo() {
const { data, isLoading, execute } = useRerank({ model, topK: 3 });
return (
<div>
<button onClick={() => execute({ query: 'What is machine learning?', documents })}>
Rerank
</button>
{data?.results.map((r) => (
<p key={r.index}>{r.score.toFixed(2)} — {r.text}</p>
))}
</div>
);
}
// Per-call override wins over the hook-level topK:
await execute({ query: 'What is machine learning?', documents, topK: 5 });data is the core RerankResult — results sorted by relevance score (highest first), plus usage and response.
Options
| Option | Type | Description |
|---|---|---|
model | RerankerModel | The reranker model to use (required) |
topK | number | Number of top results to return (default: all); a per-call topK on the execute input overrides this |
For full API reference (rerank(), options, result types, and custom providers), see the Core Reranking guide. For recommended reranker models and RAG recipes, see the Transformers Reranking guide.
For full API reference, see the Core Classification guide.
Blocks
| App | Description | Links |
|---|---|---|
| Text Insights (Sentiment Analyzer) | Batch sentiment analysis with useSequentialBatch | Live block · Source |
| Text Insights (Email Classifier) | Zero-shot classification with useOperationList | Live block · Source |