Skip to content
reaatechREAATECH

@reaatech/confidence-router-classifiers

npm v0.1.0

Provides a collection of intent classification strategies—including keyword matching, embedding similarity, and LLM-based analysis—that implement a unified `Classifier` interface for use with `confidence-router`. It includes a `ClassifierRegistry` class to manage these implementations and execute them in configurable fallback chains.

@reaatech/confidence-router-classifiers

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Pluggable classifier system for confidence-router, with built-in implementations for keyword matching, embedding similarity, and LLM-based classification. All classifiers conform to the Classifier interface and can be registered for fallback chain execution.

Installation

terminal
npm install @reaatech/confidence-router-classifiers
# or
pnpm add @reaatech/confidence-router-classifiers

Feature Overview

  • KeywordClassifier — deterministic pattern matching with exact / substring / regex modes
  • EmbeddingSimilarityClassifier — cosine similarity between input and reference vectors
  • LLMClassifier — OpenAI and Anthropic chat completion classifiers
  • ClassifierRegistry — named registration with default selection and automatic fallback chains
  • Pluggable architecture — implement Classifier to add custom classifiers
  • Zero runtime dependencies beyond core — uses native fetch (Node 18+) for LLM calls

Quick Start

typescript
import { KeywordClassifier, ClassifierRegistry } from "@reaatech/confidence-router-classifiers";
 
const classifier = new KeywordClassifier([
  { label: "book_flight", keywords: ["flight", "fly", "ticket", "plane"] },
  { label: "check_status", keywords: ["status", "check", "where", "track"] },
  { label: "cancel_booking", keywords: ["cancel", "refund", "delete"] },
]);
 
const result = await classifier.classify("I want to book a flight to Paris");
// result.predictions[0] → { label: "book_flight", confidence: 0.5 }

Built-in Classifiers

KeywordClassifier

Fast, deterministic pattern matching. Zero external dependencies.

typescript
import { KeywordClassifier } from "@reaatech/confidence-router-classifiers";
 
const classifier = new KeywordClassifier(
  [
    { label: "book_flight", keywords: ["flight", "fly", "ticket"], weight: 1.0 },
    { label: "cancel_booking", keywords: ["cancel", "refund"], mode: "substring" },
  ],
  { name: "intent-keywords", caseSensitive: false }
);
OptionTypeDefaultDescription
namestringkeywordClassifier name for registration
prioritynumber1Priority in fallback chain
enabledbooleantrueWhether this classifier is active
caseSensitivebooleanfalseMatch keywords case-sensitively

Pattern modes (MatchMode):

ModeBehavior
substring (default)Matches if keyword appears anywhere in the input
exactMatches on word boundaries only (\bkeyword\b)
regexKeywords are treated as regular expressions

EmbeddingSimilarityClassifier

Compares input embedding vectors against labeled reference vectors using cosine similarity.

typescript
import { EmbeddingSimilarityClassifier } from "@reaatech/confidence-router-classifiers";
 
const classifier = new EmbeddingSimilarityClassifier(
  [
    { label: "positive", vector: [0.9, 0.1, 0.2] },
    { label: "negative", vector: [-0.8, 0.3, 0.1] },
  ],
  {
    embeddingProvider: async (text) => {
      // Call your embedding API
      return [0.8, 0.2, 0.1];
    },
  }
);
OptionTypeDefaultDescription
namestringembeddingClassifier name
prioritynumber1Priority in fallback chain
enabledbooleantrueWhether active
embeddingProvider(text: string) => Promise<number[]> | number[]Function to generate embedding vectors

The embeddingProvider can also be passed at classify time via context.embeddingProvider.

LLMClassifier

Uses OpenAI or Anthropic chat completions to classify input text into predefined labels.

typescript
import { LLMClassifier } from "@reaatech/confidence-router-classifiers";
 
const classifier = new LLMClassifier({
  provider: "openai",
  apiKey: process.env.OPENAI_API_KEY,
  model: "gpt-4o-mini",
  labels: ["book_flight", "check_status", "cancel_booking"],
  timeout: 15000,
  retries: 2,
});
OptionTypeDefaultDescription
provideropenai" | "anthropic(required)LLM provider
apiKeystringprocess.env.OPENAI_API_KEY / ANTHROPIC_API_KEYAPI key
modelstringgpt-4o-mini / claude-3-haiku-20240307Model to use
baseUrlstringProvider defaultCustom API endpoint
labelsstring[](required)Legal classification labels
timeoutnumber30000Request timeout in ms
retriesnumber2Retry attempts with exponential backoff
systemPromptstringAuto-generatedCustom system prompt

The LLMClassifier automatically:

  • Normalizes confidence to [0, 1]
  • Fills missing labels with zero confidence
  • Strips markdown fences from responses
  • Validates that returned labels are in the allowed set

ClassifierRegistry

Manages named classifiers with default selection and fallback chain execution.

typescript
import { ClassifierRegistry } from "@reaatech/confidence-router-classifiers";
import { KeywordClassifier } from "@reaatech/confidence-router-classifiers";
 
const registry = new ClassifierRegistry();
registry.register(new KeywordClassifier([...], { name: "intents", priority: 1 }));
registry.register(new LLMClassifier({ name: "llm-backup", priority: 2, ... }));
 
// Classify with a specific classifier
const result = await registry.classify("book a flight", "intents");
 
// Run fallback chain (tries each enabled classifier in priority order)
const fallback = await registry.getFallbackChain("I want to fly");
MethodDescription
register(classifier)Adds a classifier; first enabled one becomes default
get(name)Retrieves by name; returns undefined if not found
classify(input, name?, context?)Classifies with the named or default classifier
getFallbackChain(input)Tries each enabled classifier in priority order; returns first success

Custom Classifier

Implement the Classifier interface from @reaatech/confidence-router-core:

typescript
import type { Classifier, ClassificationResult } from "@reaatech/confidence-router-core";
 
class CustomClassifier implements Classifier {
  name = "custom";
  type = "custom";
  enabled = true;
  priority = 1;
 
  async classify(input: string, context?: Record<string, unknown>): Promise<ClassificationResult> {
    return {
      predictions: [{ label: "detected_intent", confidence: 0.95 }],
    };
  }
 
  async validate(): Promise<boolean> {
    return true;
  }
}

License

MIT