inmemory

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 15 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"

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 distance d to 1/(1+d), preserving magnitude differences.

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 a concurrency-safe, in-process vector store. Index snapshots documents and Search returns detached results in deterministic score order. Search scans the full index; all records are lost when the process exits.

func NewStore

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

func (*Store) Clear

func (s *Store) Clear()

Clear removes all records and resets the index embedding dimensions.

func (*Store) DeleteIDs

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

func (*Store) DeleteWhere

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

func (*Store) Index

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

func (*Store) Len

func (s *Store) Len() int

func (*Store) Search

type StoreConfig

type StoreConfig struct {
	EmbeddingModel embedding.Model

	Similarity Similarity
}

StoreConfig requires an embedding model. A nil Similarity selects CosineSimilarity.

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