cosine

package
v1.4.13 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package cosine is a reference Selector implementation that ranks candidates by cosine similarity over embeddings produced by a PromptKit EmbeddingProvider.

This is the Go port of the in-tree EmbeddingSelector that shipped before #980 deleted it. It exists as an example, not as core code: PromptKit no longer ships any selector implementations beyond the exec client. Copy, adapt, or import directly.

Usage:

emb, _ := openai.NewEmbeddingProvider()
sel := cosine.New("skills_local", emb, cosine.Options{TopK: 5})
conv, _ := sdk.Open("./pack.json", "chat",
    sdk.WithSelector("skills_local", sel),
    sdk.WithRuntimeConfig("./runtime.yaml"), // spec.skills.selector: skills_local
)

The provider on SelectorContext (set by RAG / WithContextRetrieval) takes precedence over the one passed to New, which lets a single embedding instance back both RAG and selection. Pass nil to opt into context-driven embeddings exclusively.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// TopK caps the number of candidates returned. Zero falls back to
	// defaultTopK (10). Use a large value to effectively disable
	// truncation.
	TopK int
}

Options configures the cosine selector.

type Selector

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

Selector ranks candidates by cosine similarity. Embeddings for identical (Candidate.ID, Description) pairs are cached across Select calls; changing a description re-embeds the candidate.

func New

func New(name string, embeddings providers.EmbeddingProvider, opts Options) *Selector

New returns a Selector that uses the given embedding provider when SelectorContext doesn't supply one. Pass nil to require the context to provide a provider; Select returns an error otherwise.

func (*Selector) Init

func (s *Selector) Init(ctx selection.SelectorContext) error

Init records the embedding provider supplied by the host (typically the same instance configured for RAG). When non-nil it overrides the constructor-supplied provider so a single embedding pool serves both selection and retrieval.

func (*Selector) Name

func (s *Selector) Name() string

Name returns the selector's registered name.

func (*Selector) Select

func (s *Selector) Select(ctx context.Context, q selection.Query, candidates []selection.Candidate) ([]string, error)

Select embeds the query and the candidates, ranks by cosine similarity, and returns the top-K IDs. On any embedding failure it returns the error; the caller (skills executor) treats that as "include all eligible".

Jump to

Keyboard shortcuts

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