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
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
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
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
WithScoreThreshold drops search results whose relevance score is below the threshold. A value <= 0 keeps all results.
func WithTopK ¶ added in v0.5.0
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, draining the queue on success so a subsequent Load does not re-embed already-indexed documents. It is idempotent and safe to call concurrently: calls are serialized so indexing state cannot be corrupted or double-applied.