selfrag

package
v0.5.11 Latest Latest
Warning

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

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

Documentation

Overview

Package selfrag 提供自我反思检索增强生成 (Self-RAG) 实现

Self-RAG 是一种先进的 RAG 策略,通过自我反思机制来提高生成质量:

  • 判断是否需要检索 (Retrieval Decision)
  • 评估检索结果的相关性 (Relevance Assessment)
  • 验证生成内容的忠实度 (Faithfulness Verification)
  • 检查回答的完整性 (Completeness Check)

参考论文: Self-RAG: Learning to Retrieve, Generate, and Critique through Self-Reflection

使用示例:

selfRAG := selfrag.New(
    retriever,
    llmProvider,
    selfrag.WithMaxRetries(3),
    selfrag.WithRelevanceThreshold(0.7),
)
response, err := selfRAG.Query(ctx, "用户问题")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Critic

type Critic interface {
	// NeedsRetrieval 判断是否需要检索
	// 返回是否需要检索以及置信度
	NeedsRetrieval(ctx context.Context, query string) (bool, float32, error)

	// IsRelevant 评估文档与查询的相关性
	// 返回是否相关以及相关性分数
	IsRelevant(ctx context.Context, query string, doc rag.Document) (bool, float32, error)

	// IsFaithful 验证回答是否忠实于来源文档
	// 返回是否忠实以及忠实度分数
	IsFaithful(ctx context.Context, response string, sources []rag.Document) (bool, float32, error)

	// IsComplete 检查回答是否完整回答了问题
	// 返回是否完整以及完整度分数
	IsComplete(ctx context.Context, query string, response string) (bool, float32, error)
}

Critic 批评器接口 负责评估检索和生成的各个方面

type LLMCritic

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

LLMCritic 基于 LLM 的批评器

func NewLLMCritic

func NewLLMCritic(provider llm.Provider) *LLMCritic

NewLLMCritic 创建 LLM 批评器

func (*LLMCritic) IsComplete

func (c *LLMCritic) IsComplete(ctx context.Context, query string, response string) (bool, float32, error)

IsComplete 检查回答完整性

func (*LLMCritic) IsFaithful

func (c *LLMCritic) IsFaithful(ctx context.Context, response string, sources []rag.Document) (bool, float32, error)

IsFaithful 验证回答忠实度

func (*LLMCritic) IsRelevant

func (c *LLMCritic) IsRelevant(ctx context.Context, query string, doc rag.Document) (bool, float32, error)

IsRelevant 评估文档相关性

func (*LLMCritic) NeedsRetrieval

func (c *LLMCritic) NeedsRetrieval(ctx context.Context, query string) (bool, float32, error)

NeedsRetrieval 判断是否需要检索

type Option

type Option func(*SelfRAG)

Option SelfRAG 配置选项

func WithCritic

func WithCritic(critic Critic) Option

WithCritic 设置批评器

func WithFaithfulnessThreshold

func WithFaithfulnessThreshold(threshold float32) Option

WithFaithfulnessThreshold 设置忠实度阈值 默认值: 0.7

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries 设置最大重试次数 默认值: 3

func WithRelevanceThreshold

func WithRelevanceThreshold(threshold float32) Option

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

func WithTopK

func WithTopK(k int) Option

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

type Response

type Response struct {
	// Content 生成的回答
	Content string `json:"content"`

	// Sources 使用的来源文档
	Sources []rag.Document `json:"sources,omitempty"`

	// NeedRetrieval 是否需要检索
	NeedRetrieval bool `json:"need_retrieval"`

	// RelevanceScores 各文档的相关性分数
	RelevanceScores map[string]float32 `json:"relevance_scores,omitempty"`

	// FaithfulnessScore 忠实度分数
	FaithfulnessScore float32 `json:"faithfulness_score"`

	// CompletenessScore 完整度分数
	CompletenessScore float32 `json:"completeness_score"`

	// Retries 重试次数
	Retries int `json:"retries"`
}

Response Self-RAG 的响应

type SelfRAG

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

SelfRAG 自我反思检索增强生成

工作流程:

  1. 接收用户查询
  2. 使用 Critic 判断是否需要检索
  3. 如需检索,执行检索并评估相关性
  4. 过滤低相关性文档
  5. 生成回答
  6. 验证回答的忠实度
  7. 检查回答的完整性
  8. 如不满足要求,重试或返回最佳结果

func New

func New(retriever rag.Retriever, provider llm.Provider, opts ...Option) *SelfRAG

New 创建 SelfRAG 实例

func (*SelfRAG) Query

func (s *SelfRAG) Query(ctx context.Context, query string) (*Response, error)

Query 执行 Self-RAG 查询

Jump to

Keyboard shortcuts

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