aimem

package
v1.4.4-alpha1202-diff-... Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AIMemoryHNSWBackend

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

AIMemoryHNSWBackend 管理AIMemory的HNSW索引

func NewAIMemoryHNSWBackend

func NewAIMemoryHNSWBackend(options ...HNSWOption) (*AIMemoryHNSWBackend, error)

NewAIMemoryHNSWBackend 创建或加载HNSW后端

func (*AIMemoryHNSWBackend) Add

Add 添加记忆实体到HNSW索引

func (*AIMemoryHNSWBackend) Close

func (b *AIMemoryHNSWBackend) Close() error

Close 关闭后端,保存索引

func (*AIMemoryHNSWBackend) Delete

func (b *AIMemoryHNSWBackend) Delete(memoryID string) error

Delete 从HNSW索引中删除记忆实体

func (*AIMemoryHNSWBackend) GetStats

func (b *AIMemoryHNSWBackend) GetStats() map[string]interface{}

GetStats 获取HNSW索引统计信息

func (*AIMemoryHNSWBackend) RebuildIndex

func (b *AIMemoryHNSWBackend) RebuildIndex() error

RebuildIndex 重建HNSW索引(从数据库中的所有记忆实体)

func (*AIMemoryHNSWBackend) SaveGraph

func (b *AIMemoryHNSWBackend) SaveGraph() error

SaveGraph 保存HNSW Graph到数据库

func (*AIMemoryHNSWBackend) Search

func (b *AIMemoryHNSWBackend) Search(queryVector []float32, limit int) ([]SearchResultWithDistance, error)

Search 使用HNSW索引搜索相似的记忆实体

func (*AIMemoryHNSWBackend) Update

func (b *AIMemoryHNSWBackend) Update(entity *aicommon.MemoryEntity) error

Update 更新HNSW索引中的记忆实体

type AIMemoryTriage

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

func NewAIMemory

func NewAIMemory(sessionId string, opts ...Option) (*AIMemoryTriage, error)

NewAIMemory 创建AI记忆管理系统

func (*AIMemoryTriage) AddRawText

func (r *AIMemoryTriage) AddRawText(i string) ([]*aicommon.MemoryEntity, error)

AddRawText 从原始文本生成记忆条目

func (*AIMemoryTriage) BatchIsRepeatedMemoryEntities

func (t *AIMemoryTriage) BatchIsRepeatedMemoryEntities(entities []*aicommon.MemoryEntity, config *DeduplicationConfig) ([]int, error)

BatchIsRepeatedMemoryEntities 批量无AI版本的重复检查

func (*AIMemoryTriage) BatchIsRepeatedMemoryEntitiesByAI

func (t *AIMemoryTriage) BatchIsRepeatedMemoryEntitiesByAI(ctx context.Context, entities []*aicommon.MemoryEntity, candidateIndices []int, config *DeduplicationConfig) ([]int, error)

BatchIsRepeatedMemoryEntitiesByAI AI版本的批量重复检查

func (*AIMemoryTriage) Close

func (r *AIMemoryTriage) Close() error

Close 关闭资源

func (*AIMemoryTriage) DeleteMemoryEntity

func (r *AIMemoryTriage) DeleteMemoryEntity(memoryID string) error

DeleteMemoryEntity 删除记忆条目

func (*AIMemoryTriage) GetAllTags

func (r *AIMemoryTriage) GetAllTags() ([]string, error)

GetAllTags 获取当前会话的所有标签

func (*AIMemoryTriage) GetDB

func (r *AIMemoryTriage) GetDB() *gorm.DB

func (*AIMemoryTriage) GetDynamicContextWithTags

func (r *AIMemoryTriage) GetDynamicContextWithTags() (string, error)

GetDynamicContextWithTags 获取包含已有标签的动态上下文

func (*AIMemoryTriage) GetEmitter

func (t *AIMemoryTriage) GetEmitter() *aicommon.Emitter

func (*AIMemoryTriage) GetHNSWStats

func (r *AIMemoryTriage) GetHNSWStats() map[string]interface{}

GetHNSWStats 获取HNSW索引统计信息

func (*AIMemoryTriage) GetMemoryEntity

func (r *AIMemoryTriage) GetMemoryEntity(memoryID string) (*aicommon.MemoryEntity, error)

GetMemoryEntity 获取单个记忆条目

func (*AIMemoryTriage) GetSessionID

func (r *AIMemoryTriage) GetSessionID() string

GetSessionID 获取当前会话ID

