embed

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 10 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 generates and stores chunk embeddings for semantic search. Embeddings are produced locally via Ollama — code never leaves the machine, matching CLAUDE.md's non-negotiable on where model calls happen.

Index

Constants

View Source
const (
	DefaultBaseURL = "http://localhost:11434"
	DefaultModel   = "nomic-embed-text"
)

DefaultBaseURL and DefaultModel are what every ctx subcommand uses unless told otherwise — Ollama's standard local port, and the model migrations/002_vectors.sql's FLOAT[768] column was sized for.

View Source
const Dims = 768

Dims must match migrations/002_vectors.sql's chunk_vectors.embedding column (FLOAT[768]) — nomic-embed-text's native output size. Changing embedding models means a new column width and a full re-embed, per that migration's own comment; this constant is the one place that width is asserted in Go.

Variables

This section is empty.

Functions

func Drain

func Drain(ctx context.Context, db *sql.DB, client Embedder, batchSize int) (int, error)

Drain calls Run repeatedly until no pending chunk is left to embed and returns how many it embedded in total. On an error it returns the count so far along with the error, so callers can report partial progress.

Like emptying an inbox tray: keep taking a batch until the tray is empty. Chunks Run skips (a session with no project) stay pending but count as zero processed, so they end the loop instead of spinning it forever.

func Run

func Run(ctx context.Context, db *sql.DB, client Embedder, batchSize int) (processed int, err error)

Run processes up to batchSize pending chunks (chunks.embedded_at IS NULL — PendingEmbeddings is the embed worker's to-do list, per that query's own comment): embeds each with client, writes it into chunk_vectors, and marks the chunk embedded.

A chunk whose session never resolved a project is skipped, not embedded — chunk_vectors.project_id is vec0's PARTITION KEY and can't be NULL, and a session with no project is already a degenerate case (see internal/ingest/session.go). It stays pending; nothing currently clears it, so it needs project resolution fixed upstream rather than a fake value here.

One chunk failing to embed (a transient network error, model unloaded) stops the batch and returns the error — unlike ingest's per-line tolerance, there's no useful way to "skip and continue" a chunk that still needs embedding: it just stays pending and gets retried next Run.

func RunEvery

func RunEvery(ctx context.Context, db *sql.DB, client Embedder, batchSize int, interval time.Duration, logf func(format string, args ...any))

RunEvery drains the pending-chunk queue immediately and then again every interval until ctx is cancelled. It is meant to run as a daemon goroutine next to the ingest loop, so freshly ingested chunks become searchable without anyone remembering to run `ctx embed`.

An error (Ollama not running, model not pulled) is reported through logf and retried on the next tick rather than stopping the loop: the embedder is an optional service that can come and go, unlike the store the daemon cannot live without. The same error repeating is logged only once, so an Ollama that stays down for a day does not write a line every tick; recovery is logged when it comes back.

Types

type Embedder

type Embedder interface {
	Embed(ctx context.Context, text string) ([]float32, error)
	// Model identifies which model produced the vector, stored in
	// chunks.embedding_model so a future model switch can find and re-embed
	// everything embedded under the old one.
	Model() string
}

Embedder produces a vector for one chunk of text. An interface so the embed worker can be tested against a fake without a live Ollama, and so a different model/provider could be swapped in later without touching worker.go.

type OllamaClient

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

OllamaClient calls a local Ollama server's /api/embed endpoint (the current, batch-capable embeddings API — /api/embeddings is Ollama's older, deprecated single-text endpoint).

func NewOllamaClient

func NewOllamaClient(baseURL, model string) *OllamaClient

NewOllamaClient builds a client against baseURL (e.g. "http://localhost:11434") using model (e.g. "nomic-embed-text"). It doesn't contact the server — a bad baseURL or a missing model only surfaces on the first Embed call.

func (*OllamaClient) Embed

func (c *OllamaClient) Embed(ctx context.Context, text string) ([]float32, error)

Embed calls Ollama for a single text's embedding. It sends `input` as a single string rather than batching multiple chunks into one request — simpler error attribution (which chunk failed) at the cost of one HTTP round trip per chunk, an acceptable trade for a background worker that isn't latency-sensitive.

func (*OllamaClient) Model

func (c *OllamaClient) Model() string

Jump to

Keyboard shortcuts

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