Getting Started
Install semantic-node-router and build your first semantic router in minutes
Prerequisites
- ✓Node.js 18.0.0 or higher
- ✓npm, yarn, pnpm, or bun
- ✓OpenAI API key (if using OpenAI encoder) or nothing for offline Transformers
Step 1: Installation
Install the core package:
npm install semantic-node-routerBoth embedding providers are optional peer dependencies. Choose and install the one you want to use:
☁️OpenAI (Cloud)
Higher accuracy, requires API key
# For OpenAI (cloud-based, requires API key)
npm install openai💻Transformers.js (Local)
Free, offline, no API key needed
# For Transformers.js (local, offline, no API key needed)
npm install @huggingface/transformersStep 2: Your First Router
Let's create a simple router that can handle greetings, farewells, and technical support queries.
import { Router, Route, OpenAIEncoder } from 'semantic-node-router';
// 1. Create an encoder
const encoder = new OpenAIEncoder({
apiKey: process.env.OPENAI_API_KEY,
model: 'text-embedding-3-small'
});
// 2. Define your routes
const routes = [
new Route({
name: 'greeting',
utterances: ['hello', 'hi there', 'hey', 'good morning']
}),
new Route({
name: 'farewell',
utterances: ['goodbye', 'bye', 'see you later']
}),
new Route({
name: 'technical_support',
utterances: [
'my app is crashing',
'I got an error',
'something is broken'
]
})
];
// 3. Create and initialize router
const router = new Router({
routes,
encoder
});
await router.initialize(); // Encodes all utterances
// 4. Route queries
const result = await router.route('The app is not working');
console.log(result);
// {
// route: 'technical_support',
// score: 0.87
// }How it works
- Encoder: Converts text to vector embeddings
- Routes: Define categories with example utterances
- Initialize: Pre-computes embeddings for all utterances
- Route: Finds the best matching route using cosine similarity
Alternative: Local/Offline Routing
Prefer to keep everything local? Use Transformers.js for completely offline routing:
import { Router, Route, TransformersEncoder } from 'semantic-node-router';
// Create and initialize encoder (loads model - one-time ~10s)
const encoder = new TransformersEncoder();
await encoder.initialize();
// Create router
const router = new Router({ routes, encoder });
await router.initialize();
// Route queries (fast! ~10-50ms)
const result = await router.route('hey there');
// { route: 'greeting', score: 0.92 }Benefits of Local Models
- ✓ Free: No API costs, unlimited requests
- ✓ Fast: 70x faster than OpenAI (10-50ms vs 350ms)
- ✓ Private: Data never leaves your machine
- ✓ Offline: Works without internet after initial download
Step 3: Configuration
Aggregation Methods
Choose how to combine similarity scores when a route has multiple utterances:
Score Thresholds
Set confidence thresholds to control routing sensitivity:
Next Steps
Read the Docs
Explore comprehensive documentation, best practices, and advanced features
API Reference
Detailed API documentation for Router, Route, and Encoder classes
View Examples
Real-world examples for customer support, chatbots, and more
Performance Benchmarks
See how semantic routing compares to LLM-based routing