Skip to main content

Installation

The embedding SDK is part of the main switchAILocal package:

Basic Usage

Initialize the Engine

Generate Single Embedding

Generate Batch Embeddings

Compute Similarity

Output:

API Reference

Engine Methods

NewEngine(cfg Config) (*Engine, error)

Creates a new embedding engine instance. Parameters:
  • cfg: Configuration with model and vocabulary paths
Returns:
  • *Engine: The engine instance
  • error: Any error during creation

Initialize(sharedLibPath string) error

Loads the ONNX model and prepares for inference. Parameters:
  • sharedLibPath: Path to ONNX Runtime library (empty for auto-detect)
Returns:
  • error: Any error during initialization

Embed(text string) ([]float32, error)

Generates embedding for a single text. Parameters:
  • text: Input text to embed
Returns:
  • []float32: 384-dimensional embedding vector
  • error: Any error during embedding

BatchEmbed(texts []string) ([][]float32, error)

Generates embeddings for multiple texts efficiently. Parameters:
  • texts: Slice of input texts
Returns:
  • [][]float32: Slice of embedding vectors
  • error: Any error during embedding

CosineSimilarity(a, b []float32) float64

Computes cosine similarity between two vectors. Parameters:
  • a: First embedding vector
  • b: Second embedding vector
Returns:
  • float64: Similarity score (0.0 to 1.0)

IsEnabled() bool

Checks if the engine is initialized and ready. Returns:
  • bool: true if ready for inference

GetDimension() int

Returns the embedding output dimension. Returns:
  • int: Dimension (384 for MiniLM)

Shutdown() error

Gracefully shuts down the engine and releases resources. Returns:
  • error: Any error during shutdown

Configuration

Config Struct

Default Paths

Constants

Error Handling

Performance Tips

Use BatchEmbed for multiple texts - It’s more efficient than calling Embed() multiple times.
Pre-compute embeddings - Generate and cache embeddings for static content at startup.
Normalize vectors - The engine automatically L2-normalizes outputs for optimal similarity computation.
Thread Safety - The engine is thread-safe and can handle concurrent requests, but avoid creating multiple engine instances.

Next Steps

Custom Providers

Integrate custom embedding models

Semantic Tier

Use embeddings for intelligent routing

Overview

Learn about embedding fundamentals

Go SDK

Embed switchAILocal in Go apps