rag

package
v0.5.10 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: 4 Imported by: 0

Documentation

Overview

Package rag 提供 Hexagon AI Agent 框架的检索增强生成 (RAG) 系统

RAG 是一种将检索与生成结合的技术,让 AI Agent 能够基于外部知识库回答问题。 借鉴 LlamaIndex 的设计理念,提供完整的文档处理管道。

核心组件:

  • Document: 文档数据结构
  • Loader: 文档加载器(从文件、URL 等加载)
  • Splitter: 文档分割器(将长文档分割成小块)
  • Embedder: 向量生成器(将文本转换为向量)
  • Indexer: 索引器(将文档向量化并存储)
  • Retriever: 检索器(根据查询检索相关文档)
  • Reranker: 重排序器(对检索结果重新排序)
  • Synthesizer: 合成器(将检索结果与 LLM 结合生成答案)

使用示例:

engine := NewEngine(
    WithStore(vectorStore),
    WithEngineEmbedder(embedder),
)
docs, err := engine.Retrieve(ctx, "What is Go?")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	// ID 文档唯一标识
	ID string `json:"id"`

	// Content 文档内容
	Content string `json:"content"`

	// Metadata 文档元数据
	Metadata map[string]any `json:"metadata,omitempty"`

	// Embedding 文档向量(如果已生成)
	Embedding []float32 `json:"embedding,omitempty"`

	// Score 检索相关性分数(仅在检索结果中有效)
	Score float32 `json:"score,omitempty"`

	// Source 文档来源(文件路径、URL 等)
	Source string `json:"source,omitempty"`

	// CreatedAt 创建时间
	CreatedAt time.Time `json:"created_at,omitempty"`
}

Document 表示一个文档或文档片段

type Embedder

type Embedder interface {
	// Embed 将文本转换为向量
	Embed(ctx context.Context, texts []string) ([][]float32, error)

	// Dimension 返回向量维度
	Dimension() int
}

Embedder 是向量生成器接口

type Engine

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

Engine RAG 引擎 提供完整的 RAG 能力:文档加载、分割、索引、检索

func NewEngine

func NewEngine(opts ...EngineOption) *Engine

NewEngine 创建 RAG 引擎

func (*Engine) Clear

func (e *Engine) Clear(ctx context.Context) error

Clear 清空所有文档

func (*Engine) Count

func (e *Engine) Count(ctx context.Context) (int, error)

Count 返回文档数量

func (*Engine) Delete

func (e *Engine) Delete(ctx context.Context, ids []string) error

Delete 删除文档

func (*Engine) Index

func (e *Engine) Index(ctx context.Context, docs []Document) error

Index 实现 Indexer 接口

func (*Engine) IndexDocuments

func (e *Engine) IndexDocuments(ctx context.Context, docs []Document) error

IndexDocuments 索引文档列表

func (*Engine) Ingest

func (e *Engine) Ingest(ctx context.Context) error

Ingest 摄取文档 从 loader 加载文档,分割后索引到向量存储

func (*Engine) Query

func (e *Engine) Query(ctx context.Context, query string, opts ...RetrieveOption) (string, error)

Query 检索并返回格式化的上下文

func (*Engine) Retrieve

func (e *Engine) Retrieve(ctx context.Context, query string, opts ...RetrieveOption) ([]Document, error)

Retrieve 检索相关文档

type EngineOption

type EngineOption func(*Engine)

EngineOption Engine 配置选项

func WithEngineEmbedder

func WithEngineEmbedder(embedder Embedder) EngineOption

WithEngineEmbedder 设置向量生成器

func WithEngineMinScore

func WithEngineMinScore(score float32) EngineOption

WithEngineMinScore 设置默认最小分数

func WithEngineSplitter

func WithEngineSplitter(splitter Splitter) EngineOption

WithEngineSplitter 设置文档分割器

func WithEngineTopK

func WithEngineTopK(k int) EngineOption

WithEngineTopK 设置默认返回数量

func WithLoader

func WithLoader(loader Loader) EngineOption

WithLoader 设置文档加载器

func WithStore

func WithStore(store vector.Store) EngineOption

WithStore 设置向量存储

type Indexer

type Indexer interface {
	// Index 索引文档
	Index(ctx context.Context, docs []Document) error

	// Delete 删除文档
	Delete(ctx context.Context, ids []string) error

	// Clear 清空索引
	Clear(ctx context.Context) error

	// Count 返回文档数量
	Count(ctx context.Context) (int, error)
}

Indexer 是索引器接口 负责将文档向量化并存储

type Loader

type Loader interface {
	// Load 加载文档
	Load(ctx context.Context) ([]Document, error)

	// Name 返回加载器名称
	Name() string
}

Loader 是文档加载器接口 负责从各种来源加载文档

