inmemory

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package inmemory provides an in-process vector store backed by a map and a configurable similarity function. It is intended for demos, unit tests, and corpora that fit in RAM.

Every public method is safe for concurrent use. Reads take a read lock and writes take an exclusive lock. Embedding calls may perform provider I/O. Numeric filters preserve the values of integers and finite floating-point numbers when comparing across representations; numeric strings remain strings.

Records are not durable and disappear with the process.

Example
package main

import (
	"context"
	"fmt"

	"github.com/Tangerg/scope/core/embedding"
	"github.com/Tangerg/scope/core/vectorstore/inmemory"
)

func main() {
	model := embedding.ModelFunc(func(context.Context, *embedding.Request) (*embedding.Response, error) {
		return nil, nil
	})
	store, err := inmemory.NewStore(context.Background(), inmemory.StoreConfig{EmbeddingModel: model})
	if err != nil {
		panic(err)
	}
	fmt.Println(store.Len())
}
Output:
0

Index

Examples

Constants

View Source
const Provider = "InMemory"

Provider names the backend in [vectorstore capabilities].

Variables

View Source
var ErrMissingEmbeddingModel = errors.New("inmemory: embedding model is required")

Functions

func CosineSimilarity

func CosineSimilarity(left, right []float64) vectorstore.Score

CosineSimilarity is the default for StoreConfig.Similarity — cos(θ) mapped into [0, 1] via (1 + cos) / 2. Returns 0.5 (the "no information" midpoint) when either vector has zero magnitude rather than NaN.

func DotProductSimilarity

func DotProductSimilarity(left, right []float64) vectorstore.Score

DotProductSimilarity maps the unbounded inner product monotonically into the common score range when vector magnitude is meaningful. Products and their sum retain their exact values until the final score conversion.

func EuclideanSimilarity

func EuclideanSimilarity(left, right []float64) vectorstore.Score

EuclideanSimilarity maps Euclidean distance into [0, 1] via 1 / (1 + d). Useful when the embedding space is *not* angular and magnitude differences carry information.

Types

type Similarity

type Similarity func(left, right []float64) vectorstore.Score

Similarity scores two equal-length vectors; higher means more similar. Implementations must be deterministic and symmetric: Similarity(a, b) == Similarity(b, a). Returning vectorstore.Score keeps custom strategies inside the same normalized contract as every provider.

type Store

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

Store is the concurrency-safe reference implementation of the vector-store capability contracts. Index snapshots complete documents including Media, embeds their text once, and replaces records by caller-owned ID. Search snapshots results, evaluates the same filter AST exposed to external backends, and orders normalized scores deterministically. Deletes never expose the internal record map.

func NewStore

func NewStore(_ context.Context, config StoreConfig) (*Store, error)

NewStore builds the zero-dependency reference implementation. It exists so the shared conformance suite and callers' tests have a store with no external service, not as a production index: search is a linear scan over in-process state that is lost when the process exits.

The context is unused — there is no service to reach — and taken anyway so this store is a drop-in for a backend one, every vectorstore.Store implementation being constructed the same way.

func (*Store) Clear

func (s *Store) Clear()

func (*Store) DeleteIDs

func (s *Store) DeleteIDs(ctx context.Context, ids []string) (err error)

func (*Store) DeleteWhere

func (s *Store) DeleteWhere(ctx context.Context, expr filter.Predicate) (err error)

func (*Store) Index

func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) (err error)

func (*Store) Len

func (s *Store) Len() int

func (*Store) Search

func (s *Store) Search(ctx context.Context, req *vectorstore.SearchRequest) (response *vectorstore.SearchResponse, err error)

type StoreConfig

type StoreConfig struct {
	// EmbeddingModel embeds documents on Index and queries on Search.
	// Required.
	EmbeddingModel embedding.Model

	// Similarity is the function used to score retrieved documents
	// against the query embedding. Optional; defaults to
	// [CosineSimilarity]. Implementations must return higher-is-more-
	// similar.
	Similarity Similarity
}

StoreConfig fixes the two policies an in-memory store cannot infer per call: the required embedding model and the score function shared by indexing and retrieval. A nil Similarity selects CosineSimilarity; the model has no safe default because it determines vector shape and meaning.

func (StoreConfig) Validate

func (s StoreConfig) Validate() error

Jump to

Keyboard shortcuts

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