corpus

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package corpus provides a reference memory.Retriever over a fixed set of documents — a knowledge base the host supplies, deliberately separate from the memory.Store the memory tools read and write.

That separation is the point. The two retrieval paths in PromptKit answer different questions:

  • The memory tools (memory__remember / memory__recall) let the model manage facts about the *subject* — what the user told it to remember. The model decides when to look, and the store is scoped per subject.
  • Ambient injection asks a corpus what is relevant to *this turn* and puts the answer in the system prompt before the model runs. The model never decides; it simply sees grounding it did not have to ask for.

Pointing ambient injection at the memory store would collapse the two into one confusing path — the model would find the same rows twice, once by asking and once without. Retrieval for grounding belongs over the host's own content: documentation, a product catalog, a support knowledge base.

This implementation scores documents by term overlap with the latest user turn. That is enough to develop and test against, and deliberately not a search engine: production hosts implement memory.Retriever against a vector index or a real search backend.

Index

Constants

View Source
const (
	// MemoryType is the [memory.Memory] Type given to every retrieved
	// document, so a context formatter (or a host reading the injected set)
	// can tell grounding documents apart from remembered facts.
	MemoryType = "document"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	// ID identifies the document; it is carried onto the retrieved
	// memory so a prompt can cite it.
	ID string
	// Title is a human-readable label, surfaced in metadata.
	Title string
	// Text is the content injected into the prompt.
	Text string
}

Document is one unit of retrievable host content.

type Option

type Option func(*Retriever)

Option configures a Retriever.

func WithTopK

func WithTopK(n int) Option

WithTopK caps how many documents are injected. Defaults to 3 — enough to ground an answer without crowding the system prompt.

type Retriever

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

Retriever answers ambient-injection queries from a fixed document set.

func New

func New(docs []Document, opts ...Option) *Retriever

New returns a Retriever over docs.

func (*Retriever) RetrieveContext

func (r *Retriever) RetrieveContext(
	_ context.Context, _ map[string]string, messages []types.Message,
) ([]*memory.Memory, error)

RetrieveContext implements memory.Retriever. The query is the latest user turn — what the assistant is about to answer.

A turn with no user text, or one sharing no term with any document, returns nothing rather than everything: injecting an unrelated document is worse than injecting none, because the model cannot tell that it was not chosen.

Jump to

Keyboard shortcuts

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