knowledge

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package knowledge provides a RAG knowledge base abstraction inspired by Agno's Knowledge protocol.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	ID       string         `json:"id"`
	Content  string         `json:"content"`
	Metadata map[string]any `json:"metadata,omitempty"`
	Score    float32        `json:"score,omitempty"`
}

Document represents a retrieved knowledge document.

type Knowledge

type Knowledge interface {
	// Load indexes all documents into the underlying store. Idempotent.
	Load(ctx context.Context) error

	// Search returns the top-k most relevant documents for the query.
	Search(ctx context.Context, query string, topK int) ([]Document, error)

	// Close releases resources.
	Close() error
}

Knowledge is the interface for pluggable knowledge sources. Implementations load documents, index them, and serve similarity searches.

type Option added in v0.5.0

type Option func(*VectorKnowledge)

Option configures a VectorKnowledge at construction time. Options are applied in order, so later options override earlier ones.

func WithChunking added in v0.5.0

func WithChunking(size, overlap int) Option

WithChunking configures document chunking used during indexing. Documents longer than size (measured in runes) are split into overlapping chunks. A size <= 0 disables chunking. Overlap is clamped to [0, size).

func WithEmbedBatchSize added in v0.5.0

func WithEmbedBatchSize(n int) Option

WithEmbedBatchSize bounds how many chunks are embedded per provider call during Load. A value <= 0 embeds everything in a single call.

func WithQueryCache added in v0.5.0

func WithQueryCache(capacity int, ttl time.Duration) Option

WithQueryCache configures the bounded LRU+TTL cache for query embeddings. A capacity <= 0 or ttl <= 0 disables the cache.

func WithScoreThreshold added in v0.5.0

func WithScoreThreshold(threshold float32) Option

WithScoreThreshold drops search results whose relevance score is below the threshold. A value <= 0 keeps all results.

func WithTopK added in v0.5.0

func WithTopK(k int) Option

WithTopK sets the default number of results returned by Search when the caller passes topK <= 0.

func WithoutQueryCache added in v0.5.0

func WithoutQueryCache() Option

WithoutQueryCache disables query-embedding caching.

type VectorKnowledge

type VectorKnowledge struct {
	Collection string
	Dimension  int
	Store      storage.VectorStore
	Embedder   model.EmbeddingsProvider
	EmbedModel string
	// contains filtered or unexported fields
}

VectorKnowledge implements Knowledge backed by a VectorStore and EmbeddingsProvider.

Indexing scales to large corpora: documents are split into overlapping chunks (each chunk carries metadata linking it to its source document) and embedded in bounded batches. Concurrent Load calls are serialized so indexing state is never corrupted or double-applied. Retrieval supports a configurable top-k and relevance threshold, and query embeddings are served from a bounded LRU+TTL cache so repeated queries are not re-embedded.

func NewVectorKnowledge

func NewVectorKnowledge(collection string, dimension int, store storage.VectorStore, embedder model.EmbeddingsProvider, embedModel string, opts ...Option) *VectorKnowledge

NewVectorKnowledge creates a vector-backed knowledge base. Behavior can be tuned with functional options; sensible defaults are applied when none are given (batched embedding, document chunking, a query-embedding cache, and a default top-k for retrieval).

func (*VectorKnowledge) AddDocuments

func (v *VectorKnowledge) AddDocuments(docs ...Document)

AddDocuments queues documents for indexing on next Load() call.

func (*VectorKnowledge) Close

func (v *VectorKnowledge) Close() error

func (*VectorKnowledge) Load

func (v *VectorKnowledge) Load(ctx context.Context) error

Load creates the collection and indexes all queued documents. It is idempotent and safe to call concurrently: calls are serialized so indexing state cannot be corrupted or double-applied.

func (*VectorKnowledge) Search

func (v *VectorKnowledge) Search(ctx context.Context, query string, topK int) ([]Document, error)

Search embeds the query and performs similarity search. When topK <= 0 the configured default is used. Results below the configured score threshold are dropped. Query embeddings are served from the cache when available.

Directories

Path Synopsis
Package loaders provides document loaders for different file formats.
Package loaders provides document loaders for different file formats.

Jump to

Keyboard shortcuts

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