corrective

package
v0.5.13 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package corrective 提供纠错检索增强生成 (Corrective RAG) 实现

Corrective RAG 是一种通过评估和纠错来提高检索质量的策略:

  • 评估主检索器的结果质量
  • 当质量不佳时触发备选检索器(如网络搜索)
  • 融合多个来源的结果

参考论文: Corrective Retrieval Augmented Generation

使用示例:

crag := corrective.New(
    primaryRetriever,
    corrective.WithFallbackRetriever(webSearchRetriever),
    corrective.WithRelevanceThreshold(0.5),
)
docs, err := crag.Retrieve(ctx, "用户问题")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CorrectiveRAG

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

CorrectiveRAG 纠错检索增强生成

工作流程:

  1. 使用主检索器检索
  2. 评估每个结果: Correct / Ambiguous / Incorrect
  3. 质量不佳时触发备选检索器
  4. 融合结果

func New

func New(primaryRetriever rag.Retriever, opts ...Option) *CorrectiveRAG

New 创建 CorrectiveRAG 实例

func (*CorrectiveRAG) Retrieve

func (c *CorrectiveRAG) Retrieve(ctx context.Context, query string, opts ...rag.RetrieveOption) ([]rag.Document, error)

Retrieve 执行纠错检索

type EvaluatedDocument

type EvaluatedDocument struct {
	rag.Document
	Evaluation EvaluationResult `json:"evaluation"`
}

EvaluatedDocument 带评估结果的文档

type EvaluationResult

type EvaluationResult string

EvaluationResult 评估结果类型

const (
	// ResultCorrect 正确/相关
	ResultCorrect EvaluationResult = "correct"
	// ResultAmbiguous 模糊/部分相关
	ResultAmbiguous EvaluationResult = "ambiguous"
	// ResultIncorrect 错误/不相关
	ResultIncorrect EvaluationResult = "incorrect"
)

type LLMEvaluator

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

LLMEvaluator 基于 LLM 的相关性评估器

func NewLLMEvaluator

func NewLLMEvaluator(provider llm.Provider) *LLMEvaluator

NewLLMEvaluator 创建 LLM 评估器

func (*LLMEvaluator) Evaluate

func (e *LLMEvaluator) Evaluate(ctx context.Context, query string, doc rag.Document) (EvaluationResult, float32, error)

Evaluate 评估文档相关性

type LLMQueryRewriter

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

LLMQueryRewriter 基于 LLM 的查询重写器

func NewLLMQueryRewriter

func NewLLMQueryRewriter(provider llm.Provider) *LLMQueryRewriter

NewLLMQueryRewriter 创建 LLM 查询重写器

func (*LLMQueryRewriter) Rewrite

func (r *LLMQueryRewriter) Rewrite(ctx context.Context, query string) (string, error)

Rewrite 重写查询

type Option

type Option func(*CorrectiveRAG)

Option CorrectiveRAG 配置选项

func WithAmbiguousThreshold

func WithAmbiguousThreshold(threshold float32) Option

WithAmbiguousThreshold 设置模糊阈值 默认值: 0.7

func WithEvaluator

func WithEvaluator(evaluator RelevanceEvaluator) Option

WithEvaluator 设置相关性评估器

func WithFallbackRetriever

func WithFallbackRetriever(retriever rag.Retriever) Option

WithFallbackRetriever 设置备选检索器

func WithLLM

func WithLLM(provider llm.Provider) Option

WithLLM 设置 LLM 提供者

func WithQueryRewriter

func WithQueryRewriter(rewriter QueryRewriter) Option

WithQueryRewriter 设置查询重写器

func WithRelevanceThreshold

func WithRelevanceThreshold(threshold float32) Option

WithRelevanceThreshold 设置相关性阈值 默认值: 0.5

func WithTopK

func WithTopK(k int) Option

WithTopK 设置检索文档数量 默认值: 5

type QueryRewriter

type QueryRewriter interface {
	// Rewrite 重写/优化查询
	Rewrite(ctx context.Context, query string) (string, error)
}

QueryRewriter 查询重写器接口

type RelevanceEvaluator

type RelevanceEvaluator interface {
	// Evaluate 评估文档与查询的相关性
	// 返回评估结果和分数
	Evaluate(ctx context.Context, query string, doc rag.Document) (EvaluationResult, float32, error)
}

RelevanceEvaluator 相关性评估器接口

Jump to

Keyboard shortcuts

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