File size: 1,729 Bytes
45fe8b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity


class SemanticCache:
    def __init__(self, similarity_threshold=0.70):
        self.cache = []
        self.similarity_threshold = similarity_threshold
        self.hit_count = 0
        self.miss_count = 0

    def search(self, query_embedding):
        """
        Check if a similar query exists in cache
        """
        if len(self.cache) == 0:
            self.miss_count += 1
            return None

        embeddings = [item["embedding"] for item in self.cache]

        similarities = cosine_similarity(query_embedding, embeddings)[0]

        best_idx = np.argmax(similarities)
        best_score = similarities[best_idx]

        if best_score >= self.similarity_threshold:
            self.hit_count += 1
            return {
                "result": self.cache[best_idx]["result"],
                "matched_query": self.cache[best_idx]["query"],
                "similarity_score": float(best_score)
            }

        self.miss_count += 1
        return None

    def add(self, query, embedding, result):
        """
        Store query and result in cache
        """
        self.cache.append({
            "query": query,
            "embedding": embedding[0],
            "result": result
        })

    def stats(self):
        total = self.hit_count + self.miss_count
        hit_rate = self.hit_count / total if total > 0 else 0

        return {
            "total_entries": len(self.cache),
            "hit_count": self.hit_count,
            "miss_count": self.miss_count,
            "hit_rate": hit_rate
        }

    def clear(self):
        self.cache = []
        self.hit_count = 0
        self.miss_count = 0