embedding

package
v0.137.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

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 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 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

Jump to

Keyboard shortcuts

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