Documentation
¶
Overview ¶
Package cache 提供 RAG 语义缓存功能
通过缓存相似查询的结果,避免重复检索和生成,提升性能。 与传统精确字符串匹配不同,语义缓存基于查询的语义相似度进行匹配:
- 使用向量嵌入表示查询语义
- 通过余弦相似度判断查询是否命中缓存
- 支持 TTL 过期、LRU 淘汰和容量限制
使用示例:
sc := cache.New(embedder,
cache.WithMaxSize(500),
cache.WithTTL(30 * time.Minute),
cache.WithThreshold(0.9),
)
// 查找缓存
result, hit, err := sc.Get(ctx, "什么是 Go 语言?")
if !hit {
// 缓存未命中,执行检索后写入缓存
sc.Put(ctx, "什么是 Go 语言?", result)
}
Index ¶
- type CacheEntry
- type CacheResult
- type CacheStats
- type CachedDocument
- type Embedder
- type Option
- type SemanticCache
- func (c *SemanticCache) Clear()
- func (c *SemanticCache) Get(ctx context.Context, query string) (*CacheResult, bool, error)
- func (c *SemanticCache) Invalidate(query string)
- func (c *SemanticCache) Put(ctx context.Context, query string, result *CacheResult) error
- func (c *SemanticCache) Size() int
- func (c *SemanticCache) Stats() CacheStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheEntry ¶
type CacheEntry struct {
// Query 原始查询文本
Query string
// Embedding 查询的向量表示
Embedding []float64
// Result 缓存的检索结果
Result *CacheResult
// CreatedAt 创建时间
CreatedAt time.Time
// HitCount 命中次数(用于统计和 LRU 淘汰参考)
HitCount int64
}
CacheEntry 缓存条目 存储查询的向量表示和对应的检索结果
type CacheResult ¶
type CacheResult struct {
// Documents 缓存的文档列表
Documents []CachedDocument
// Answer 缓存的生成答案
Answer string
// Metadata 附加元数据
Metadata map[string]any
}
CacheResult 缓存结果 包含检索到的文档和生成的答案
type CacheStats ¶
type CacheStats struct {
// Size 当前缓存条目数
Size int
// MaxSize 最大缓存容量
MaxSize int
// Hits 命中次数
Hits int64
// Misses 未命中次数
Misses int64
// HitRate 命中率 (0-1)
HitRate float64
// Evictions 淘汰次数
Evictions int64
}
CacheStats 缓存统计信息
type CachedDocument ¶
type CachedDocument struct {
// Content 文档内容
Content string
// Score 相关性分数
Score float64
// Metadata 文档元数据
Metadata map[string]any
}
CachedDocument 缓存文档
type Embedder ¶
type Embedder interface {
// Embed 将文本转换为向量
Embed(ctx context.Context, text string) ([]float64, error)
}
Embedder 嵌入器接口(简化版,不依赖 ai-core) 将文本转换为向量表示,用于语义相似度计算
type Option ¶
type Option func(*SemanticCache)
Option 语义缓存配置选项
func WithMaxSize ¶
WithMaxSize 设置最大缓存条目数 当缓存满时,最早创建的条目会被淘汰 默认值: 1000
func WithThreshold ¶
WithThreshold 设置相似度阈值 只有余弦相似度 >= threshold 的缓存才会被视为命中 值范围: 0.0 ~ 1.0,越高越严格 默认值: 0.85
type SemanticCache ¶
type SemanticCache struct {
// contains filtered or unexported fields
}
SemanticCache 语义缓存 基于查询语义相似度进行缓存匹配,而非精确字符串匹配。 通过向量嵌入计算余弦相似度,当相似度超过阈值时视为命中。
线程安全:所有方法都是并发安全的
func New ¶
func New(embedder Embedder, opts ...Option) *SemanticCache
New 创建语义缓存 embedder 用于将查询文本转换为向量表示
func (*SemanticCache) Get ¶
func (c *SemanticCache) Get(ctx context.Context, query string) (*CacheResult, bool, error)
Get 查找缓存(语义匹配) 将查询文本向量化后,与所有缓存条目计算余弦相似度, 返回相似度最高且超过阈值的结果。
返回值:
- result: 缓存结果,未命中时为 nil
- hit: 是否命中
- err: 嵌入计算错误
func (*SemanticCache) Invalidate ¶
func (c *SemanticCache) Invalidate(query string)
Invalidate 使特定缓存失效 通过精确匹配查询文本来删除对应的缓存条目
func (*SemanticCache) Put ¶
func (c *SemanticCache) Put(ctx context.Context, query string, result *CacheResult) error
Put 写入缓存 将查询和结果写入缓存。如果缓存已满,淘汰最早创建的条目。