Documentation
¶
Index ¶
- func GetChunks(content string, opts ...ChunkOption) ([]*core.Chunk, error)
- func GetFileChunks(file string, opts ...ChunkOption) ([]*core.Chunk, error)
- func NewFulltextIndexer(store core.FullTextStore) (core.Indexer, error)
- func NewFulltextIndexerWithFile(dbPath string) (core.Indexer, error)
- func NewSafeFulltextIndexer(dbPath string) (core.Indexer, error)
- func NewSemanticIndexer(db core.VectorStore, embedder core.Embedder, opts ...SemanticOption) core.Indexer
- type ChunkOption
- type CommunityIndexer
- type EntityDef
- type GraphIndexer
- func (g *GraphIndexer) Add(ctx context.Context, content string) ([]*core.Chunk, error)
- func (g *GraphIndexer) AddFile(ctx context.Context, filePath string) ([]*core.Chunk, error)
- func (g *GraphIndexer) Close(ctx context.Context) error
- func (g *GraphIndexer) Count(ctx context.Context) (int, error)
- func (g *GraphIndexer) DB() core.GraphStore
- func (g *GraphIndexer) GetChunks(ctx context.Context, docId string) ([]*core.Chunk, error)
- func (g *GraphIndexer) List(ctx context.Context, offset, limit int) ([]core.Hit, error)
- func (g *GraphIndexer) Name() string
- func (g *GraphIndexer) NewQuery(terms string) core.Query
- func (g *GraphIndexer) Remove(ctx context.Context, chunkID string) error
- func (g *GraphIndexer) Search(ctx context.Context, qry core.Query) ([]core.Hit, error)
- func (g *GraphIndexer) SearchByChunkIDs(ctx context.Context, chunkIDs []string, depth, limit int, ...) ([]core.Hit, error)
- func (g *GraphIndexer) SearchGlobal(ctx context.Context, query string, level int) ([]core.Hit, error)
- func (g *GraphIndexer) Type() string
- type GraphOption
- type GraphSearchResult
- type IndexData
- type LLMIndexer
- func (idx *LLMIndexer) Add(ctx context.Context, content string) ([]*core.Chunk, error)
- func (idx *LLMIndexer) AddFile(ctx context.Context, filePath string) ([]*core.Chunk, error)
- func (idx *LLMIndexer) Close(ctx context.Context) error
- func (idx *LLMIndexer) Count(ctx context.Context) (int, error)
- func (idx *LLMIndexer) CumulativeTokenUsage() *TokenUsage
- func (idx *LLMIndexer) GetChunks(ctx context.Context, docID string) ([]*core.Chunk, error)
- func (idx *LLMIndexer) LastTokenUsage() *TokenUsage
- func (idx *LLMIndexer) List(ctx context.Context, offset, limit int) ([]core.Hit, error)
- func (idx *LLMIndexer) Name() string
- func (idx *LLMIndexer) NewQuery(terms string) core.Query
- func (idx *LLMIndexer) Remove(ctx context.Context, chunkID string) error
- func (idx *LLMIndexer) Search(ctx context.Context, qry core.Query) ([]core.Hit, error)
- func (idx *LLMIndexer) SetEntityDefs(defs []EntityDef)
- func (idx *LLMIndexer) Type() string
- type LLMOption
- type ModelConfig
- type SemanticOption
- type TokenUsage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetChunks ¶
func GetChunks(content string, opts ...ChunkOption) ([]*core.Chunk, error)
GetChunks 根据文本内容进行结构化和分块 如果没有指定策略,会根据内容自动选择最佳分块策略 返回完整的分块数组
func GetFileChunks ¶
func GetFileChunks(file string, opts ...ChunkOption) ([]*core.Chunk, error)
GetFileChunks 根据文件路径进行结构化和分块 如果没有指定策略,会根据内容自动选择最佳分块策略 返回完整的分块数组
func NewFulltextIndexer ¶
func NewFulltextIndexer(store core.FullTextStore) (core.Indexer, error)
func NewFulltextIndexerWithFile ¶
NewFulltextIndexer 创建全文索引器
func NewSafeFulltextIndexer ¶
NewSafeFulltextIndexer 创建线程安全的全文索引器
func NewSemanticIndexer ¶
func NewSemanticIndexer(db core.VectorStore, embedder core.Embedder, opts ...SemanticOption) core.Indexer
Types ¶
type ChunkOption ¶
type ChunkOption func(*chunkOption)
ChunkOption 分块选项
func WithChunkLogger ¶ added in v1.3.0
func WithChunkLogger(logger logging.Logger) ChunkOption
WithChunkLogger attaches a logger to the chunking operation. The chunker emits a "chunker.parse" log when the source has been loaded and a "chunker.chunked" log when the chunk array is produced.
func WithChunkStrategy ¶
func WithChunkStrategy(strategy core.ChunkStrategy) ChunkOption
WithChunkStrategy 设置分块策略
type CommunityIndexer ¶
type CommunityIndexer struct {
// contains filtered or unexported fields
}
CommunityIndexer 社区索引器 负责:社区检测 → 摘要生成 → 存储 → Global Search 查询
func NewCommunityIndexer ¶
func NewCommunityIndexer(store core.GraphStore, client chat.Client, detector ...core.CommunityDetector) *CommunityIndexer
NewCommunityIndexer 创建社区索引器 detector 参数可选,如果为 nil 则使用默认的连通分量检测器
func (*CommunityIndexer) Build ¶
func (ci *CommunityIndexer) Build(ctx context.Context, maxLevel int) error
Build 执行完整的社区构建流程 1. 社区检测(纯图算法) 2. 社区摘要生成(LLM) 3. 摘要存储到图数据库 maxLevel: 层次聚类最大深度(0 = 单层)
func (*CommunityIndexer) SearchGlobal ¶
func (ci *CommunityIndexer) SearchGlobal(ctx context.Context, query string, level int) ([]core.CommunityMatch, error)
SearchGlobal 通过社区摘要回答全局性问题 (Global Search) 流程:获取社区摘要 → LLM Map-Reduce 生成答案 返回匹配的社区信息,不生成最终答案(答案生成由调用方完成)
type EntityDef ¶ added in v1.9.0
type EntityDef struct {
Prompt string // 实体类型描述文本(如 "**Person** — author, expert, contributor")
Schema string // JSON Schema 文本(如 `{"type":"object","properties":{...}}`),可选
}
EntityDef 定义一个实体类型的 Prompt 与 Schema,供 WithEntities 注入 LLM Prompt。 Schema 是可选字段;为空时不生成对应的 ### Entity Schema 段。 Prompt 写入 ### Entity Types 段,Schema 写入 ### Entity Schema 段。
func ParseEntityDefsYAML ¶ added in v1.8.0
ParseEntityDefsYAML 解析实体类型定义的 YAML 数据,返回 EntityDef 列表。 YAML 中每项支持两个输出字段:
- prompt:直接使用;为空时自动生成为 "**{Name}** — {Desc}"
- schema:可选字段,直接使用
type GraphIndexer ¶
type GraphIndexer struct {
// contains filtered or unexported fields
}
GraphIndexer 知识图谱索引器 与语义/全文索引器不同,GraphRAG 的索引是构建实体-关系图,查询是图遍历 支持两种查询模式: 1. 独立 GraphRAG 模式(需要 client):Query → LLM提取实体 → 图遍历 2. 混合模式(通过 Chunk IDs):Query → Semantic搜索 → Chunks → 关联 Nodes/Edges
func NewGraphIndexer ¶
func NewGraphIndexer(store core.GraphStore, opts ...any) *GraphIndexer
NewGraphIndexer 创建知识图谱索引器 client 参数可选:
- 提供 client:支持独立 GraphRAG 模式(索引 + 查询都用 LLM)
- 不提供 client:仅支持混合模式(索引时不用 LLM,查询时通过 Chunk IDs)
opts 可选配置,如 WithCache
func (*GraphIndexer) Add ¶
Add 从内容构建知识图谱(实现 core.Indexer 接口) 流程:分块 → LLM实体关系提取 → 图存储 注意:如果没有 client,会跳过实体提取
func (*GraphIndexer) Count ¶ added in v1.4.0
func (g *GraphIndexer) Count(ctx context.Context) (int, error)
Count returns the total number of indexed graph entities (nodes + edges).
func (*GraphIndexer) GetChunks ¶ added in v1.3.0
GetChunks returns empty — Graph indexer stores entities/relations, not raw chunks.
func (*GraphIndexer) List ¶ added in v1.3.0
List returns empty result — Graph indexer does not support chunk browsing.
func (*GraphIndexer) NewQuery ¶
func (g *GraphIndexer) NewQuery(terms string) core.Query
NewQuery 创建图查询(实现 core.Indexer 接口)
func (*GraphIndexer) Remove ¶
func (g *GraphIndexer) Remove(ctx context.Context, chunkID string) error
Remove 移除与指定 chunk 关联的所有实体和关系(实现 core.Indexer 接口)
func (*GraphIndexer) Search ¶
Search 执行图搜索(实现 core.Indexer 接口) 独立 GraphRAG 模式:LLM实体提取 → 图遍历 → 节点/边序列化 → Hit 注意:需要 client,否则返回空结果
func (*GraphIndexer) SearchByChunkIDs ¶
func (g *GraphIndexer) SearchByChunkIDs(ctx context.Context, chunkIDs []string, depth, limit int, edgeTypes ...[]string) ([]core.Hit, error)
SearchByChunkIDs 通过 Chunk IDs 查询关联的图结构 混合模式:由 HybridIndexer 调用,无需 LLM 流程:Chunk IDs → 查询关联 Nodes → 多跳遍历(可选边类型过滤) → 路径评分 → Hit
支持选项:
- depth: 遍历深度(默认 1,即直接邻居)
- limit: 返回结果数量上限
- edgeTypes: 关系类型过滤,仅遍历指定类型的边
func (*GraphIndexer) SearchGlobal ¶
func (g *GraphIndexer) SearchGlobal(ctx context.Context, query string, level int) ([]core.Hit, error)
SearchGlobal 通过社区摘要回答全局性问题 (Global Search) 适用于:"数据集主要讨论了哪些主题?" 这类宏观问题 需要 client 和已构建的社区摘要
type GraphOption ¶
type GraphOption func(*GraphIndexer)
GraphOption GraphIndexer 的可选配置
func WithCache ¶
func WithCache(cache core.CacheStore) GraphOption
WithCache 设置实体提取缓存(依赖注入 core.CacheStore 接口)
type GraphSearchResult ¶
type GraphSearchResult struct {
Query string `json:"query,omitempty"` // 原始查询
Entities []*core.Node `json:"entities,omitempty"` // 匹配的实体节点
Relations []*core.Edge `json:"relations,omitempty"` // 关联的边
ChunkIDs []string `json:"chunk_ids,omitempty"` // 关联的 chunk ID 列表
DocIDs []string `json:"doc_ids,omitempty"` // 关联的文档 ID 列表
}
GraphSearchResult 图搜索结果,用于序列化为 Hit.Content
type IndexData ¶ added in v1.5.0
type IndexData struct {
Chunks []struct {
Content string `json:"content"`
Metadata map[string]any `json:"metadata,omitempty"`
ChunkMeta struct {
Positions [][2]int `json:"positions"`
} `json:"chunk_meta,omitempty"`
} `json:"chunks"`
Entities []struct {
ID int `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Properties map[string]any `json:"properties,omitempty"`
} `json:"entities"`
Relations []struct {
Source int `json:"source"`
Target int `json:"target"`
Type string `json:"type"`
Predicate string `json:"predicate,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
} `json:"relations"`
}
IndexData LLM 返回的顶层 JSON 结构。 字段设计对齐 core.Chunk / core.Node / core.Edge,减少后处理映射。 ID 字段使用序数(1, 2, 3...),由 writeToStores 解析为真实存储 ID。
type LLMIndexer ¶ added in v1.5.0
type LLMIndexer struct {
// contains filtered or unexported fields
}
LLMIndexer 使用 LLM 进行文本分块、实体提取,并同时写入 VectorStore + GraphStore。 与 semanticIndexer / graphIndexer 平级,是 GoRAG 的第三个索引器实现。
数据流:
Add / AddFile → document (获取 docID) → Token 估算 → 未超限 → LLM (分块+实体提取) → 写入 vectorDB + graphDB → 超限 → 切片 → N 次 LLM 调用 → 合并结果 → 写入
func New ¶ added in v1.5.0
func New( model ModelConfig, embedder core.Embedder, vectorDB core.VectorStore, graphDB core.GraphStore, opts ...LLMOption, ) *LLMIndexer
New 创建 LLMIndexer
- model: LLM 模型连接配置(APIKey, BaseURL, Model, MaxTokens)
- embedder: 文本向量化引擎
- vectorDB: 向量存储(写入 Chunk 向量,用于语义检索)
- graphDB: 图存储(写入实体/关系,用于知识图谱检索)
- opts: 可选配置(WithLLMLogger 等)
func (*LLMIndexer) Add ¶ added in v1.5.0
Add 对一段文本执行 LLM 索引。
流程:document → Token 估算
- 未超限:单次 LLM 分块+实体提取 → 写入 vectorDB + graphDB
- 超限:按 80% maxTokens 切片 → 多次 LLM → 合并结果 → 写入
超短文本(< minContentLength 字符)会被静默丢弃。
func (*LLMIndexer) AddFile ¶ added in v1.5.0
AddFile 从文件读取内容后执行 LLM 索引。
流程:document.Open(文档读取 + 清洗)→ Token 估算
- 未超限:单次 LLM → 写入
- 超限:返回错误,要求用户手动拆分文件
超短文件(< minContentLength 字符)会被静默丢弃。
func (*LLMIndexer) Close ¶ added in v1.5.0
func (idx *LLMIndexer) Close(ctx context.Context) error
Close 关闭底层存储。
func (*LLMIndexer) Count ¶ added in v1.5.0
func (idx *LLMIndexer) Count(ctx context.Context) (int, error)
Count 返回 vectorDB 中的索引总数。
func (*LLMIndexer) CumulativeTokenUsage ¶ added in v1.9.0
func (idx *LLMIndexer) CumulativeTokenUsage() *TokenUsage
CumulativeTokenUsage 返回从创建/重置起累积的 Token 用量(多切片场景使用)。
func (*LLMIndexer) LastTokenUsage ¶ added in v1.5.0
func (idx *LLMIndexer) LastTokenUsage() *TokenUsage
LastTokenUsage 返回最近一次 LLM 调用的 Token 用量(单次值)。
func (*LLMIndexer) Name ¶ added in v1.5.0
func (idx *LLMIndexer) Name() string
func (*LLMIndexer) NewQuery ¶ added in v1.5.0
func (idx *LLMIndexer) NewQuery(terms string) core.Query
func (*LLMIndexer) Remove ¶ added in v1.5.0
func (idx *LLMIndexer) Remove(ctx context.Context, chunkID string) error
Remove 从 vectorDB 中移除 chunk 向量。 graphDB 中的关联节点/边不做级联删除(由 GraphIndexer 的 Cypher 语法处理)。
func (*LLMIndexer) Search ¶ added in v1.5.0
Search 执行向量检索(委托给 vectorDB)。 图检索的混合策略后续由 Query 对象设计时统一处理。
func (*LLMIndexer) SetEntityDefs ¶ added in v1.8.0
func (idx *LLMIndexer) SetEntityDefs(defs []EntityDef)
SetEntityDefs 运行时更新实体类型定义列表。 用于用户在界面上保存知识标签选择后,同步到正在运行的 LLMIndexer。 下次索引调用会使用新的实体定义。
func (*LLMIndexer) Type ¶ added in v1.5.0
func (idx *LLMIndexer) Type() string
type LLMOption ¶ added in v1.5.0
type LLMOption func(*LLMIndexer)
LLMOption configures an LLMIndexer.
func WithEntities ¶ added in v1.8.0
WithEntities 为 LLMIndexer 指定实体类型定义。 每项是一个 EntityDef(Prompt + Schema),会分别注入 Prompt 的 ### Entity Types 和 ### Entity Schema 段。多次调用会累积所有定义。 不调用该方法时,使用一组通用的默认实体类型。
func WithEntitiesFromFS ¶ added in v1.8.0
WithEntitiesFromFS 从文件系统(如 embed.FS)中读取匹配 glob 模式的实体类型配置文件, 解析后注入到 LLMIndexer。匹配多个文件时会合并所有实体类型定义。
用法:
//go:embed settings/entities-*.yml
var runtimeFS embed.FS
idx := New(cfg, embedder, vdb, gdb,
WithEntitiesFromFS(runtimeFS, "settings/entities-*.yml"),
)
func WithLLMLogger ¶ added in v1.5.0
WithLLMLogger attaches a logger to the LLMIndexer for observation logs.
type ModelConfig ¶ added in v1.5.0
type ModelConfig struct {
APIKey string
BaseURL string
Model string
Language string // 内容语言英文名(如 "Chinese", "English", "Japanese"),直接注入提示词
MaxTokens int // 模型的最大上下文窗口(token 数),0 表示使用默认值 128000
ThinkingBudget int // 思考模式的 token 预算(0 = 模型默认值),LLMIndexer 始终启用思考模式
}
ModelConfig LLM 模型连接配置
type SemanticOption ¶ added in v1.3.0
type SemanticOption func(*semanticIndexer)
SemanticOption configures a semantic indexer.
func WithLogger ¶ added in v1.3.0
func WithLogger(logger logging.Logger) SemanticOption
WithLogger attaches a logger to the semantic indexer for observation logs.
type TokenUsage ¶ added in v1.5.0
type TokenUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
TokenUsage 单次 LLM 调用的 Token 消耗