embed

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package embed generates sentence embeddings with a local ONNX model (all-MiniLM-L6-v2, 384-dim). Inference runs fully in-process against a single ONNX Runtime session — no network, no API key. The runtime library and model files are downloaded once (see download.go) and cached under the OS cache dir (see paths.go).

Index

Constants

View Source
const (
	ModelURL     = "https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx"
	TokenizerURL = "https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/tokenizer.json" //nolint:gosec // G101 false positive: a public asset URL, not a credential
)

ModelURL and TokenizerURL are where `semantic init` fetches the embedding model. ModelURL is the full model (~86MB); the quantized model (model_quantized.onnx) is ~23MB.

View Source
const OrtVersion = "1.24.1"

OrtVersion is the ONNX Runtime release that matches onnxruntime_go v1.27.0.

Variables

This section is empty.

Functions

func Check

func Check() error

Check reports whether the ONNX runtime library and model files are all present on disk, without initializing the runtime. Returns nil when embedding is ready, or an error naming the first missing piece — suitable for `semantic status`. Get performs the same checks lazily.

func CosineSim

func CosineSim(a, b Vec) float64

CosineSim returns cosine similarity in [-1, 1]. Vectors from Get are already L2-normalized, so for those this reduces to the dot product, but the full formula is kept so callers can pass un-normalized inputs.

func DownloadAll

func DownloadAll(logf func(string, ...any)) error

DownloadAll downloads the ONNX Runtime library and model files. logf receives progress messages.

func DownloadModel

func DownloadModel(logf func(string, ...any)) error

DownloadModel downloads and caches model.onnx and tokenizer.json.

func DownloadOrt

func DownloadOrt(logf func(string, ...any)) error

DownloadOrt downloads and caches the ONNX Runtime shared library.

func ModelCacheDir

func ModelCacheDir() string

ModelCacheDir returns the directory where model files are cached. $SEMANTIC_MODEL_DIR overrides.

func OrtCacheDir

func OrtCacheDir() string

OrtCacheDir returns the directory where the ONNX Runtime library is cached.

func OrtDownloadURL

func OrtDownloadURL() string

OrtDownloadURL returns the GitHub release URL for the ORT shared library archive for the current platform.

func OrtLibFilename

func OrtLibFilename() string

OrtLibFilename returns the platform-appropriate shared library filename.

func RepresentationID

func RepresentationID() string

RepresentationID names the vector space this package produces. The index stores it and rebuilds itself when it stops matching, so anything that changes what Get returns for the same input must be reflected here.

Every component is load-bearing:

  • the checkpoint, because different weights mean different vectors;
  • the pooling and normalization, because mean-vs-CLS pooling or dropping the L2 norm rewrites the space without changing its dimension;
  • the dimension, which is the one mismatch that would fail loudly anyway;
  • the sequence cap, because raising it changes the vector for every chunk long enough to have been truncated at the old cap — silently, and only for the long chunks, which is the worst kind of drift to debug.

It is a function rather than a const so it stays derived from the constants it names. A hand-written string would be free to drift from them, which is the exact failure this guards against.

Types

type Vec

type Vec []float32

Vec is a float32 embedding vector.

func Get

func Get(text string) (Vec, error)

Get returns a normalized embedding vector for text using the local ONNX model. Returns an error if the model is not installed — run the binary's `init` command to download it (~23 MB).

v1 has no warm daemon: every process pays the ONNX session cold-start (~800ms) on its first Get. The session then stays warm for the life of the process, so batch indexing amortizes the cost.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL