vector

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package vector defines a lightweight vector-store API and SQLite-backed utilities used by this project. It includes:

  • Document model and Store interface
  • SQLiteStore: baseline durable storage for documents
  • Schema helpers to create a docs table
  • Embedding encoding (BLOB) and distance functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CosineSimilarity

func CosineSimilarity(a, b []float32) (float64, error)

CosineSimilarity computes the cosine similarity between two vectors. It returns an error if the vectors have different lengths or if either vector has zero magnitude.

func DecodeEmbedding

func DecodeEmbedding(b []byte) ([]float32, error)

DecodeEmbedding decodes a BLOB produced by EncodeEmbedding back into a slice of float32 values.

func EncodeEmbedding

func EncodeEmbedding(vec []float32) ([]byte, error)

EncodeEmbedding encodes a slice of float32 values into a BLOB representation suitable for storage in SQLite. The current encoding is a simple little-endian sequence of IEEE 754 float32 values without a length prefix; the length is derived from the BLOB size on decode.

func EnsureSchema

func EnsureSchema(db *sql.DB) error

EnsureSchema creates the base documents table in the provided database if it does not already exist. The initial schema is intentionally simple and can be evolved as the integration with Embedius/schema.Document matures.

func L2Distance

func L2Distance(a, b []float32) (float64, error)

L2Distance computes the Euclidean (L2) distance between two vectors. It returns an error if the vectors have different lengths.

Types

type Document

type Document struct {
	// ID is the logical identifier of the document. When empty on insert, the
	// store may generate one.
	ID string

	// Content holds the main text/body of the document.
	Content string

	// Metadata is an opaque JSON or structured payload associated with the
	// document. For now it is modeled as a raw string to avoid a dependency on a
	// particular JSON library.
	Metadata string

	// Embedding is the vector representation of the document content.
	Embedding []float32
}

Document represents a logical document stored in the vector store. It is a simplified placeholder that can be refined as the integration with Embedius/schema progresses.

type SQLiteStore

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

SQLiteStore is a minimal implementation of Store that uses a SQLite database for durable storage. Vector similarity is not yet implemented; SimilaritySearch currently returns the first k documents in insertion order. This provides a working baseline that can be extended with a real index in later phases.

func NewSQLiteStore

func NewSQLiteStore(db *sql.DB) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite-backed Store. It ensures the base docs schema exists in the provided database.

func (*SQLiteStore) AddDocuments

func (s *SQLiteStore) AddDocuments(ctx context.Context, docs []Document) ([]string, error)

AddDocuments inserts documents into the docs table. For this initial implementation, Document.ID must be non-empty; ID generation can be added later as needed.

func (*SQLiteStore) Remove

func (s *SQLiteStore) Remove(ctx context.Context, id string) error

Remove deletes a document by ID from the docs table. Vector index removal will be added once a real index is wired.

func (*SQLiteStore) SimilaritySearch

func (s *SQLiteStore) SimilaritySearch(ctx context.Context, queryEmbedding []float32, k int) ([]Document, error)

SimilaritySearch currently ignores the queryEmbedding and returns up to k documents from the docs table in insertion order. This will be replaced by a real vector search implementation in later phases.

type Store

type Store interface {
	// AddDocuments inserts documents into the store and returns their assigned
	// IDs. If a Document has ID set, implementations should attempt to honor it
	// (subject to uniqueness constraints).
	AddDocuments(ctx context.Context, docs []Document) ([]string, error)

	// SimilaritySearch performs a k-nearest-neighbour search using the provided
	// embedding as the query vector and returns up to k matching documents.
	SimilaritySearch(ctx context.Context, queryEmbedding []float32, k int) ([]Document, error)

	// Remove deletes or tombstones the document with the given ID from both the
	// underlying SQLite tables and the vector index.
	Remove(ctx context.Context, id string) error
}

Store defines the application-level vector store API. Implementations in this module will use SQLite for durable storage and a pure-Go index (e.g. Embedius+GDS) for kNN search.

Jump to

Keyboard shortcuts

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