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 ¶
- type CorrectiveRAG
- type EvaluatedDocument
- type EvaluationResult
- type LLMEvaluator
- type LLMQueryRewriter
- type Option
- func WithAmbiguousThreshold(threshold float32) Option
- func WithEvaluator(evaluator RelevanceEvaluator) Option
- func WithFallbackRetriever(retriever rag.Retriever) Option
- func WithLLM(provider llm.Provider) Option
- func WithQueryRewriter(rewriter QueryRewriter) Option
- func WithRelevanceThreshold(threshold float32) Option
- func WithTopK(k int) Option
- type QueryRewriter
- type RelevanceEvaluator
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 纠错检索增强生成
工作流程:
- 使用主检索器检索
- 评估每个结果: Correct / Ambiguous / Incorrect
- 质量不佳时触发备选检索器
- 融合结果
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 评估器
type LLMQueryRewriter ¶
type LLMQueryRewriter struct {
// contains filtered or unexported fields
}
LLMQueryRewriter 基于 LLM 的查询重写器
func NewLLMQueryRewriter ¶
func NewLLMQueryRewriter(provider llm.Provider) *LLMQueryRewriter
NewLLMQueryRewriter 创建 LLM 查询重写器
type Option ¶
type Option func(*CorrectiveRAG)
Option CorrectiveRAG 配置选项
func WithAmbiguousThreshold ¶
WithAmbiguousThreshold 设置模糊阈值 默认值: 0.7
func WithFallbackRetriever ¶
WithFallbackRetriever 设置备选检索器
func WithQueryRewriter ¶
func WithQueryRewriter(rewriter QueryRewriter) Option
WithQueryRewriter 设置查询重写器
func WithRelevanceThreshold ¶
WithRelevanceThreshold 设置相关性阈值 默认值: 0.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 相关性评估器接口
Click to show internal directories.
Click to hide internal directories.