docsearch

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 19 Imported by: ? 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 docsearch gives an assistant the product's documentation: the sections of a docsite.Site as two read-only tools, {prefix}docs_search and {prefix}docs_read, and the system prompt guidance that tells the model to look there first and cite what it used.

Search is keyword retrieval (docsite.Site.Search, BM25) from the moment the process starts, with no credential. With a Dir and an embedding credential a Corpus also keeps a vector index of the same sections and fuses the two rankings. The vectors live in a generation directory named by the embedding profile (provider, endpoint, model), so a change of model, even to one with the same dimension, is a new generation and never mixes with the old; the manifest beside the index records each section's content hash, so a restart or a rebuilt site re-embeds only what changed. Embedding uses the credential the host resolves for the corpus, for indexing and for queries alike, never the caller's own, so query and document vectors always come from the same model.

The host owns the Corpus: it builds one from its loaded site, starts it, hands it to ai/instance through Options.Docs, and closes it after the instance.

Index

Constants

View Source
const (
	// DefaultSyncInterval is how often Start re-checks the embedding
	// credential and the index when nothing is pending.
	DefaultSyncInterval = 10 * time.Minute
	// SyncBatch is how many sections one embedding round indexes before
	// the manifest is written, so an interrupted sync resumes there.
	SyncBatch = 16
	// Purpose is the rag collection purpose of a corpus index. The
	// collection's owner slot carries the project id: the index is
	// dedicated to the corpus and holds no tenant data.
	Purpose = "docs"
	// ManifestName is the file beside a generation's index.
	ManifestName = "manifest.json"
)
View Source
const (
	// SearchToolSuffix and ReadToolSuffix follow the instance's tool
	// prefix: "acme_docs_search", "acme_docs_read".
	SearchToolSuffix = "docs_search"
	ReadToolSuffix   = "docs_read"
	// MaxReadRunes bounds what docs_read returns in one call, under the
	// API tools' response budget so a long section still fits a turn.
	MaxReadRunes = 24000
	// MaxSearchResults bounds one search.
	MaxSearchResults = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Corpus

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

Corpus is one documentation set available to an assistant.

func New

func New(o Options) (*Corpus, error)

New builds a corpus over o.Site. It opens nothing; Sync or Start do.

func (*Corpus) Close

func (c *Corpus) Close() error

Close stops the background sync, waits for it, and closes the index.

func (*Corpus) Guidance

func (c *Corpus) Guidance(prefix string) string

Guidance is the system prompt text that tells the model when to use the tools and how to cite.

func (*Corpus) Search

func (c *Corpus) Search(ctx context.Context, query string, n int) ([]Hit, error)

Search ranks sections for query: the keyword ranking fused (reciprocal rank) with the vector ranking when the index has vectors. It returns at most n hits.

func (*Corpus) Site

func (c *Corpus) Site() *docsite.Site

Site returns the documentation.

func (*Corpus) SiteURL

func (c *Corpus) SiteURL() string

SiteURL returns where the site is served.

func (*Corpus) Start

func (c *Corpus) Start()

Start runs Sync now and then every SyncInterval until Close, so an embedding credential added later is picked up and a failed round is retried. It returns at once.

func (*Corpus) Status

func (c *Corpus) Status() Status

Status reports the corpus and its vector index.

func (*Corpus) Sync

func (c *Corpus) Sync(ctx context.Context) error

Sync brings the vector index up to date with the site in one pass: resolves the credential, opens (or switches to) the generation for its profile, embeds changed and new sections in batches, removes sections the site no longer has, and retires other generations once this one is complete. Without a credential it records that and returns nil.

func (*Corpus) Tools

func (c *Corpus) Tools(prefix string) []ai.Tool

Tools returns the corpus's two read-only tools, named with prefix.

type Hit

type Hit struct {
	Section *docsite.Section
	Snippet string
	// Score is the fused rank score; it orders hits and means nothing on
	// its own.
	Score float64
}

Hit is one search result.

type Options

type Options struct {
	// Site is the loaded documentation. Required.
	Site *docsite.Site
	// SiteURL is where the site is served, as the browser reaches it:
	// absolute ("https://host:9680/docs"), because the console that
	// shows the assistant may be on another origin than the product.
	// Required.
	SiteURL string
	// Dir holds the vector generations ({Dir}/{profile}/). Empty keeps the
	// corpus keyword-only.
	Dir string
	// Embedding resolves the corpus's embedding credential, for example
	// the admin account's default. ai.ErrNoCredential means none is
	// configured yet; Start tries again at the next interval. Nil keeps the
	// corpus keyword-only.
	Embedding func(ctx context.Context) (ai.Credential, error)
	// SyncInterval is the background sync period; zero means
	// DefaultSyncInterval.
	SyncInterval time.Duration
	// Logger receives operational lines; nil logs as docsearch/{project}.
	Logger *logger.Logger
}

Options configure New.

type Status

type Status struct {
	Project  string       `json:"project"`
	Title    string       `json:"title"`
	Revision string       `json:"revision"`
	SiteURL  string       `json:"site_url"`
	Pages    int          `json:"pages"`
	Sections int          `json:"sections"`
	Vectors  VectorStatus `json:"vectors"`
}

Status describes a corpus.

type VectorStatus

type VectorStatus struct {
	// Enabled is whether Dir and Embedding were configured.
	Enabled bool `json:"enabled"`
	// Ready is whether the active generation covers the site revision.
	Ready bool `json:"ready"`
	// Profile names the embedding provider and model of the active
	// generation.
	Profile string `json:"profile,omitempty"`
	// Indexed is how many sections the active generation holds.
	Indexed int `json:"indexed"`
	// Pending is how many sections still need embedding.
	Pending int `json:"pending"`
	// LastSync is when a sync last finished, successfully or not.
	LastSync time.Time `json:"last_sync,omitempty"`
	// Error is the last sync's failure, or why vectors are unavailable.
	Error string `json:"error,omitempty"`
}

VectorStatus describes the vector index.

Jump to

Keyboard shortcuts

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