Semantic Space
Semantic space is a high-dimensional mathematical space where meaning is represented geometrically. Imagine a vast coordinate system where every point corresponds to a specific concept or idea. In this space, the sentence “I need help with my password” exists as a point near “Can’t log in to my account” but far from “What’s the weather forecast?” Semantic similarity becomes a measurable distance, transforming abstract meaning into computable relationships.- Each point (vector) represents the meaning of a piece of content
- Distance between points represents semantic difference
- Content with similar meanings cluster together, regardless of exact wording
Encoders
To place content in a semantic space, we need to convert it into vector representations – a process called encoding. This is handled by neural network models called bi-encoders (commonly known as embedding models or encoders) which:- Process the input content (text, image, etc.)
- Analyze its features and semantic properties
- Output a fixed-size vector of floating-point numbers (typically hundreds or thousands of dimensions)
[0.12, -0.34, 0.56, ...]
, while “What’s the temperature outside?” would produce a different but nearby vector, reflecting their similar meanings.
Semantic Router supports various encoder types:
- Dense encoders (like
OpenAIEncoder
orHuggingFaceEncoder
): Generate vectors where every dimension has a value, capturing complex semantic relationships - Sparse encoders (like
AurelioSparseEncoder
orBM25Encoder
): Generate vectors where most dimensions are zero, excelling at keyword matching and term frequency
Multimodal Routing
While text is the most common application, semantic routing works with any content that can be meaningfully encoded into vectors:- Images: Using encoders like
CLIPEncoder
orVitEncoder
, images can be placed in the same semantic space as text, enabling cross-modal comparisons and routing - Audio: Speech or sound can be encoded and routed based on content, tone, or other semantic attributes
- Hybrid content: Combinations of text, images, and other modalities can be encoded together or separately
Making Routing Decisions
Once content is encoded into vectors, semantic similarity is used to make routing decisions. This is typically calculated using mathematical operations like:- Cosine similarity: cos(θ) = (A·B)/(||A||·||B||)
- Euclidean distance: d(A,B) = √(Σ(Aᵢ-Bᵢ)²)
- Dot product: A·B = Σ(Aᵢ·Bᵢ)
Implementation Workflow
- Define routes: Create example utterances for each target category
- Select encoder: Choose based on content type and performance requirements
- Configure router: Connect encoder, routes, and vector index
- Implement handlers: Define logic for each route
- Process inputs: Transform, encode, and route based on similarity
Getting Started with Semantic Router
Semantic Router makes implementing these capabilities straightforward:- Define routes with example utterances representing the concepts you want to detect
- Choose an encoder appropriate for your content type and requirements
- Initialize a router that connects your encoder, routes, and an index for storing embeddings
- Route incoming content to appropriate handlers based on semantic similarity