embed

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineSimilarity

func CosineSimilarity(a, b Vector) float64

CosineSimilarity computes cosine similarity between two vectors. Returns 0 if vectors have different lengths or either is zero.

func Normalize

func Normalize(v Vector)

Normalize normalizes a vector to unit length in-place. No-op for zero vectors.

Types

type Embedder

type Embedder interface {
	Embed(text string) (Vector, error)
	Model() string // returns the model identifier
}

Embedder generates embedding vectors from text.

type OllamaEmbedder

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

OllamaEmbedder calls the Ollama HTTP API for embeddings.

func NewOllama

func NewOllama(baseURL, model string) *OllamaEmbedder

NewOllama creates an Ollama embedder. baseURL is the Ollama API URL (e.g. "http://localhost:11434"). model is the embedding model name (e.g. "nomic-embed-text").

func (*OllamaEmbedder) Embed

func (o *OllamaEmbedder) Embed(text string) (Vector, error)

Embed sends a request to POST /api/embed and returns the vector.

func (*OllamaEmbedder) Model

func (o *OllamaEmbedder) Model() string

Model returns the model identifier.

type SearchHit

type SearchHit struct {
	ID         string
	Similarity float64
}

SearchHit is a search result with similarity score.

type Store

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

Store is an in-memory vector store with brute-force cosine search. Vectors are normalized on Add, so cosine similarity reduces to dot product.

func NewStore

func NewStore() *Store

NewStore creates a new vector store.

func (*Store) Add

func (s *Store) Add(id string, vec Vector)

Add adds or updates a document's vector. The vector is normalized before storage.

func (*Store) Count

func (s *Store) Count() int

Count returns the number of stored vectors.

func (*Store) ModelID

func (s *Store) ModelID() string

ModelID returns the model identifier used for stored vectors, or "" if not set.

func (*Store) Remove

func (s *Store) Remove(id string)

Remove removes a document's vector.

func (*Store) Search

func (s *Store) Search(query Vector, k int) []SearchHit

Search returns the top-k most similar documents to the query vector. Returns results sorted by descending similarity. Because stored vectors are normalized, cosine similarity == dot product.

func (*Store) SetModelID

func (s *Store) SetModelID(model string)

SetModelID sets the model identifier. If it changes, the caller is responsible for clearing and rebuilding the store.

type Vector

type Vector []float64

Vector is a float64 embedding vector.

Jump to

Keyboard shortcuts

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