func (*AIMemoryTriage) HandleMemory

func (t *AIMemoryTriage) HandleMemory(i any) error

HandleMemory 处理输入内容,自动构造记忆并去重保存

func (*AIMemoryTriage) ListAllMemories

func (r *AIMemoryTriage) ListAllMemories(limit int) ([]*aicommon.MemoryEntity, error)

ListAllMemories 列出所有记忆条目

func (*AIMemoryTriage) RebuildHNSWIndex

func (r *AIMemoryTriage) RebuildHNSWIndex() error

RebuildHNSWIndex 重建HNSW索引

func (*AIMemoryTriage) SafeGetDB

func (r *AIMemoryTriage) SafeGetDB() *gorm.DB

func (*AIMemoryTriage) SaveMemoryEntities

func (r *AIMemoryTriage) SaveMemoryEntities(entities ...*aicommon.MemoryEntity) error

SaveMemoryEntities 保存记忆条目到数据库并索引到RAG系统和HNSW

func (*AIMemoryTriage) SearchByScoreVector

func (r *AIMemoryTriage) SearchByScoreVector(targetScores *aicommon.MemoryEntity, limit int) ([]*aicommon.SearchResult, error)

SearchByScoreVector 通过分数向量搜索相似的记忆(基于HNSW)

func (*AIMemoryTriage) SearchByScores

func (r *AIMemoryTriage) SearchByScores(filter *aicommon.ScoreFilter, limit int) ([]*aicommon.MemoryEntity, error)

SearchByScores 按照C.O.R.E. P.A.C.T.评分搜索

func (*AIMemoryTriage) SearchBySemantics

func (r *AIMemoryTriage) SearchBySemantics(query string, limit int) ([]*aicommon.SearchResult, error)

SearchBySemantics 通过语义搜索记忆

func (*AIMemoryTriage) SearchByTags

func (r *AIMemoryTriage) SearchByTags(tags []string, matchAll bool, limit int) ([]*aicommon.MemoryEntity, error)

SearchByTags 按照标签搜索

func (*AIMemoryTriage) SearchMemory

func (t *AIMemoryTriage) SearchMemory(origin any, bytesLimit int) (*aicommon.SearchMemoryResult, error)

func (*AIMemoryTriage) SearchMemoryWithoutAI

func (t *AIMemoryTriage) SearchMemoryWithoutAI(origin any, bytesLimit int) (*aicommon.SearchMemoryResult, error)

func (*AIMemoryTriage) SelectTags

func (r *AIMemoryTriage) SelectTags(ctx context.Context, i any) ([]string, error)

TriageTagsFromText 从文本中生成标签以便搜索

func (*AIMemoryTriage) SetInvoker

func (a *AIMemoryTriage) SetInvoker(invoker aicommon.AIInvokeRuntime)

func (*AIMemoryTriage) ShouldSaveMemoryEntities

func (t *AIMemoryTriage) ShouldSaveMemoryEntities(entities []*aicommon.MemoryEntity) []*aicommon.MemoryEntity

ShouldSaveMemoryEntities 判断哪些记忆实体值得保存(去重后)

func (*AIMemoryTriage) UpdateMemoryEntity

func (r *AIMemoryTriage) UpdateMemoryEntity(entity *aicommon.MemoryEntity) error

UpdateMemoryEntity 更新记忆条目

type Config

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

Config AIMemoryTriage的配置

type DeduplicationConfig

type DeduplicationConfig struct {
	TagOverlapThreshold         float64 // 标签交集比例阈值,默认0.8
	QuestionSimilarityThreshold float64 // 问题相似度阈值,默认0.85
	ContentSimilarityThreshold  float64 // 内容相似度阈值,默认0.9
}

DeduplicationConfig 去重配置

func DefaultDeduplicationConfig

func DefaultDeduplicationConfig() *DeduplicationConfig

DefaultDeduplicationConfig 默认去重配置

type HNSWBackendConfig

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

func NewHNSWBackendConfig

func NewHNSWBackendConfig(opts ...HNSWOption) (*HNSWBackendConfig, error)

type HNSWOption

type HNSWOption func(*HNSWBackendConfig)

func WithHNSWAutoSave

func WithHNSWAutoSave(autoSave bool) HNSWOption

func WithHNSWDatabase

func WithHNSWDatabase(db *gorm.DB) HNSWOption

func WithHNSWSessionID

func WithHNSWSessionID(sessionID string) HNSWOption

type KeywordMatcher

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

MatchKeywords 进行关键词匹配,支持多种匹配策略

