vector

package
v0.59.0 Latest Latest
Warning

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

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

Documentation

Overview

Package vector adapts core vector search to the rag Retriever contract. Search policy is fixed at construction, and RetrieverConfig.FilterFunc is the only source of a retrieval's metadata filter.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

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

Encoder binds both halves of a compatible vector space. The Host identifies the deployed revisions and preprocessing; neither a model name nor a vector width is an adequate substitute for that binding.

func NewEncoder

func NewEncoder(config EncoderConfig) (*Encoder, error)

func (*Encoder) EncodeDocuments

func (e *Encoder) EncodeDocuments(ctx context.Context, texts []string) ([][]float64, error)

func (*Encoder) EncodeQuery

func (e *Encoder) EncodeQuery(ctx context.Context, text string, options vectorstore.SearchOptions) (*vectorstore.Query, error)

EncodeQuery returns coordinates that can be reused across compatible native indexes. Lexical text is retained only when the selected search requires it.

func (*Encoder) Space

func (e *Encoder) Space() vectorstore.SpaceID

type EncoderConfig

type EncoderConfig struct {
	Space     vectorstore.SpaceID
	Documents Encoding
	Queries   Encoding
}

type Encoding

type Encoding struct {
	Model   embedding.Model
	Options embedding.Options
}

Encoding binds the actual provider model and effective per-purpose options; asymmetric providers receive their native document or query input type here.

type Retriever

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

func NewRetriever

func NewRetriever(config RetrieverConfig) (*Retriever, error)

func (*Retriever) Retrieve

func (r *Retriever) Retrieve(ctx context.Context, query rag.Query) (rag.Candidates, error)

type RetrieverConfig

type RetrieverConfig struct {
	Encoder     *Encoder
	VectorStore Source

	// TopK caps the number of returned documents. Zero uses
	// [corevs.DefaultTopK]; negative values are invalid.
	TopK int

	// MinScore filters semantic matches below this relevance threshold. Hybrid
	// mode requires zero because fusion scores are not portable across stores.
	// Range [0.0, 1.0].
	MinScore corevs.Score

	// SearchMode selects semantic or native hybrid retrieval. The zero value is
	// semantic; a store that cannot honor hybrid returns a typed error.
	SearchMode corevs.SearchMode

	// FilterFunc builds each retrieval's metadata filter from the complete
	// query and is the filter's only source. A per-query filter is a
	// caller-owned [rag.ValueKey] read here, so it composes with fixed policy
	// such as tenant isolation instead of replacing it. Nil applies no filter;
	// an error fails the retrieval before the store is searched.
	FilterFunc func(ctx context.Context, query rag.Query) (filter.Predicate, error)
}
Example (FilterFunc)
package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/Tangerg/scope/core/embedding"
	corevs "github.com/Tangerg/scope/core/vectorstore"
	"github.com/Tangerg/scope/core/vectorstore/filter"
	"github.com/Tangerg/scope/rag"
	ragvectorstore "github.com/Tangerg/scope/rag/vector"
)

type filterPrinter struct{}

func (filterPrinter) Query(_ context.Context, request *corevs.Query) (*corevs.SearchResponse, error) {
	fmt.Println(request.Options.Filter)
	return &corevs.SearchResponse{}, nil
}

func main() {
	tenantKey, err := rag.NewValueKey[string]("tenant")
	if err != nil {
		panic(err)
	}
	retriever, err := ragvectorstore.NewRetriever(ragvectorstore.RetrieverConfig{
		Encoder: demoEncoder(), VectorStore: filterPrinter{},
		FilterFunc: func(_ context.Context, query rag.Query) (filter.Predicate, error) {
			tenant, found, valueErr := query.Value(tenantKey)
			if valueErr != nil {
				return nil, valueErr
			}
			if !found {
				return nil, errors.New("tenant is required")
			}
			return filter.EQ("tenant", tenant), nil
		},
	})
	if err != nil {
		panic(err)
	}
	query, err := rag.NewQuery("release notes")
	if err != nil {
		panic(err)
	}
	query, err = query.WithValue(tenantKey, "acme")
	if err != nil {
		panic(err)
	}
	if _, err := retriever.Retrieve(context.Background(), query); err != nil {
		panic(err)
	}
}

func (filterPrinter) CheckQuery(context.Context, corevs.SpaceID, corevs.SearchOptions) error {
	return nil
}

func demoEncoder() *ragvectorstore.Encoder {
	model := embedding.ModelFunc(func(_ context.Context, request *embedding.Request) (*embedding.Response, error) {
		return embedding.NewResponse([]*embedding.Output{{Embedding: []float64{1, 0}}}, nil)
	})
	binding := ragvectorstore.Encoding{Model: model}
	encoder, err := ragvectorstore.NewEncoder(ragvectorstore.EncoderConfig{Space: "example-v1", Documents: binding, Queries: binding})
	if err != nil {
		panic(err)
	}
	return encoder
}
Output:
tenant == 'acme'

type Source

type Source interface {
	corevs.Querier
	CheckQuery(context.Context, corevs.SpaceID, corevs.SearchOptions) error
}

Source admits the requested space and search policy before encoding the query.

Jump to

Keyboard shortcuts

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