knn

package
v0.207.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package knn implements a zero-training k-nearest-neighbor entry-tier pre-filter over embeddinggemma vectors of past inputs. It is a bridge before the LR router (internal/router) has enough rows to train: it predicts whether the small E2B tier will accept an input, from the E2B-accept labels the shadow-labeling flywheel already manufactures, embedded at drain time. Brute-force cosine; fail-open; nil-safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(path string, r Row) error

Append writes one row as a JSON line (O_APPEND, concurrency-safe). It creates the parent directory if needed.

Types

type Index

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

Index holds per-task labeled vectors loaded from a JSONL substrate. A nil *Index is safe to query (PreferLargerEntry returns no preference).

func Load

func Load(path string) *Index

Load reads the JSONL substrate at path. Returns nil if the file is absent or unreadable (caller treats nil as "no kNN preference"); malformed lines are skipped. A present-but-empty file yields a non-nil, empty Index.

func (*Index) PreferLargerEntry

func (ix *Index) PreferLargerEntry(task string, query []float64, k, minNeighbors int, threshold float64) (skip bool, ok bool)

PreferLargerEntry returns (skip, ok). skip=true means the kNN predicts E2B will NOT accept this input — the fraction of the k nearest same-task neighbors that accepted at E2B is below threshold — so the caller should skip E2B and enter at a larger tier. ok=false signals no usable signal (nil receiver, unknown task, empty query, or fewer than minNeighbors usable rows): the caller keeps the default entry. Rows whose vector dimension differs from the query are ignored.

type Row

type Row struct {
	Task   string    `json:"task"`
	Vec    []float64 `json:"vec"`
	Accept bool      `json:"accept"`
}

Row is one labeled substrate point: the embedding of a past input and whether the small E2B tier accepted it.

Jump to

Keyboard shortcuts

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