问题描述
当前 AmazingParrot 使用简单的 LRU 缓存,基于完整输入的 SHA256 hash 进行精确匹配。这导致:
- 语义相似但措辞不同的查询无法命中缓存
- 例如 "明天有什么安排" vs "明天的日程是什么" 被视为不同查询
- 缓存命中率预估仅 ~30%
- 每次 cache miss 需要完整的 LLM 调用流程
相关代码: ai/agent/amazing_parrot.go:ExecuteWithCallback()
建议方案
实现基于 embedding 相似度的语义缓存层(L2 Cache):
type SemanticCache struct {
vectors [][]float32 // 已缓存查询的向量
responses []string // 对应的响应
timestamps []time.Time // 缓存时间
threshold float32 // 相似度阈值 (建议 0.95)
maxSize int // 最大缓存条目 (建议 1000)
ttl time.Duration // TTL (建议 30分钟)
embedding embedding.Service // 向量化服务
mu sync.RWMutex
}
func (c *SemanticCache) Get(query string) (string, bool) {
vec, err := c.embedding.Encode(query)
if err != nil {
return "", false
}
c.mu.RLock()
defer c.mu.RUnlock()
for i, cached := range c.vectors {
if time.Since(c.timestamps[i]) > c.ttl {
continue // 过期
}
if cosineSimilarity(vec, cached) >= c.threshold {
return c.responses[i], true
}
}
return "", false
}
多级缓存架构
┌─────────────────────────────────────────┐
│ L1: 精确匹配缓存 (现有) │
│ Key: SHA256(userInput) │
│ TTL: 5 分钟 │
├─────────────────────────────────────────┤
│ L2: 语义缓存 (本 Issue) │
│ Key: embedding(userInput) │
│ Similarity: >= 0.95 │
│ TTL: 30 分钟 │
└─────────────────────────────────────────┘
文件变更
ai/agent/cache.go - 新增 SemanticCache 实现
ai/agent/cache_test.go - 单元测试
ai/agent/amazing_parrot.go - 集成多级缓存查询
验收标准
预估工时
3-5 天
相关文档
问题描述
当前 AmazingParrot 使用简单的 LRU 缓存,基于完整输入的 SHA256 hash 进行精确匹配。这导致:
相关代码:
ai/agent/amazing_parrot.go:ExecuteWithCallback()建议方案
实现基于 embedding 相似度的语义缓存层(L2 Cache):
多级缓存架构
文件变更
ai/agent/cache.go- 新增 SemanticCache 实现ai/agent/cache_test.go- 单元测试ai/agent/amazing_parrot.go- 集成多级缓存查询验收标准
预估工时
3-5 天
相关文档