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 ¶
- type Critic
- type LLMCritic
- func (c *LLMCritic) IsComplete(ctx context.Context, query string, response string) (bool, float32, error)
- func (c *LLMCritic) IsFaithful(ctx context.Context, response string, sources []rag.Document) (bool, float32, error)
- func (c *LLMCritic) IsRelevant(ctx context.Context, query string, doc rag.Document) (bool, float32, error)
- func (c *LLMCritic) NeedsRetrieval(ctx context.Context, query string) (bool, float32, error)
- type Option
- type Response
- type SelfRAG
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 (*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 验证回答忠实度
type Option ¶
type Option func(*SelfRAG)
Option SelfRAG 配置选项
func WithFaithfulnessThreshold ¶
WithFaithfulnessThreshold 设置忠实度阈值 默认值: 0.7
func WithRelevanceThreshold ¶
WithRelevanceThreshold 设置相关性阈值 默认值: 0.7
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 的响应
Click to show internal directories.
Click to hide internal directories.