Documentation
¶
Overview ¶
Package semantic-router provides a semantic routing layer for LLM applications. It uses vector similarity to classify user queries and route them to appropriate handlers before invoking expensive LLM calls.
Index ¶
- func SparseSimilarity(a, b map[string]float64) float64
- type BM25Encoder
- type CachedEmbedder
- type Config
- type ConfigOption
- type Embedder
- type FunctionCall
- type FunctionHandler
- type FunctionRouter
- type FunctionSchema
- type HybridConfig
- type HybridConfigOption
- func WithDenseThreshold(threshold float64) HybridConfigOption
- func WithHybridAlpha(alpha float64) HybridConfigOption
- func WithHybridSimilarityFunc(fn core.SimilarityFunc) HybridConfigOption
- func WithHybridSparseThreshold(threshold float64) HybridConfigOption
- func WithHybridThreshold(threshold float64) HybridConfigOption
- type HybridEmbedder
- func (h *HybridEmbedder) Dimensions() int
- func (h *HybridEmbedder) Embed(ctx context.Context, text string) ([]float32, error)
- func (h *HybridEmbedder) EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)
- func (h *HybridEmbedder) EncodeSparse(text string) map[string]float64
- func (h *HybridEmbedder) EncodeSparseBatch(texts []string) []map[string]float64
- func (h *HybridEmbedder) GetAlpha() float64
- func (h *HybridEmbedder) HybridSimilarity(queryDense []float32, querySparse map[string]float64, compareDense []float32, ...) float64
- func (h *HybridEmbedder) SetAlpha(alpha float64)
- type HybridRouteResult
- type HybridRouter
- func (h *HybridRouter) Add(route *Route, sparseRoute *SparseRoute) error
- func (h *HybridRouter) AddBatch(denseRoutes []*Route, sparseRoutes []*SparseRoute) error
- func (h *HybridRouter) GetAlpha() float64
- func (h *HybridRouter) List() []string
- func (h *HybridRouter) Route(ctx context.Context, query string) (*HybridRouteResult, error)
- func (h *HybridRouter) RouteBatch(ctx context.Context, queries []string) ([]*HybridRouteResult, error)
- func (h *HybridRouter) SetAlpha(alpha float64)
- func (h *HybridRouter) Stats() map[string]interface{}
- type MockEmbedder
- type ParameterExtractor
- type ParameterSchema
- type RegexExtractor
- type Route
- type RouteHandler
- type RouteResult
- type RouteWithFunction
- type Router
- func (r *Router) Add(route *Route) error
- func (r *Router) AddBatch(routes []*Route) error
- func (r *Router) Get(name string) *Route
- func (r *Router) List() []string
- func (r *Router) Remove(name string) error
- func (r *Router) Route(ctx context.Context, query string) (*RouteResult, error)
- func (r *Router) RouteBatch(ctx context.Context, queries []string) ([]*RouteResult, error)
- func (r *Router) Stats() map[string]interface{}
- type SchemaBuilder
- func (b *SchemaBuilder) AddParameter(name, paramType, description string, required bool) *SchemaBuilder
- func (b *SchemaBuilder) AddParameterWithDefault(name, paramType, description string, required bool, defaultValue interface{}) *SchemaBuilder
- func (b *SchemaBuilder) AddParameterWithEnum(name, description string, required bool, enumValues []interface{}) *SchemaBuilder
- func (b *SchemaBuilder) Build() *FunctionSchema
- func (b *SchemaBuilder) SetHandler(handler FunctionHandler) *SchemaBuilder
- type SparseConfig
- type SparseConfigOption
- type SparseEncoder
- type SparseRoute
- type SparseRouter
- type TFIDFEncoder
- func (e *TFIDFEncoder) Dimensions() int
- func (e *TFIDFEncoder) EncodeSparse(text string) map[string]float64
- func (e *TFIDFEncoder) EncodeSparseBatch(texts []string) []map[string]float64
- func (e *TFIDFEncoder) Fit(ctx context.Context, documents []string) error
- func (e *TFIDFEncoder) Vocabulary() []string
- type TermFrequencyEncoder
- type TermScore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SparseSimilarity ¶
SparseSimilarity computes similarity between two sparse vectors. Uses cosine similarity for sparse representations.
Types ¶
type BM25Encoder ¶
type BM25Encoder struct {
// contains filtered or unexported fields
}
BM25Encoder implements BM25 (Best Matching 25) sparse encoding. BM25 improves upon TF-IDF by incorporating document length normalization.
func NewBM25Encoder ¶
func NewBM25Encoder() *BM25Encoder
NewBM25Encoder creates a new BM25 encoder with default parameters.
func NewBM25EncoderWithParams ¶
func NewBM25EncoderWithParams(k1, b float64) *BM25Encoder
NewBM25EncoderWithParams creates a new BM25 encoder with custom parameters.
func (*BM25Encoder) Dimensions ¶
func (e *BM25Encoder) Dimensions() int
Dimensions returns the size of the vocabulary.
func (*BM25Encoder) EncodeSparse ¶
func (e *BM25Encoder) EncodeSparse(text string) map[string]float64
EncodeSparse converts a document into a BM25 sparse vector.
func (*BM25Encoder) EncodeSparseBatch ¶
func (e *BM25Encoder) EncodeSparseBatch(texts []string) []map[string]float64
EncodeSparseBatch converts multiple documents into BM25 sparse vectors.
func (*BM25Encoder) Fit ¶
func (e *BM25Encoder) Fit(ctx context.Context, documents []string) error
Fit trains the BM25 encoder on a corpus of documents. This computes IDF values based on the document frequency of terms.
func (*BM25Encoder) Vocabulary ¶
func (e *BM25Encoder) Vocabulary() []string
Vocabulary returns the encoder's vocabulary.
type CachedEmbedder ¶
type CachedEmbedder struct {
// contains filtered or unexported fields
}
CachedEmbedder wraps another embedder and caches results.
func NewCachedEmbedder ¶
func NewCachedEmbedder(embedder Embedder) *CachedEmbedder
NewCachedEmbedder creates a new cached embedder.
func (*CachedEmbedder) CacheSize ¶
func (c *CachedEmbedder) CacheSize() int
CacheSize returns the number of cached embeddings.
func (*CachedEmbedder) ClearCache ¶
func (c *CachedEmbedder) ClearCache()
ClearCache clears the embedding cache.
func (*CachedEmbedder) Dimensions ¶
func (c *CachedEmbedder) Dimensions() int
Dimensions returns the embedding dimension.
func (*CachedEmbedder) Embed ¶
Embed returns a cached embedding if available, otherwise computes and caches it.
func (*CachedEmbedder) EmbedBatch ¶
EmbedBatch embeds multiple texts, using cache where available.
type Config ¶
type Config struct {
// Threshold is the minimum similarity score to consider a route matched (default: 0.82)
Threshold float64 `json:"threshold"`
// SimilarityFunc is the function used to compute vector similarity (default: cosine)
// Use core.CosineSimilarity, core.DotProduct, or core.EuclideanDist
SimilarityFunc core.SimilarityFunc `json:"-"`
// TopK is the number of top results to consider when finding routes (default: 1)
TopK int `json:"topK"`
// CacheEmbeddings enables caching of utterance embeddings (default: true)
CacheEmbeddings bool `json:"cacheEmbeddings"`
}
Config holds configuration for the semantic router.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a configuration with sensible defaults.
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a function that modifies router configuration.
func WithCacheEmbeddings ¶
func WithCacheEmbeddings(enabled bool) ConfigOption
WithCacheEmbeddings enables or disables embedding caching.
func WithSimilarityFunc ¶
func WithSimilarityFunc(fn core.SimilarityFunc) ConfigOption
WithSimilarityFunc sets the similarity function.
func WithThreshold ¶
func WithThreshold(threshold float64) ConfigOption
WithThreshold sets the similarity threshold for route matching.
func WithTopK ¶
func WithTopK(k int) ConfigOption
WithTopK sets the number of top results to consider.
type Embedder ¶
type Embedder interface {
// Embed converts a single text string into a vector embedding.
Embed(ctx context.Context, text string) ([]float32, error)
// EmbedBatch converts multiple text strings into vector embeddings.
EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)
// Dimensions returns the dimensionality of the embedding vectors.
Dimensions() int
}
Embedder is the interface for computing text embeddings.
type FunctionCall ¶
type FunctionCall struct {
// Function name
Name string `json:"name"`
// Extracted parameters
Arguments map[string]interface{} `json:"arguments"`
// Whether all required parameters were found
Complete bool `json:"complete"`
// Missing required parameters
Missing []string `json:"missing,omitempty"`
// Handler to execute
Handler FunctionHandler `json:"-"`
}
FunctionCall represents a function call result.
type FunctionHandler ¶
FunctionHandler executes a function with extracted parameters.
type FunctionRouter ¶
type FunctionRouter struct {
// contains filtered or unexported fields
}
FunctionRouter handles routing with function calling.
func NewFunctionRouter ¶
func NewFunctionRouter(baseRouter *Router) *FunctionRouter
NewFunctionRouter creates a new function router.
func (*FunctionRouter) Add ¶
func (fr *FunctionRouter) Add(route *RouteWithFunction) error
Add adds a route with function schema.
func (*FunctionRouter) Route ¶
func (fr *FunctionRouter) Route(ctx context.Context, text string) (*FunctionCall, error)
Route performs semantic routing and returns function call with extracted parameters.
type FunctionSchema ¶
type FunctionSchema struct {
// Name is the function identifier
Name string `json:"name"`
// Description of what the function does
Description string `json:"description"`
// Parameters schema for the function
Parameters map[string]*ParameterSchema `json:"parameters"`
// Required parameter names
Required []string `json:"required,omitempty"`
// Handler is the function to execute
Handler FunctionHandler `json:"-"`
// Extractor for parameter extraction (optional, uses default if nil)
Extractor ParameterExtractor `json:"-"`
}
FunctionSchema defines a callable function with structured parameters.
func ParseFunctionSchema ¶
func ParseFunctionSchema(jsonStr string) (*FunctionSchema, error)
ParseFunctionSchema parses a function schema from JSON.
func (*FunctionSchema) ToJSON ¶
func (s *FunctionSchema) ToJSON() (string, error)
ToJSON converts the schema to JSON.
type HybridConfig ¶
type HybridConfig struct {
// Alpha is weight for dense (0-1). Final = alpha*dense + (1-alpha)*sparse
Alpha float64
// Threshold for combined score
Threshold float64
// DenseThreshold for dense-only decisions
DenseThreshold float64
// SparseThreshold for sparse-only decisions
SparseThreshold float64
// Similarity function for dense vectors
SimilarityFunc core.SimilarityFunc
}
HybridConfig holds configuration for hybrid router.
func DefaultHybridConfig ¶
func DefaultHybridConfig() HybridConfig
DefaultHybridConfig returns default hybrid configuration.
type HybridConfigOption ¶
type HybridConfigOption func(*HybridConfig)
HybridConfigOption modifies hybrid router configuration.
func WithDenseThreshold ¶
func WithDenseThreshold(threshold float64) HybridConfigOption
WithDenseThreshold sets the dense threshold.
func WithHybridAlpha ¶
func WithHybridAlpha(alpha float64) HybridConfigOption
WithHybridAlpha sets the alpha weight.
func WithHybridSimilarityFunc ¶
func WithHybridSimilarityFunc(fn core.SimilarityFunc) HybridConfigOption
WithHybridSimilarityFunc sets the similarity function.
func WithHybridSparseThreshold ¶
func WithHybridSparseThreshold(threshold float64) HybridConfigOption
WithHybridSparseThreshold sets the sparse threshold for hybrid router.
func WithHybridThreshold ¶
func WithHybridThreshold(threshold float64) HybridConfigOption
WithHybridThreshold sets the combined threshold.
type HybridEmbedder ¶
type HybridEmbedder struct {
// contains filtered or unexported fields
}
HybridEmbedder combines a dense embedder with a sparse encoder.
func NewHybridEmbedder ¶
func NewHybridEmbedder(dense Embedder, sparse SparseEncoder) *HybridEmbedder
NewHybridEmbedder creates a new hybrid embedder.
func NewHybridEmbedderWithAlpha ¶
func NewHybridEmbedderWithAlpha(dense Embedder, sparse SparseEncoder, alpha float64) *HybridEmbedder
NewHybridEmbedderWithAlpha creates a new hybrid embedder with custom alpha.
func (*HybridEmbedder) Dimensions ¶
func (h *HybridEmbedder) Dimensions() int
Dimensions returns the dense embedding dimension.
func (*HybridEmbedder) Embed ¶
Embed computes dense embedding (for compatibility with Embedder interface).
func (*HybridEmbedder) EmbedBatch ¶
EmbedBatch computes dense embeddings in batch.
func (*HybridEmbedder) EncodeSparse ¶
func (h *HybridEmbedder) EncodeSparse(text string) map[string]float64
EncodeSparse computes sparse encoding.
func (*HybridEmbedder) EncodeSparseBatch ¶
func (h *HybridEmbedder) EncodeSparseBatch(texts []string) []map[string]float64
EncodeSparseBatch computes sparse encodings in batch.
func (*HybridEmbedder) GetAlpha ¶
func (h *HybridEmbedder) GetAlpha() float64
GetAlpha returns the current alpha value.
func (*HybridEmbedder) HybridSimilarity ¶
func (h *HybridEmbedder) HybridSimilarity( queryDense []float32, querySparse map[string]float64, compareDense []float32, compareSparse map[string]float64, similarityFunc core.SimilarityFunc, ) float64
HybridSimilarity computes combined similarity using both dense and sparse.
func (*HybridEmbedder) SetAlpha ¶
func (h *HybridEmbedder) SetAlpha(alpha float64)
SetAlpha updates the weight for combining dense and sparse similarities.
type HybridRouteResult ¶
type HybridRouteResult struct {
RouteResult
// DenseScore is the similarity from dense vector matching
DenseScore float64 `json:"denseScore"`
// SparseScore is the similarity from sparse keyword matching
SparseScore float64 `json:"sparseScore"`
// CombinedScore is the weighted combination score
CombinedScore float64 `json:"combinedScore"`
}
HybridRouteResult extends RouteResult with separate dense and sparse scores.
type HybridRouter ¶
type HybridRouter struct {
// contains filtered or unexported fields
}
HybridRouter combines dense vector embeddings with sparse keyword matching. It uses a weighted combination of both similarity scores for routing decisions.
func NewHybridRouter ¶
func NewHybridRouter( dense Embedder, sparse SparseEncoder, opts ...HybridConfigOption, ) (*HybridRouter, error)
NewHybridRouter creates a new hybrid router.
func (*HybridRouter) Add ¶
func (h *HybridRouter) Add(route *Route, sparseRoute *SparseRoute) error
Add adds a route to both dense and sparse routers.
func (*HybridRouter) AddBatch ¶
func (h *HybridRouter) AddBatch(denseRoutes []*Route, sparseRoutes []*SparseRoute) error
AddBatch adds multiple routes.
func (*HybridRouter) GetAlpha ¶
func (h *HybridRouter) GetAlpha() float64
GetAlpha returns the current alpha value.
func (*HybridRouter) List ¶
func (h *HybridRouter) List() []string
List returns all route names from both routers.
func (*HybridRouter) Route ¶
func (h *HybridRouter) Route(ctx context.Context, query string) (*HybridRouteResult, error)
Route performs hybrid routing using both dense and sparse.
func (*HybridRouter) RouteBatch ¶
func (h *HybridRouter) RouteBatch(ctx context.Context, queries []string) ([]*HybridRouteResult, error)
RouteBatch routes multiple queries.
func (*HybridRouter) SetAlpha ¶
func (h *HybridRouter) SetAlpha(alpha float64)
SetAlpha updates the alpha weight.
func (*HybridRouter) Stats ¶
func (h *HybridRouter) Stats() map[string]interface{}
Stats returns statistics about the hybrid router.
type MockEmbedder ¶
type MockEmbedder struct {
// contains filtered or unexported fields
}
MockEmbedder is a simple embedder for testing purposes. It generates deterministic pseudo-random vectors based on input text.
func NewMockEmbedder ¶
func NewMockEmbedder(dimensions int) *MockEmbedder
NewMockEmbedder creates a mock embedder for testing.
func (*MockEmbedder) Dimensions ¶
func (m *MockEmbedder) Dimensions() int
Dimensions returns the embedding dimension.
func (*MockEmbedder) EmbedBatch ¶
EmbedBatch generates embeddings for multiple texts.
type ParameterExtractor ¶
type ParameterExtractor interface {
Extract(ctx context.Context, text string, schema *FunctionSchema) (map[string]interface{}, []string, error)
}
ParameterExtractor extracts parameters from text based on schema.
type ParameterSchema ¶
type ParameterSchema struct {
// Type is the parameter type (string, integer, number, boolean, array, object)
Type string `json:"type"`
// Description of what the parameter represents
Description string `json:"description,omitempty"`
// Required indicates if this parameter is mandatory
Required bool `json:"required"`
// Default value (optional)
Default interface{} `json:"default,omitempty"`
// Enum for enum validation (optional)
Enum []interface{} `json:"enum,omitempty"`
// Properties for object type (optional)
Properties map[string]*ParameterSchema `json:"properties,omitempty"`
// Items for array type (optional)
Items *ParameterSchema `json:"items,omitempty"`
// Pattern for regex validation (optional)
Pattern string `json:"pattern,omitempty"`
// Minimum for numeric types
Minimum *float64 `json:"minimum,omitempty"`
// Maximum for numeric types
Maximum *float64 `json:"maximum,omitempty"`
}
ParameterSchema defines the structure for function parameters.
type RegexExtractor ¶
type RegexExtractor struct {
// contains filtered or unexported fields
}
RegexExtractor extracts parameters using regex patterns.
func NewRegexExtractor ¶
func NewRegexExtractor() *RegexExtractor
NewRegexExtractor creates a new regex-based parameter extractor.
func (*RegexExtractor) Extract ¶
func (r *RegexExtractor) Extract(ctx context.Context, text string, schema *FunctionSchema) (map[string]interface{}, []string, error)
Extract extracts parameters from text using regex patterns.
func (*RegexExtractor) RegisterPattern ¶
func (r *RegexExtractor) RegisterPattern(functionName, paramName, pattern string)
RegisterPattern registers a regex pattern for a parameter.
type Route ¶
type Route struct {
// Name is the unique identifier for this route
Name string `json:"name"`
// Utterances are example phrases that represent this route's intent
Utterances []string `json:"utterances"`
// Handler is an optional function to execute when this route is matched
Handler RouteHandler `json:"-"`
// Metadata stores additional information about the route
Metadata map[string]string `json:"metadata,omitempty"`
// contains filtered or unexported fields
}
Route represents a single semantic route with associated example utterances.
type RouteHandler ¶
RouteHandler is a function that handles a matched route. It receives the original query and the confidence score.
type RouteResult ¶
type RouteResult struct {
// RouteName is the name of the matched route, empty if no match
RouteName string `json:"routeName"`
// Score is the similarity score (0.0 to 1.0)
Score float64 `json:"score"`
// Matched indicates whether the score exceeded the threshold
Matched bool `json:"matched"`
// Handler is the matched route's handler function
Handler RouteHandler `json:"-"`
}
RouteResult represents the result of a routing decision.
type RouteWithFunction ¶
type RouteWithFunction struct {
*Route
FunctionSchema *FunctionSchema
}
RouteWithFunction extends Route with function calling capability.
func CreateRouteWithFunction ¶
func CreateRouteWithFunction(routeName string, utterances []string, schema *FunctionSchema) *RouteWithFunction
CreateRouteWithFunction creates a route with function calling capability.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router performs semantic routing using vector similarity.
func NewRouter ¶
func NewRouter(embedder Embedder, opts ...ConfigOption) (*Router, error)
NewRouter creates a new semantic router.
func NewRouterWithStore ¶
NewRouterWithStore creates a new semantic router with persistent storage. Routes and their embeddings are stored in the vector store for persistence.
func (*Router) RouteBatch ¶
RouteBatch routes multiple queries and returns results for each.
type SchemaBuilder ¶
type SchemaBuilder struct {
// contains filtered or unexported fields
}
SchemaBuilder helps build function schemas.
func NewSchemaBuilder ¶
func NewSchemaBuilder(name, description string) *SchemaBuilder
NewSchemaBuilder creates a new schema builder.
func (*SchemaBuilder) AddParameter ¶
func (b *SchemaBuilder) AddParameter(name, paramType, description string, required bool) *SchemaBuilder
AddParameter adds a parameter to the schema.
func (*SchemaBuilder) AddParameterWithDefault ¶
func (b *SchemaBuilder) AddParameterWithDefault(name, paramType, description string, required bool, defaultValue interface{}) *SchemaBuilder
AddParameterWithDefault adds a parameter with a default value.
func (*SchemaBuilder) AddParameterWithEnum ¶
func (b *SchemaBuilder) AddParameterWithEnum(name, description string, required bool, enumValues []interface{}) *SchemaBuilder
AddParameterWithEnum adds an enum parameter.
func (*SchemaBuilder) Build ¶
func (b *SchemaBuilder) Build() *FunctionSchema
Build returns the constructed schema.
func (*SchemaBuilder) SetHandler ¶
func (b *SchemaBuilder) SetHandler(handler FunctionHandler) *SchemaBuilder
SetHandler sets the function handler.
type SparseConfig ¶
SparseConfig holds configuration for sparse router.
func DefaultSparseConfig ¶
func DefaultSparseConfig() SparseConfig
DefaultSparseConfig returns default sparse router configuration.
type SparseConfigOption ¶
type SparseConfigOption func(*SparseConfig)
SparseConfigOption modifies sparse router configuration.
func WithSparseThreshold ¶
func WithSparseThreshold(threshold float64) SparseConfigOption
WithSparseThreshold sets the threshold for sparse router.
func WithSparseTopK ¶
func WithSparseTopK(k int) SparseConfigOption
WithSparseTopK sets the top-k for sparse router.
type SparseEncoder ¶
type SparseEncoder interface {
// EncodeSparse converts text into a sparse vector representation.
// Returns a map where keys are term identifiers and values are weights.
EncodeSparse(text string) map[string]float64
// EncodeSparseBatch converts multiple texts into sparse vectors.
EncodeSparseBatch(texts []string) []map[string]float64
// Vocabulary returns the encoder's vocabulary.
Vocabulary() []string
// Dimensions returns the size of the vocabulary (sparse vector dimensionality).
Dimensions() int
}
SparseEncoder converts text into sparse vector representations (mostly zeros). Sparse vectors are efficient for keyword matching and term frequency analysis.
type SparseRoute ¶
type SparseRoute struct {
Name string
Utterances []string
Handler RouteHandler
Metadata map[string]string
// contains filtered or unexported fields
}
SparseRoute represents a route for sparse encoding.
type SparseRouter ¶
type SparseRouter struct {
// contains filtered or unexported fields
}
SparseRouter handles routing using sparse encoders (BM25, TFIDF).
func NewLexicalRouter ¶ added in v2.18.0
func NewLexicalRouter(opts ...SparseConfigOption) (*SparseRouter, error)
NewLexicalRouter creates a sparse lexical router using a fit-free term frequency encoder. It does not require an embedding model or a training corpus.
func NewSparseRouter ¶
func NewSparseRouter(encoder SparseEncoder, opts ...SparseConfigOption) (*SparseRouter, error)
NewSparseRouter creates a new sparse router.
func (*SparseRouter) Add ¶
func (r *SparseRouter) Add(route *SparseRoute) error
Add adds a route to the sparse router.
func (*SparseRouter) Route ¶
func (r *SparseRouter) Route(ctx context.Context, query string) (*RouteResult, error)
Route performs sparse routing.
type TFIDFEncoder ¶
type TFIDFEncoder struct {
// contains filtered or unexported fields
}
TFIDFEncoder implements TF-IDF (Term Frequency-Inverse Document Frequency) sparse encoding.
func NewTFIDFEncoder ¶
func NewTFIDFEncoder() *TFIDFEncoder
NewTFIDFEncoder creates a new TF-IDF encoder.
func NewTFIDFEncoderWithSublinearTF ¶
func NewTFIDFEncoderWithSublinearTF() *TFIDFEncoder
NewTFIDFEncoderWithSublinearTF creates a new TF-IDF encoder with sublinear TF scaling.
func (*TFIDFEncoder) Dimensions ¶
func (e *TFIDFEncoder) Dimensions() int
Dimensions returns the size of the vocabulary.
func (*TFIDFEncoder) EncodeSparse ¶
func (e *TFIDFEncoder) EncodeSparse(text string) map[string]float64
EncodeSparse converts a document into a TF-IDF sparse vector.
func (*TFIDFEncoder) EncodeSparseBatch ¶
func (e *TFIDFEncoder) EncodeSparseBatch(texts []string) []map[string]float64
EncodeSparseBatch converts multiple documents into TF-IDF sparse vectors.
func (*TFIDFEncoder) Fit ¶
func (e *TFIDFEncoder) Fit(ctx context.Context, documents []string) error
Fit trains the TF-IDF encoder on a corpus of documents.
func (*TFIDFEncoder) Vocabulary ¶
func (e *TFIDFEncoder) Vocabulary() []string
Vocabulary returns the encoder's vocabulary.
type TermFrequencyEncoder ¶ added in v2.18.0
type TermFrequencyEncoder struct{}
TermFrequencyEncoder is a lightweight lexical encoder that does not require fitting. It is useful for no-embedder routing over short route utterances.
func NewTermFrequencyEncoder ¶ added in v2.18.0
func NewTermFrequencyEncoder() *TermFrequencyEncoder
NewTermFrequencyEncoder creates a no-fit lexical sparse encoder.
func (*TermFrequencyEncoder) Dimensions ¶ added in v2.18.0
func (e *TermFrequencyEncoder) Dimensions() int
Dimensions returns zero because this encoder is fit-free.
func (*TermFrequencyEncoder) EncodeSparse ¶ added in v2.18.0
func (e *TermFrequencyEncoder) EncodeSparse(text string) map[string]float64
EncodeSparse converts text into term-frequency weights.
func (*TermFrequencyEncoder) EncodeSparseBatch ¶ added in v2.18.0
func (e *TermFrequencyEncoder) EncodeSparseBatch(texts []string) []map[string]float64
EncodeSparseBatch converts multiple texts into term-frequency sparse vectors.
func (*TermFrequencyEncoder) Vocabulary ¶ added in v2.18.0
func (e *TermFrequencyEncoder) Vocabulary() []string
Vocabulary returns nil because this encoder is fit-free.