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.
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(inmemory.StoreConfig{EmbeddingModel: model})
if err != nil {
panic(err)
}
fmt.Println(store.Len())
}
Output: 0
Index ¶
- Constants
- Variables
- func CosineSimilarity(left, right []float64) vectorstore.Score
- func DotProductSimilarity(left, right []float64) vectorstore.Score
- func EuclideanSimilarity(left, right []float64) vectorstore.Score
- type Similarity
- type Store
- func (s *Store) Clear()
- func (s *Store) DeleteIDs(ctx context.Context, ids []string) (err error)
- func (s *Store) DeleteWhere(ctx context.Context, expr filter.Predicate) (err error)
- func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) (err error)
- func (s *Store) Len() int
- func (s *Store) Search(ctx context.Context, req *vectorstore.SearchRequest) (response *vectorstore.SearchResponse, err error)
- type StoreConfig
Examples ¶
Constants ¶
const Provider = "InMemory"
Provider names the backend in [vectorstore capabilities].
Variables ¶
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. It is cheaper than CosineSimilarity when vector magnitude is meaningful or embeddings are already normalized.
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 documents, embeds each upsert 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(config StoreConfig) (*Store, error)
func (*Store) DeleteWhere ¶
func (*Store) Index ¶
func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) (err error)
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