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 ¶
- 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) error
- func (s *Store) DeleteWhere(ctx context.Context, expr filter.Predicate) error
- func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) error
- func (s *Store) Len() int
- func (s *Store) Search(ctx context.Context, req *vectorstore.SearchRequest) (*vectorstore.SearchResponse, error)
- type StoreConfig
Examples ¶
Constants ¶
const Provider = "InMemory"
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 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 (*Store) Clear ¶
func (s *Store) Clear()
Clear removes all records and resets the index embedding dimensions.
func (*Store) DeleteWhere ¶
func (*Store) Index ¶
func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) error
func (*Store) Search ¶
func (s *Store) Search(ctx context.Context, req *vectorstore.SearchRequest) (*vectorstore.SearchResponse, error)
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