embedding

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ThresholdAssign   = float32(0.40) // post lands in anchor's feed
	ThresholdFrontier = float32(0.25) // near-miss zone
	ThresholdSwallow  = float32(0.25) // below this = spam
	MaxAssignments    = 2             // top-2 anchors
	ProximityBoost    = float32(0.05) // pro user additive bonus
)

Variables

This section is empty.

Functions

func Blend

func Blend(a, b []float32, wa, wb float32) []float32

Blend returns a weighted combination of two vectors, normalized.

func BytesAsVec

func BytesAsVec(b []byte) []float32

BytesAsVec converts a raw byte blob back to a float32 vector.

func Cosine

func Cosine(a, b []float32) float32

Cosine returns the cosine similarity between two vectors.

func EffectiveAnchor

func EffectiveAnchor(static, centroid []float32) []float32

EffectiveAnchor computes normalize(0.5*static + 0.5*centroid). If centroid is nil (no posts yet), returns static as-is.

func Normalize

func Normalize(v []float32) []float32

Normalize performs L2 normalization on v in place and returns it.

func VecAsBytes

func VecAsBytes(v []float32) []byte

VecAsBytes converts a float32 vector to a raw byte blob (for DB storage).

Types

type Assignment

type Assignment struct {
	AnchorIndex int
	Similarity  float32
}

Assignment represents a post's assignment to an anchor.

func Assign

func Assign(postVec []float32, anchorVecs [][]float32, isPro bool) (assignments []Assignment, swallowed bool)

Assign returns the top-2 anchor assignments above ThresholdAssign. If isPro, adds ProximityBoost to all similarities before thresholding. Returns nil assignments and swallowed=true if all similarities are below ThresholdSwallow. Posts between ThresholdSwallow and ThresholdAssign are "frontier" (nil assignments, swallowed=false).

type Embedder

type Embedder interface {
	Embed(texts []string) ([][]float32, error)
	Dims() int
	Name() string // unique key for caching, e.g. "openai-3small-512"
}

Embedder produces vector embeddings from text.

func NewFromProvider

func NewFromProvider(provider, model, baseURL string) (Embedder, error)

NewFromProvider constructs an Embedder by provider name. "auto" (default) tries ollama first, falls back to openai. "ollama": model and baseURL are optional (defaults apply). "openai": reads OPENAI_API_KEY from environment.

type Match

type Match struct {
	Index      int
	Similarity float32
}

Match represents a candidate's index and its cosine similarity to a query.

func TopN

func TopN(query []float32, candidates [][]float32, n int) []Match

TopN returns the top-N candidates by cosine similarity to query.

type Ollama

type Ollama struct {
	// contains filtered or unexported fields
}

func NewOllama

func NewOllama(model, baseURL string) *Ollama

func (*Ollama) Dims

func (o *Ollama) Dims() int

func (*Ollama) Embed

func (o *Ollama) Embed(texts []string) ([][]float32, error)

func (*Ollama) Name

func (o *Ollama) Name() string

type OpenAI

type OpenAI struct {
	// contains filtered or unexported fields
}

func NewOpenAI

func NewOpenAI(apiKey string) *OpenAI

func (*OpenAI) Dims

func (o *OpenAI) Dims() int

func (*OpenAI) Embed

func (o *OpenAI) Embed(texts []string) ([][]float32, error)

func (*OpenAI) Name

func (o *OpenAI) Name() string

type Space

type Space struct {
	Slug        string `yaml:"slug"`
	Description string `yaml:"description"`
	Centroid    string `yaml:"centroid"`
}

Space is a single entry from spaces.yaml.

func LoadSpaces

func LoadSpaces(path string) ([]Space, error)

LoadSpaces parses a spaces.yaml file.

type SpaceIndex

type SpaceIndex struct {
	Spaces []Space
	// contains filtered or unexported fields
}

SpaceIndex holds parsed spaces with centroid embeddings for multiple embedders.

func LoadSpaceIndex

func LoadSpaceIndex(yamlPath, cacheDir string, embedders ...Embedder) (*SpaceIndex, error)

LoadSpaceIndex loads spaces.yaml and embeds centroids with every provided embedder. Each embedder's vectors are cached independently in cacheDir as "{name}.bin".

func (*SpaceIndex) EmbedderNames

func (idx *SpaceIndex) EmbedderNames() []string

EmbedderNames returns all loaded embedder names.

func (*SpaceIndex) Lookup

func (idx *SpaceIndex) Lookup(slug, embedderName string) (*Space, []float32)

Lookup returns the Space and its vector for a given slug and embedder.

func (*SpaceIndex) Vecs

func (idx *SpaceIndex) Vecs(embedderName string) [][]float32

Vecs returns the centroid vectors for a given embedder name.

Jump to

Keyboard shortcuts

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