Skip to content

[AI优化] 语义缓存层实现 - 基于 Embedding 相似度匹配 #91

@hrygo

Description

@hrygo

问题描述

当前 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 - 集成多级缓存查询

验收标准

  • 单元测试覆盖率 >80%
  • 基准测试显示命中率提升 >20%
  • 查询延迟增加 <50ms (embedding 计算)
  • 内存使用可控 (maxSize 限制)
  • 配置化的 threshold 和 ttl

预估工时

3-5 天

相关文档

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions