embed

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 14 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

Overview

Package embed defines the embedding providers the memory service uses to turn text into vectors: a search query on one side, the chunks of a concept on the other.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fake

type Fake struct {

	// Err is returned by Embed when it is not nil.
	Err error
	// contains filtered or unexported fields
}

Fake is a deterministic Provider for tests and CI: a hashed bag of words, so texts that share words land close together. It imitates a lexical embedder, not a semantic one — a quality claim needs a real model. Err, when set, makes Embed fail with it (the provider-down path).

func NewFake

func NewFake(dim int) *Fake

NewFake returns a Fake with the given dimension (<= 0 = 64).

func (*Fake) Dim

func (f *Fake) Dim() int

Dim returns the vector size.

func (*Fake) Embed

func (f *Fake) Embed(_ context.Context, texts []string, _ Kind) ([][]float32, error)

Embed hashes the words of every text into a unit vector. The first five runes of a word stand for the word, which is a crude stem in both Russian and English ("деплоится" and "деплой" collide).

func (*Fake) Name

func (f *Fake) Name() string

Name identifies the provider.

type Kind

type Kind int

Kind tells a provider which side of a search a text is on. Providers that need an instruction or an input_type for queries take it from the kind.

const (
	Query Kind = iota
	Document
)

Text kinds.

type OpenAI

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

OpenAI is a Provider over an OpenAI-compatible embeddings API.

func NewOpenAI

func NewOpenAI(cfg *OpenAIConfig) (*OpenAI, error)

NewOpenAI validates the configuration and returns the provider.

func (*OpenAI) Dim

func (p *OpenAI) Dim() int

Dim returns the stored vector size.

func (*OpenAI) Embed

func (p *OpenAI) Embed(ctx context.Context, texts []string, kind Kind) ([][]float32, error)

Embed sends the texts in batches, with the kind's prefix, and returns one vector per text in order. A rate limit, a server error or a network error is retried; a client error (bad key, unknown model) is returned at once.

func (*OpenAI) Name

func (p *OpenAI) Name() string

Name identifies the provider by its model.

type OpenAIConfig

type OpenAIConfig struct {
	// BaseURL is the API base, e.g. https://openrouter.ai/api/v1.
	BaseURL string
	APIKey  string
	Model   string
	// Dim is the stored vector size; sent as `dimensions` (MRL models).
	Dim int
	// QueryPrefix and DocPrefix are put before every text of that kind:
	// Qwen3-Embedding expects an instruction before a query, e5 "query:" and
	// "passage:".
	QueryPrefix string
	DocPrefix   string
	// InputType sends input_type "query" / "document": Perplexity's
	// embeddings refuse to answer without it.
	InputType bool
	// Batch is the number of texts per request (0 = 64).
	Batch int
	// Timeout is per request (0 = 30s).
	Timeout time.Duration
	// Client overrides the HTTP client (nil = Timeout applied).
	Client *http.Client
}

OpenAIConfig configures an OpenAI-compatible /embeddings endpoint (OpenRouter, Ollama's OpenAI layer, vLLM, ...).

type Provider

type Provider interface {
	Embed(ctx context.Context, texts []string, kind Kind) ([][]float32, error)
	Dim() int
	// Name identifies the provider and model; the service records it with
	// every bundle it indexes.
	Name() string
}

Provider embeds texts into vectors of Dim dimensions. Embeddings of the same text with the same provider are stable; switching provider or model means re-indexing, because vectors of different models are not comparable.

Jump to

Keyboard shortcuts

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