type Pipeline

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

Pipeline 是 RAG 处理管道

func NewPipeline

func NewPipeline(loader Loader, splitter Splitter, indexer Indexer, retriever Retriever) *Pipeline

NewPipeline 创建 RAG 管道

func (*Pipeline) Ingest

func (p *Pipeline) Ingest(ctx context.Context) error

Ingest 执行完整的文档摄取流程

func (*Pipeline) Query

func (p *Pipeline) Query(ctx context.Context, query string, opts ...RetrieveOption) ([]Document, error)

Query 执行查询

type RAG

type RAG interface {
	Indexer
	Retriever
}

RAG 是 RAG 系统的核心接口 组合了 Indexer 和 Retriever 的功能

type RetrieveConfig

type RetrieveConfig struct {
	// TopK 返回的文档数量
	TopK int

	// MinScore 最小相关性分数
	MinScore float32

	// Filter 元数据过滤条件
	Filter map[string]any
}

RetrieveConfig 是检索配置

type RetrieveOption

type RetrieveOption func(*RetrieveConfig)

RetrieveOption 是检索选项

func WithFilter

func WithFilter(filter map[string]any) RetrieveOption

WithFilter 设置元数据过滤

func WithMinScore

func WithMinScore(score float32) RetrieveOption

WithMinScore 设置最小分数阈值

func WithTopK

func WithTopK(k int) RetrieveOption

WithTopK 设置返回文档数量

type Retriever

type Retriever interface {
	// Retrieve 检索相关文档
	Retrieve(ctx context.Context, query string, opts ...RetrieveOption) ([]Document, error)
}

Retriever 是检索器接口 负责根据查询检索相关文档

type Splitter

type Splitter interface {
	// Split 分割文档
	Split(ctx context.Context, docs []Document) ([]Document, error)

	// Name 返回分割器名称
	Name() string
}

Splitter 是文档分割器接口 负责将长文档分割成适合向量化的小块

type VectorStore

type VectorStore interface {
	// Add 添加向量
	Add(ctx context.Context, docs []Document) error

	// Search 相似性搜索
	Search(ctx context.Context, embedding []float32, topK int, filter map[string]any) ([]Document, error)

	// Delete 删除向量
	Delete(ctx context.Context, ids []string) error

	// Clear 清空存储
	Clear(ctx context.Context) error

	// Count 返回文档数量
	Count(ctx context.Context) (int, error)
}

VectorStore 是向量存储接口

Directories

Path Synopsis
adw
Package adw 提供智能文档工作流(Agentic Document Workflows)功能
Package adw 提供智能文档工作流(Agentic Document Workflows)功能
extractor
Package extractor 提供文档信息提取功能
Package extractor 提供文档信息提取功能
validator
Package validator 提供文档验证功能
Package validator 提供文档验证功能
Package agentic 提供代理式检索增强生成 (Agentic RAG) 实现
Package agentic 提供代理式检索增强生成 (Agentic RAG) 实现
Package cache 提供 RAG 语义缓存功能
Package cache 提供 RAG 语义缓存功能
Package citation 提供引用追踪功能
Package citation 提供引用追踪功能
Package corrective 提供纠错检索增强生成 (Corrective RAG) 实现
Package corrective 提供纠错检索增强生成 (Corrective RAG) 实现
Package embedder 提供 RAG 系统的文本嵌入生成器
Package embedder 提供 RAG 系统的文本嵌入生成器
Package extractor 提供 RAG 系统的文档元数据提取器
Package extractor 提供 RAG 系统的文档元数据提取器
Package indexer 提供 RAG 系统的文档索引器
Package indexer 提供 RAG 系统的文档索引器
Package knowledge 提供知识图谱能力
Package knowledge 提供知识图谱能力
Package loader 提供 RAG 文档加载功能
Package loader 提供 RAG 文档加载功能
Package multimodal 提供多模态 RAG 支持
Package multimodal 提供多模态 RAG 支持
Package query 提供 RAG 系统的查询增强功能
Package query 提供 RAG 系统的查询增强功能
Package reranker 提供高级重排序算法
Package reranker 提供高级重排序算法
Package retriever 提供 RAG 系统的文档检索器
Package retriever 提供 RAG 系统的文档检索器
Package router 提供 RAG 系统的查询路由器
Package router 提供 RAG 系统的查询路由器
Package selfrag 提供自我反思检索增强生成 (Self-RAG) 实现
Package selfrag 提供自我反思检索增强生成 (Self-RAG) 实现
Package splitter 提供 RAG 系统的文档分割器
Package splitter 提供 RAG 系统的文档分割器
Package synthesizer 提供 RAG 响应合成能力
Package synthesizer 提供 RAG 响应合成能力

Jump to

Keyboard shortcuts

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