Documentation
¶
Overview ¶
Package rag provides Self-RAG optimization.
Package rag provides Self-RAG optimization.
Package rag provides Self-RAG (Retrieval-Augmented Generation) optimization. It implements a lightweight, rule-driven approach to reduce unnecessary retrievals.
Package rag provides Self-RAG optimization.
Index ¶
- Constants
- func ShouldRerank(query string, results []*SearchResult) bool
- type EvaluationResult
- type HybridSearcher
- type RRFConfig
- type RerankDecider
- type ResultEvaluator
- type RetrievalDecider
- type RetrievalDecision
- type SearchResult
- type SearchStrategy
- type StrategyConfig
- type StrategySelector
- type SuggestedAction
Constants ¶
const ( UsefulScoreThreshold = 0.6 // Top1 score threshold for "useful" MinResultsForRerank = 5 // Minimum results to consider reranking ScoreDiffThreshold = 0.15 // Score difference threshold for reranking )
Constants for result evaluation.
const ( ReasonChitchat = "chitchat_detected" ReasonSystemCommand = "system_command" ReasonRetrievalTrigger = "retrieval_trigger" ReasonScheduleQuery = "schedule_query" ReasonDefault = "default" )
DecisionReason constants.
const (
RRFDampingFactor = 60 // k = 60 is a common default
)
RRF (Reciprocal Rank Fusion) constants.
Variables ¶
This section is empty.
Functions ¶
func ShouldRerank ¶
func ShouldRerank(query string, results []*SearchResult) bool
ShouldRerank is a convenience function.
Types ¶
type EvaluationResult ¶
type EvaluationResult struct {
Reason string
SuggestedAction SuggestedAction
TopScore float32
ScoreSpread float32
IsUseful bool
}
EvaluationResult contains the evaluation of retrieval results.
func EvaluateResults ¶
func EvaluateResults(results []*SearchResult) *EvaluationResult
EvaluateResults is a convenience function for evaluation.
type HybridSearcher ¶
type HybridSearcher struct {
// contains filtered or unexported fields
}
HybridSearcher performs hybrid search combining BM25 and vector search.
func NewHybridSearcher ¶
func NewHybridSearcher(strategy SearchStrategy) *HybridSearcher
NewHybridSearcher creates a new hybrid searcher.
func (*HybridSearcher) GetConfig ¶
func (h *HybridSearcher) GetConfig() StrategyConfig
GetConfig returns the current strategy configuration.
func (*HybridSearcher) MergeResults ¶
func (h *HybridSearcher) MergeResults(bm25Results, vectorResults []*SearchResult) []*SearchResult
MergeResults merges BM25 and vector results using the configured weights.
type RRFConfig ¶
type RRFConfig struct {
DampingFactor int
}
RRFConfig contains configuration for RRF fusion.
func DefaultRRFConfig ¶
func DefaultRRFConfig() RRFConfig
DefaultRRFConfig returns the default RRF configuration.
type RerankDecider ¶
type RerankDecider struct {
// contains filtered or unexported fields
}
RerankDecider decides whether to apply reranking.
func NewRerankDecider ¶
func NewRerankDecider() *RerankDecider
NewRerankDecider creates a new rerank decider.
func (*RerankDecider) ShouldRerank ¶
func (d *RerankDecider) ShouldRerank(query string, results []*SearchResult) bool
ShouldRerank determines if reranking should be applied.
type ResultEvaluator ¶
type ResultEvaluator struct {
// contains filtered or unexported fields
}
ResultEvaluator evaluates the usefulness of retrieval results.
func NewResultEvaluator ¶
func NewResultEvaluator() *ResultEvaluator
NewResultEvaluator creates a new result evaluator.
func (*ResultEvaluator) Evaluate ¶
func (e *ResultEvaluator) Evaluate(results []*SearchResult) *EvaluationResult
Evaluate evaluates the retrieval results.
type RetrievalDecider ¶
type RetrievalDecider struct {
// contains filtered or unexported fields
}
RetrievalDecider makes decisions about whether to retrieve.
func NewRetrievalDecider ¶
func NewRetrievalDecider() *RetrievalDecider
NewRetrievalDecider creates a new retrieval decider with default patterns.
func (*RetrievalDecider) Decide ¶
func (d *RetrievalDecider) Decide(query string) *RetrievalDecision
Decide determines whether retrieval is needed for a query.
type RetrievalDecision ¶
RetrievalDecision represents the decision on whether to retrieve.
func DecideRetrieval ¶
func DecideRetrieval(query string) *RetrievalDecision
DecideRetrieval is a convenience function for quick decisions.
type SearchResult ¶
SearchResult represents a single search result.
func FuseMultiple ¶
func FuseMultiple(resultLists [][]*SearchResult, weights []float64) []*SearchResult
FuseMultiple fuses multiple result lists using RRF. weights should have the same length as resultLists.
func FuseWithRRF ¶
func FuseWithRRF(bm25Results, vectorResults []*SearchResult, config StrategyConfig) []*SearchResult
RRF(d) = Σ weight_i / (k + rank_i(d)).
func FuseWithRRFConfig ¶
func FuseWithRRFConfig(bm25Results, vectorResults []*SearchResult, config StrategyConfig, rrfConfig RRFConfig) []*SearchResult
FuseWithRRFConfig fuses results with custom RRF configuration.
type SearchStrategy ¶
type SearchStrategy string
SearchStrategy represents the retrieval strategy to use.
const ( StrategyBM25Only SearchStrategy = "bm25_only" StrategySemanticOnly SearchStrategy = "semantic_only" StrategyHybridStandard SearchStrategy = "hybrid_standard" StrategyHybridBM25Heavy SearchStrategy = "hybrid_bm25_weighted" StrategyFullPipeline SearchStrategy = "full_pipeline" )
func SelectStrategy ¶
func SelectStrategy(intent router.Intent) SearchStrategy
SelectStrategy is a convenience function.
type StrategyConfig ¶
StrategyConfig contains weights for hybrid search.
func GetStrategyConfig ¶
func GetStrategyConfig(strategy SearchStrategy) StrategyConfig
GetStrategyConfig returns the configuration for a strategy.
type StrategySelector ¶
type StrategySelector struct{}
StrategySelector selects the appropriate search strategy.
func NewStrategySelector ¶
func NewStrategySelector() *StrategySelector
NewStrategySelector creates a new strategy selector.
func (*StrategySelector) Select ¶
func (s *StrategySelector) Select(intent router.Intent) SearchStrategy
Select chooses a search strategy based on intent.
type SuggestedAction ¶
type SuggestedAction string
SuggestedAction represents the recommended action after evaluation.
const ( ActionUse SuggestedAction = "use" // Use the results as-is ActionExpand SuggestedAction = "expand" // Expand/refine the query ActionDirect SuggestedAction = "direct" // Skip retrieval, answer directly )