gorm

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitText

func SplitText(text string, chunkSize, chunkOverlap int) []string

SplitText 将文本按字符数分块,优先在自然边界(句号、换行等)处断开

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 存储配置

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig 默认配置

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

type EmbeddingFunc func(ctx context.Context, text string) ([]float32, error)

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 (d *GORMDocStore) GetDocuments(ctx context.Context, collection string) ([]*schema.Document, error)

func (*GORMDocStore) RecallDocuments

func (d *GORMDocStore) RecallDocuments(ctx context.Context, collection string, query string, topK int) ([]*schema.Document, error)

func (*GORMDocStore) SaveDocuments

func (d *GORMDocStore) SaveDocuments(ctx context.Context, collection string, docs []*schema.Document) error

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

func (s *GORMStore) ClearSession(ctx context.Context, sessionID string) error

ClearSession 清空会话(硬删除)

func (*GORMStore) Close

func (s *GORMStore) Close() error

Close 关闭存储

func (*GORMStore) GetDB

func (s *GORMStore) GetDB() *gorm.DB

GetDB 返回底层数据库连接(供 Retriever 使用)

func (*GORMStore) GetSession

func (s *GORMStore) GetSession(ctx context.Context, sessionID string) ([]*schema.Message, error)

GetSession 获取完整会话历史

func (*GORMStore) GetSessionStats

func (s *GORMStore) GetSessionStats(ctx context.Context, sessionID string) (map[string]any, error)

GetSessionStats 获取会话统计

func (*GORMStore) GetSessionWithReasoning

func (s *GORMStore) GetSessionWithReasoning(ctx context.Context, sessionID string) ([]*schema.Message, error)

GetSessionWithReasoning 获取完整会话历史(含推理内容) 当前实现与 GetSession 相同,因为 MessageModel.ToSchemaMessage 已包含 ReasoningContent

func (*GORMStore) Save

func (s *GORMStore) Save(ctx context.Context, sessionID string, msgs []*schema.Message) error

Save 保存消息(支持批量 + 嵌入生成) 每条消息使用递增时间戳,确保同一轮对话内的顺序正确 嵌入生成在事务外执行,避免网络 I/O 阻塞 SQLite 写锁

func (*GORMStore) SearchByRole

func (s *GORMStore) SearchByRole(ctx context.Context, sessionID string, role schema.RoleType, limit int) ([]*schema.Message, error)

SearchByRole 按角色搜索

func (*GORMStore) SearchByTimeRange

func (s *GORMStore) SearchByTimeRange(ctx context.Context, sessionID string, start, end time.Time) ([]*schema.Message, error)

SearchByTimeRange 按时间范围搜索

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) Close

func (r *HNSWRetriever) Close() error

Close 关闭检索器

func (*HNSWRetriever) IndexReady

func (r *HNSWRetriever) IndexReady() bool

IndexReady 返回向量索引是否就绪

func (*HNSWRetriever) RebuildIndex

func (r *HNSWRetriever) RebuildIndex()

RebuildIndex 从存储中重建完整索引

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) TableName

func (m *MessageModel) TableName() string

TableName 指定表名

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                   // 向量 + 关键词 + 时间组合权重
)

Jump to

Keyboard shortcuts

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