Documentation
¶
Index ¶
- func SplitText(text string, chunkSize, chunkOverlap int) []string
- type CombinedWeights
- type Config
- type DocumentModel
- type EmbeddingChunk
- type EmbeddingFunc
- type GORMDocStore
- func (d *GORMDocStore) Close() error
- func (d *GORMDocStore) DeleteCollection(ctx context.Context, collection string) error
- func (d *GORMDocStore) GetDocuments(ctx context.Context, collection string) ([]*schema.Document, error)
- func (d *GORMDocStore) RecallDocuments(ctx context.Context, collection string, query string, topK int) ([]*schema.Document, error)
- func (d *GORMDocStore) SaveDocuments(ctx context.Context, collection string, docs []*schema.Document) error
- type GORMStore
- func (s *GORMStore) ClearSession(ctx context.Context, sessionID string) error
- func (s *GORMStore) Close() error
- func (s *GORMStore) GetDB() *gorm.DB
- func (s *GORMStore) GetSession(ctx context.Context, sessionID string) ([]*schema.Message, error)
- func (s *GORMStore) GetSessionStats(ctx context.Context, sessionID string) (map[string]any, error)
- func (s *GORMStore) GetSessionWithReasoning(ctx context.Context, sessionID string) ([]*schema.Message, error)
- func (s *GORMStore) Save(ctx context.Context, sessionID string, msgs []*schema.Message) error
- func (s *GORMStore) SearchByRole(ctx context.Context, sessionID string, role schema.RoleType, limit int) ([]*schema.Message, error)
- func (s *GORMStore) SearchByTimeRange(ctx context.Context, sessionID string, start, end time.Time) ([]*schema.Message, error)
- type HNSWRetriever
- func (r *HNSWRetriever) AddToIndex(ctx context.Context, sessionID string, msgs []*schema.Message) error
- func (r *HNSWRetriever) Close() error
- func (r *HNSWRetriever) IndexReady() bool
- func (r *HNSWRetriever) RebuildIndex()
- func (r *HNSWRetriever) Recall(ctx context.Context, sessionID string, query string, topK int) ([]*schema.Message, error)
- func (r *HNSWRetriever) RemoveFromIndex(sessionID string) error
- type MessageModel
- type RecallMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CombinedWeights ¶
type CombinedWeights struct {
VectorWeight float64 // 向量相似度权重
KeywordWeight float64 // 关键词匹配权重
TimeWeight float64 // 时间衰减权重
}
CombinedWeights 组合召回的各因子权重,总和应为 1.0
func DefaultCombinedWeights ¶
func DefaultCombinedWeights() *CombinedWeights
DefaultCombinedWeights 默认组合权重
type Config ¶
type Config struct {
DBPath string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
LogLevel logger.LogLevel
DisableVectorSearch bool
EmbeddingDimension int
RecallMode RecallMode
CombinedWeights *CombinedWeights
ChunkSize int
ChunkOverlap int
IndexCachePath string
// HNSW 参数
HNSW_M int // 每节点最大双向链接数,默认 16
HNSW_Ml float64 // 层级生成因子,默认 0.25
HNSW_EfSearch int // 搜索宽度(越大越精确越慢),默认 200
// 召回参数
DefaultTopK int // 默认召回数量,默认 3
CombinedCandidateMult int // 组合召回候选倍数,默认 10
TimeDecayHalfLifeMs float64 // 时间衰减半衰期(毫秒),默认 7 天
IndexRebuildBatchSize int // 索引重建批次大小,默认 500
HybridTimeWeight float64 // 混合召回时间权重,默认 0.3
HybridKeywordWeight float64 // 混合召回关键词权重,默认 0.7
}
Config GORM 存储配置
type DocumentModel ¶
type DocumentModel struct {
ID string `gorm:"primaryKey;size:128"`
Collection string `gorm:"index:idx_collection;size:128;not null"`
Content string `gorm:"type:text;not null"`
Embedding string `gorm:"type:text"`
MetaData string `gorm:"type:text"` // JSON 序列化
Timestamp int64 `gorm:"not null"`
CreatedAt time.Time
}
DocumentModel GORM 通用文档模型 用于存储任意非对话数据
func (*DocumentModel) GetEmbedding ¶
func (m *DocumentModel) GetEmbedding() []float32
func (*DocumentModel) TableName ¶
func (m *DocumentModel) TableName() string
func (*DocumentModel) ToDocument ¶
func (m *DocumentModel) ToDocument() *schema.Document
type EmbeddingChunk ¶
type EmbeddingChunk struct {
ID string `gorm:"primaryKey;size:64"`
MessageID string `gorm:"index;size:64;not null"` // 关联 MessageModel.ID
SessionID string `gorm:"index;size:128;not null"` // 冗余字段,加速查询
ChunkIndex int `gorm:"not null"` // 块序号
Content string `gorm:"type:text;not null"` // 块文本
Embedding string `gorm:"type:text"` // 嵌入向量 JSON
}
EmbeddingChunk 分块嵌入记录,用于长文本分段存储
func (*EmbeddingChunk) GetEmbedding ¶
func (c *EmbeddingChunk) GetEmbedding() ([]float32, error)
func (*EmbeddingChunk) SetEmbedding ¶
func (c *EmbeddingChunk) SetEmbedding(vec []float32) error
func (*EmbeddingChunk) TableName ¶
func (c *EmbeddingChunk) TableName() string
type EmbeddingFunc ¶
EmbeddingFunc 文本嵌入函数签名 将文本转换为向量,用于语义相似度搜索
type GORMDocStore ¶
type GORMDocStore struct {
// contains filtered or unexported fields
}
GORMDocStore 基于 GORM 的通用文档存储
func NewGORMDocStore ¶
func NewGORMDocStore(store *GORMStore) *GORMDocStore
NewGORMDocStore 创建通用文档存储
func (*GORMDocStore) Close ¶
func (d *GORMDocStore) Close() error
func (*GORMDocStore) DeleteCollection ¶
func (d *GORMDocStore) DeleteCollection(ctx context.Context, collection string) error
func (*GORMDocStore) GetDocuments ¶
func (*GORMDocStore) RecallDocuments ¶
func (*GORMDocStore) SaveDocuments ¶
type GORMStore ¶
type GORMStore struct {
// contains filtered or unexported fields
}
GORMStore 基于 GORM 的消息持久化存储 负责 SQLite 持久化,包括嵌入生成和分块,但不管理向量索引
func NewGORMStore ¶
func NewGORMStore(config *Config, embedding EmbeddingFunc) (*GORMStore, error)
NewGORMStore 创建 GORM 存储
func (*GORMStore) ClearSession ¶
ClearSession 清空会话(硬删除)
func (*GORMStore) GetSession ¶
GetSession 获取完整会话历史
func (*GORMStore) GetSessionStats ¶
GetSessionStats 获取会话统计
func (*GORMStore) GetSessionWithReasoning ¶
func (s *GORMStore) GetSessionWithReasoning(ctx context.Context, sessionID string) ([]*schema.Message, error)
GetSessionWithReasoning 获取完整会话历史(含推理内容) 当前实现与 GetSession 相同,因为 MessageModel.ToSchemaMessage 已包含 ReasoningContent
func (*GORMStore) Save ¶
Save 保存消息(支持批量 + 嵌入生成) 每条消息使用递增时间戳,确保同一轮对话内的顺序正确 嵌入生成在事务外执行,避免网络 I/O 阻塞 SQLite 写锁
type HNSWRetriever ¶
type HNSWRetriever struct {
// contains filtered or unexported fields
}
HNSWRetriever 基于 HNSW 的向量检索器 负责向量索引管理和多种召回策略
func NewHNSWRetriever ¶
func NewHNSWRetriever(db *gorm.DB, embedding EmbeddingFunc, config *Config) *HNSWRetriever
NewHNSWRetriever 创建 HNSW 检索器 db 应来自 GORMStore.GetDB(),共享同一数据库连接
func (*HNSWRetriever) AddToIndex ¶
func (r *HNSWRetriever) AddToIndex(ctx context.Context, sessionID string, msgs []*schema.Message) error
AddToIndex 将消息的嵌入向量添加到索引
func (*HNSWRetriever) Recall ¶
func (r *HNSWRetriever) Recall(ctx context.Context, sessionID string, query string, topK int) ([]*schema.Message, error)
Recall 智能召回(多策略混合)
func (*HNSWRetriever) RemoveFromIndex ¶
func (r *HNSWRetriever) RemoveFromIndex(sessionID string) error
RemoveFromIndex 从索引中移除指定会话的所有向量
type MessageModel ¶
type MessageModel struct {
ID string `gorm:"primaryKey;size:64"`
SessionID string `gorm:"index:idx_session_time,priority:1;size:128"`
Role string `gorm:"size:32;not null"`
Content string `gorm:"type:text;not null"`
ReasoningContent string `gorm:"type:text"`
Embedding string `gorm:"type:text"` // JSON 序列化的向量(空字符串表示无嵌入)
Timestamp int64 `gorm:"index:idx_session_time,priority:2;not null"`
Metadata string `gorm:"type:text"` // JSON 序列化的元数据
CreatedAt time.Time
}
MessageModel GORM 消息模型
func (*MessageModel) GetEmbedding ¶
func (m *MessageModel) GetEmbedding() ([]float32, error)
GetEmbedding 获取嵌入向量
func (*MessageModel) SetEmbedding ¶
func (m *MessageModel) SetEmbedding(vec []float32) error
SetEmbedding 设置嵌入向量
func (*MessageModel) ToSchemaMessage ¶
func (m *MessageModel) ToSchemaMessage() *schema.Message
ToSchemaMessage 转换为 schema.Message
type RecallMode ¶
type RecallMode int
RecallMode 召回策略模式
const ( RecallModeAuto RecallMode = iota // 自动:优先向量,失败回退混合(默认行为) RecallModeVector // 仅向量语义搜索 RecallModeHybrid // 仅关键词 + 时间衰减 RecallModeCombined // 向量 + 关键词 + 时间组合权重 )