func NewKeywordMatcher

func NewKeywordMatcher() *KeywordMatcher

NewKeywordMatcher 创建关键词匹配器

func (*KeywordMatcher) ContainsKeyword

func (km *KeywordMatcher) ContainsKeyword(query string, content string) bool

ContainsKeyword 检查内容是否包含查询中的关键词(至少一个) 支持部分匹配和中英混合

func (*KeywordMatcher) ExpandKeywords

func (km *KeywordMatcher) ExpandKeywords(keywords []string) []string

ExpandKeywords 关键词扩展 - 使用同义词进行扩展

func (*KeywordMatcher) MatchAllKeywords

func (km *KeywordMatcher) MatchAllKeywords(query string, content string) bool

MatchAllKeywords 检查内容是否包含查询中的所有关键词

func (*KeywordMatcher) MatchScore

func (km *KeywordMatcher) MatchScore(query string, content string) float64

MatchScore 计算两个文本的关键词匹配分数 (0.0-1.0)

type KeywordNormalizer

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

KeywordNormalizer 关键词规范化器 - 支持中英文混合处理

func NewKeywordNormalizer

func NewKeywordNormalizer() *KeywordNormalizer

NewKeywordNormalizer 创建新的关键词规范化器

func (*KeywordNormalizer) ExtractKeywords

func (kn *KeywordNormalizer) ExtractKeywords(text string) []string

ExtractKeywords 从文本中提取关键词(中英文混合)

func (*KeywordNormalizer) NormalizeKeyword

func (kn *KeywordNormalizer) NormalizeKeyword(keyword string) string

NormalizeKeyword 规范化单个关键词 处理:转换大小写、移除特殊字符、处理同义词

type MockMemoryTriage

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

MockMemoryTriage 这是一个简单的mock,用于不需要使用triage的测试

func NewMockMemoryTriage

func NewMockMemoryTriage() *MockMemoryTriage

func (*MockMemoryTriage) AddRawText

func (m *MockMemoryTriage) AddRawText(text string) ([]*aicommon.MemoryEntity, error)

func (*MockMemoryTriage) Close

func (m *MockMemoryTriage) Close() error

func (*MockMemoryTriage) GetSessionID

func (m *MockMemoryTriage) GetSessionID() string

func (*MockMemoryTriage) HandleMemory

func (m *MockMemoryTriage) HandleMemory(i any) error

func (*MockMemoryTriage) SaveMemoryEntities

func (m *MockMemoryTriage) SaveMemoryEntities(entities ...*aicommon.MemoryEntity) error

func (*MockMemoryTriage) SearchBySemantics

func (m *MockMemoryTriage) SearchBySemantics(query string, limit int) ([]*aicommon.SearchResult, error)

func (*MockMemoryTriage) SearchByTags

func (m *MockMemoryTriage) SearchByTags(tags []string, matchAll bool, limit int) ([]*aicommon.MemoryEntity, error)

func (*MockMemoryTriage) SearchMemory

func (m *MockMemoryTriage) SearchMemory(origin any, bytesLimit int) (*aicommon.SearchMemoryResult, error)

func (*MockMemoryTriage) SearchMemoryWithoutAI

func (m *MockMemoryTriage) SearchMemoryWithoutAI(origin any, bytesLimit int) (*aicommon.SearchMemoryResult, error)

func (*MockMemoryTriage) SetInvoker

func (m *MockMemoryTriage) SetInvoker(invoker aicommon.AIInvokeRuntime)

func (*MockMemoryTriage) SetOverSearch

func (m *MockMemoryTriage) SetOverSearch(over bool)

type Option

type Option func(config *Config)

Option AIMemoryTriage的配置选项

func WithContextProvider

func WithContextProvider(i func() (string, error)) Option

WithContextProvider 设置上下文提供者

func WithDatabase

func WithDatabase(db *gorm.DB) Option

WithDatabase 设置GORM数据库连接

func WithInvoker

func WithInvoker(invoker aicommon.AIInvokeRuntime) Option

WithInvoker 设置AI调用运行时

func WithRAGOptions

func WithRAGOptions(opts ...rag.RAGSystemConfigOption) Option

WithRAGOptions 设置RAG选项(主要用于测试时注入mock embedding)

type SearchResultWithDistance

type SearchResultWithDistance struct {
	Entity   *aicommon.MemoryEntity
	Distance float64
	Score    float64
}

SearchResultWithDistance 搜索结果(包含距离)

Jump to

Keyboard shortcuts

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