engine

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCache                    = errors.New("invalid cache")
	ErrInvalidEmbeddingBase            = errors.New("invalid embedding base")
	ErrInvalidReturnableVectorQuantity = errors.New("invalid returnable vector quantity")
)
View Source
var (
	ErrReadingKnowledgeFile = errors.New("error reading knowledge file")
	ErrParsingKnowledgeFile = errors.New("error parsing knowledge file")
)
View Source
var (
	ErrNilEmbedder           = fmt.Errorf("base embedder cannot be nil")
	ErrUninitializedEmbedder = fmt.Errorf("progress embedder not initialized")
)
View Source
var (
	ErrEmptyRerankerBaseURL  = errors.New("reranker base URL cannot be empty")
	ErrEmptyRerankerModel    = errors.New("reranker model cannot be empty")
	ErrUninitializedReranker = errors.New("reranker not initialized")
	ErrNoRerankerResults     = errors.New("reranker returned no results")
	ErrInvalidRerankerIndex  = errors.New("reranker returned only invalid indexes")
)

Functions

func CountIndexableChunks

func CountIndexableChunks(chunks []models.KnowledgeChunk) int

CountIndexableChunks returns the number of non-empty chunks.

func ReadKnowledge

func ReadKnowledge(path string) ([]models.KnowledgeChunk, error)

ReadKnowledge reads and parses a knowledge base JSON file, returning expanded chunks.

Types

type CacheEmbedder

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

CacheEmbedder is a wrapper around an embeddings.Embedder that caches results.

func NewCacheEmbedder

func NewCacheEmbedder(
	base embeddings.Embedder,
	cache *cache.Embedding,
) (*CacheEmbedder, error)

NewCacheEmbedder creates a new CacheEmbedder.

func (*CacheEmbedder) EmbedDocuments

func (e *CacheEmbedder) EmbedDocuments(
	ctx context.Context,
	texts []string,
) ([][]float32, error)

func (*CacheEmbedder) EmbedQuery

func (e *CacheEmbedder) EmbedQuery(
	ctx context.Context,
	text string,
) ([]float32, error)

EmbedQuery embeds a query

type Config

type Config struct {
	TopK          int
	MinScore      float32
	ForceAlways   bool
	RetrievalMode string
	RerankerK     int
	ChunksLoaded  int
	KeywordDocs   []schema.Document
	ContextMax    int
	ChunkMax      int
	SystemPrompt  string
}

Config holds the configuration parameters for a RAGEngine.

type CrossEncoderConfig

type CrossEncoderConfig struct {
	BaseURL      string
	EndpointPath string
	APIKey       string
	Model        string
	Timeout      time.Duration
}

CrossEncoderConfig holds the configuration for a CrossEncoderReranker.

type CrossEncoderReranker

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

CrossEncoderReranker reranks documents using a cross-encoder HTTP API.

func NewCrossEncoderReranker

func NewCrossEncoderReranker(cfg CrossEncoderConfig) (*CrossEncoderReranker, error)

NewCrossEncoderReranker creates a CrossEncoderReranker from the given config.

func (*CrossEncoderReranker) Rerank

func (r *CrossEncoderReranker) Rerank(ctx context.Context, query string, docs []schema.Document, topK int) ([]schema.Document, error)

Rerank sends documents to the cross-encoder API and returns the top-K results sorted by relevance score in descending order.

type DocumentReranker

type DocumentReranker interface {
	Rerank(ctx context.Context, query string, docs []schema.Document, topK int) ([]schema.Document, error)
}

DocumentReranker reranks a set of candidate documents for a given query.

type ProgressEmbedder

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

ProgressEmbedder wraps an Embedder to process documents in parallel batches, logging progress as chunks are embedded.

func NewProgressEmbedder

func NewProgressEmbedder(base embeddings.Embedder, total, workers int) (*ProgressEmbedder, error)

NewProgressEmbedder creates a ProgressEmbedder with the given base embedder, expected total document count, and worker concurrency.

func (*ProgressEmbedder) EmbedDocuments

func (e *ProgressEmbedder) EmbedDocuments(
	ctx context.Context,
	texts []string,
) ([][]float32, error)

EmbedDocuments embeds all texts in parallel batches, logging progress for each chunk.

func (*ProgressEmbedder) EmbedQuery

func (e *ProgressEmbedder) EmbedQuery(ctx context.Context, text string) ([]float32, error)

EmbedQuery returns the embedding vector for a single query text.

type RAGEngine

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

RAGEngine retrieves relevant document chunks from a vector store and optionally reranks them before injecting into a prompt.

func NewRAGEngine

func NewRAGEngine(
	store vectorstores.VectorStore,
	reranker DocumentReranker,
	embCache *cache.Embedding,
	cfg Config,
) *RAGEngine

NewRAGEngine creates a RAGEngine with the given store, reranker, cache and config.

func (*RAGEngine) ChunksLoaded

func (r *RAGEngine) ChunksLoaded() int

ChunksLoaded returns the number of knowledge chunks loaded into the store.

func (*RAGEngine) EmbeddingCacheStats

func (r *RAGEngine) EmbeddingCacheStats() cache.CacheStats

EmbeddingCacheStats returns cache statistics for the embedding cache.

func (*RAGEngine) Query

func (r *RAGEngine) Query(ctx context.Context, query string) (string, bool, error)

Query retrieves relevant context for the given query and returns it formatted as a prompt block. The boolean return indicates whether any context was found.

func (*RAGEngine) SystemPrompt added in v0.0.3

func (r *RAGEngine) SystemPrompt() string

SystemPrompt returns the fixed system prompt context loaded at bootstrap.

type RetrievalMode

type RetrievalMode string

RetrievalMode defines the document retrieval strategy.

const (
	RetrievalDense          RetrievalMode = "dense"
	RetrievalReranker       RetrievalMode = "reranker"
	RetrievalHybrid         RetrievalMode = "hybrid"
	RetrievalHybridReranker RetrievalMode = "hybrid_reranker"
)

Jump to

Keyboard shortcuts

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