rag

package
v0.1.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 19 Imported by: 1 module, 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

@facet: navigation/retrieval/registry-index @facet-role: related

Package rag turns a FACET registry into something a question can be asked of by meaning rather than by exact id.

@facet: navigation/retrieval/semantic-search @facet-role: primary

Index

Constants

View Source
const ConfigHint = facet.ConfigName

ConfigHint names the configuration file in error messages.

View Source
const DefaultBatch = 64

DefaultBatch is how many chunks one request carries.

View Source
const DefaultProvider = "local"

DefaultProvider is what a project gets when it configures nothing.

View Source
const DefaultTimeout = 60 * time.Second

DefaultTimeout bounds one embedding request.

View Source
const LocalDim = 512

LocalDim is the width of the built-in embedder's vectors.

View Source
const MaxChunkChars = 1400

MaxChunkChars is the soft size budget for one chunk.

Variables

View Source
var RecordFacets = []string{
	"intent",
	"contract",
	"verification",
	"observation",
	"observability",
	"knowledge",
	"lineage",
	"flags",
	"variants",
	"metadata",
}

RecordFacets are the top-level facets chunked out of a record, in the order they are indexed.

Functions

func Describe

func Describe(e Embedder) string

Describe renders an embedder's identity for a person to read.

Types

type Chunk

type Chunk struct {
	// ID is stable across rebuilds: same registry, same feature, same facet,
	// same part number yields the same id. That is what lets an index be
	// rebuilt incrementally later without renumbering everything.
	ID string `json:"id"`
	// Registry is the declaring registry's name, empty in a single-registry
	// project.
	Registry string `json:"registry,omitempty"`
	// Feature is the feature id this text belongs to, empty for a generated
	// document that names no feature.
	Feature string `json:"feature,omitempty"`
	// Kind is "record" for text taken from a FACET record, "doc" for text taken
	// from a generated article. A reader is entitled to know which it got.
	Kind string `json:"kind"`
	// Facet is the record facet the text came from — intent, contract,
	// verification, observation, knowledge, lineage — or "article" for a doc.
	Facet string `json:"facet"`
	// Path is repository-relative, so a hit names a file that is really there.
	Path string `json:"path"`
	// Part is the slice number when one facet was too long for a single chunk.
	Part int `json:"part,omitempty"`
	// Title is the one-line headline shown above a hit.
	Title string `json:"title,omitempty"`
	// Text is what was embedded, verbatim. Stored so a hit can be quoted rather
	// than summarised — the same discipline the rest of this toolchain applies
	// to generated prose.
	Text string `json:"text"`
}

Chunk is one embeddable unit of text and where it came from.

func Chunks

func Chunks(reg facet.Registry, multi bool) ([]Chunk, error)

Chunks builds every chunk for one registry: its records, then the generated documents its manifest claims.

type Embedder

type Embedder interface {
	// Embed returns one vector per input, in order. Implementations batch as
	// they see fit; the contract is only that the order is preserved.
	Embed(ctx context.Context, texts []string) ([][]float32, error)
	// Describe names what produced the vectors, as "provider" and "model".
	// The pair is written into the index so a query embedded by something else
	// is refused rather than answered wrongly.
	Describe() (provider, model string)
}

Embedder turns text into vectors.

@facet: navigation/retrieval/embedding-configuration @facet-role: primary

func Resolve

func Resolve(cfg facet.EmbeddingConfig) (Embedder, error)

Resolve turns configuration into an embedder.

type HTTPEmbedder

type HTTPEmbedder struct {
	BaseURL string
	Model   string
	// APIKey is the bearer token. Empty is valid and normal: a local Ollama
	// wants no key at all.
	APIKey string
	// Dimensions is passed through when non-zero, for the providers that let a
	// caller shorten the vector. Providers that do not understand it ignore it.
	Dimensions int
	// Batch is how many texts go in one request. Zero means the default.
	Batch  int
	Client *http.Client
}

HTTPEmbedder calls an OpenAI-compatible /embeddings endpoint.

func (*HTTPEmbedder) Describe

func (e *HTTPEmbedder) Describe() (string, string)

Describe names the provider kind and the model, so an index says what produced its vectors without carrying a key or a URL.

func (*HTTPEmbedder) Embed

func (e *HTTPEmbedder) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed sends the texts in batches and returns the vectors in input order.

type Hit

type Hit struct {
	Chunk
	// Score is cosine similarity in [-1, 1]. Vectors are stored normalised, so
	// this is a plain dot product and needs no division at query time.
	Score float32 `json:"score"`
}

Hit is one search result.

func Query

func Query(ctx context.Context, store *Store, embedder Embedder, question string, k int) ([]Hit, error)

Query embeds one question and searches a store.

type LocalEmbedder

type LocalEmbedder struct {
	// Dim is the vector width. Zero uses LocalDim.
	Dim int
}

LocalEmbedder is a lexical embedder that runs in this process.

@facet: navigation/retrieval/embedding-configuration @facet-role: related

func (*LocalEmbedder) Describe

func (l *LocalEmbedder) Describe() (string, string)

Describe names this embedder in the index, so a store built with it is visibly not a store built with a neural model.

func (*LocalEmbedder) Embed

func (l *LocalEmbedder) Embed(_ context.Context, texts []string) ([][]float32, error)

Embed hashes each text into a vector. It never fails and never blocks.

type Progress

type Progress func(phase string, done, total int)

Progress reports one step of a build. It is called from the goroutine doing the work, so a slow implementation slows the build.

type Store

type Store struct {
	// Provider and Model identify what produced the vectors. A query embedded
	// by a different model than the index is meaningless, and this is how that
	// is caught rather than silently returning nonsense.
	Provider string `json:"provider"`
	Model    string `json:"model"`
	Dim      int    `json:"dim"`
	BuiltAt  string `json:"builtAt"`
	// contains filtered or unexported fields
}

Store is an index held entirely in memory and searched by brute force.

func Build

func Build(ctx context.Context, registries []facet.Registry, embedder Embedder, report Progress) (*Store, error)

Build reads every registry into chunks, embeds them, and returns a store.

@facet: navigation/retrieval/registry-index @facet-role: related

func LoadStore

func LoadStore(path string) (*Store, error)

LoadStore reads an index from disk.

func NewStore

func NewStore(provider, model, builtAt string, chunks []Chunk, vectors [][]float32) (*Store, error)

NewStore builds a store from chunks and their vectors, normalising as it goes so that search never has to.

func (*Store) Chunks

func (s *Store) Chunks() []Chunk

Chunks exposes the stored chunks, for callers that report on the index rather than search it.

func (*Store) Len

func (s *Store) Len() int

Len reports how many chunks the store holds.

func (*Store) Save

func (s *Store) Save(path string) error

Save writes the store to one file.

func (*Store) Search

func (s *Store) Search(query []float32, k int) ([]Hit, error)

Search returns the k nearest chunks to a query vector, best first.

Jump to

Keyboard shortcuts

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