rag

package
v0.596.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package rag provides lexical retrieval over the bundled PromptZero documentation corpus. The agent uses it to ground tool arguments in authoritative reference material (cheat sheets, scenario recipes, tool documentation) without shipping an embedding provider or a separate retrieval service.

This is the lexical half of a future hybrid retrieval stack. BM25 covers rare-term queries where a dense embedding model would over-generalise — "OOK650Async", "ATQA", "T5577", "NTAG215" are all exact terms the operator will use verbatim, and BM25 rewards that. A dense-embedding layer and a cross-encoder reranker can be added later without changing this package's public surface; the hybrid merger simply interleaves results from both retrievers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Snippet

func Snippet(body, query string, maxLen int) string

Snippet renders a short excerpt of the document body around the first matching query term, up to maxLen characters. Falls back to the opening of the body when no query term is present (e.g. when the caller scored on title-only matches). Intended for shaping the search result payload the model sees.

Types

type Document

type Document struct {
	ID     string
	Title  string
	Body   string
	Source string
}

Document is one indexed unit. A document can be a full markdown file or a paragraph-sized chunk; the indexer doesn't care. Title is shown in result snippets; Source is the original path so the agent can read the full file if it wants more context.

type Hit

type Hit struct {
	Doc   Document
	Score float64
}

Hit is a ranked retrieval result.

type Index

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

Index is the immutable BM25 retriever. Build once at startup; Search is read-only and safe for concurrent use.

func BuildIndex

func BuildIndex(docs []Document) *Index

BuildIndex tokenises each document and precomputes term-frequency statistics. Runs in one pass over the corpus.

func DefaultIndex

func DefaultIndex() *Index

DefaultIndex builds an index over the bundled documentation corpus. Returns an empty index if the embed FS is unexpectedly missing files (keeps the tool path fail-closed — no panics at startup).

func (*Index) Search

func (i *Index) Search(query string, k int) []Hit

Search returns the top-K documents by BM25 score for the given query. A zero or negative k defaults to 5. An empty query returns nil. Ties break by document ID for determinism.

Jump to

Keyboard shortcuts

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