npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

semantic-router-ts

v1.0.3

Published

Superfast semantic routing for LLMs and AI agents

Readme

semantic-router-ts

Superfast semantic routing for LLMs and AI agents.

npm version License: MIT

Features

  • 🚀 Fast - Uses semantic embeddings for sub-100ms routing decisions
  • 🔌 Pluggable Encoders - OpenAI, local Transformers.js, or bring your own
  • 🎯 Accurate - Semantic similarity beats keyword matching
  • 📦 Zero Dependencies - Core package has no deps; encoders are optional
  • 🔒 Type Safe - Full TypeScript support with strict types
  • 🌐 Offline Support - LocalEncoder works without API calls

Installation

npm install semantic-router-ts

# For local/offline embeddings (recommended)
npm install @xenova/transformers

# For OpenAI embeddings
npm install openai

Quick Start

import { SemanticRouter, Route, LocalEncoder } from 'semantic-router-ts';

// Define your routes
const routes: Route[] = [
  {
    name: 'greeting',
    utterances: ['hello', 'hi there', 'hey', 'good morning'],
  },
  {
    name: 'farewell', 
    utterances: ['goodbye', 'bye', 'see you later', 'take care'],
  },
  {
    name: 'help',
    utterances: ['I need help', 'can you assist me', 'support please'],
  },
];

// Create router with local encoder (no API calls)
const router = new SemanticRouter({
  routes,
  encoder: new LocalEncoder(),
  threshold: 0.4, // Minimum confidence to match
});

// Initialize (pre-encodes all routes)
await router.initialize();

// Route queries
const result = await router.route('hey, how are you doing?');
console.log(result.name);       // 'greeting'
console.log(result.confidence); // 0.85

// No match below threshold
const noMatch = await router.route('what is the weather?');
console.log(noMatch.name);      // null

Encoders

LocalEncoder (Recommended)

Uses Transformers.js for fully offline embeddings:

import { LocalEncoder } from 'semantic-router-ts';

const encoder = new LocalEncoder({
  model: 'Xenova/all-MiniLM-L6-v2', // Default, 384 dimensions
  normalize: true,
});

Supported models:

  • Xenova/all-MiniLM-L6-v2 (384d, fast)
  • Xenova/all-mpnet-base-v2 (768d, more accurate)
  • Xenova/bge-small-en-v1.5 (384d)
  • Xenova/bge-base-en-v1.5 (768d)

OpenAIEncoder

Uses OpenAI's embedding API:

import { OpenAIEncoder } from 'semantic-router-ts';

const encoder = new OpenAIEncoder({
  apiKey: process.env.OPENAI_API_KEY,
  model: 'text-embedding-3-small', // Default
  dimensions: 1536,
});

Configuration

const router = new SemanticRouter({
  routes: [...],
  encoder: new LocalEncoder(),
  
  // How many similar utterances to consider
  topK: 5,
  
  // How to aggregate scores ('mean' | 'max' | 'sum')
  aggregation: 'mean',
  
  // Minimum confidence threshold (0-1)
  threshold: 0.4,
  
  // Optional LLM for low-confidence fallback
  llm: myLLMImplementation,
});

Dynamic Routes

Add routes at runtime:

await router.addRoute({
  name: 'billing',
  utterances: ['payment issue', 'invoice problem', 'billing question'],
  description: 'Billing and payment related queries',
});

LLM Fallback

For ambiguous queries, provide an LLM to classify:

const router = new SemanticRouter({
  routes,
  encoder: new LocalEncoder(),
  threshold: 0.4,
  llm: {
    name: 'claude',
    generate: async (prompt) => {
      // Your LLM call here
      return await callClaude(prompt);
    },
  },
});

API Reference

SemanticRouter

| Method | Description | |--------|-------------| | initialize() | Pre-encode all routes (call once) | | route(query) | Route a query, returns RouteMatch | | classify(query) | Shorthand, returns route name or null | | addRoute(route) | Add a route dynamically | | getStats() | Get router statistics | | isReady() | Check if initialized |

RouteMatch

interface RouteMatch {
  route: Route | null;    // Full route object
  name: string | null;    // Route name
  confidence: number;     // 0-1 score
  scores?: RouteScore[];  // All route scores
}

License

MIT