-
Notifications
You must be signed in to change notification settings - Fork 249
Allow semantic cache similarity threshold to be set at the category level #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a6f5607
7912729
9bff917
11324dd
1b7e9ae
ae39fe2
06a32e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -487,18 +487,23 @@ func (c *MilvusCache) addEntry(id string, requestID string, model string, query | |
|
|
||
| // FindSimilar searches for semantically similar cached requests | ||
| func (c *MilvusCache) FindSimilar(model string, query string) ([]byte, bool, error) { | ||
| return c.FindSimilarWithThreshold(model, query, c.similarityThreshold) | ||
| } | ||
|
|
||
| // FindSimilarWithThreshold searches for semantically similar cached requests using a specific threshold | ||
| func (c *MilvusCache) FindSimilarWithThreshold(model string, query string, threshold float32) ([]byte, bool, error) { | ||
|
Comment on lines
+493
to
+494
|
||
| start := time.Now() | ||
|
|
||
| if !c.enabled { | ||
| observability.Debugf("MilvusCache.FindSimilar: cache disabled") | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: cache disabled") | ||
| return nil, false, nil | ||
| } | ||
| queryPreview := query | ||
| if len(query) > 50 { | ||
| queryPreview = query[:50] + "..." | ||
| } | ||
| observability.Debugf("MilvusCache.FindSimilar: searching for model='%s', query='%s' (len=%d chars)", | ||
| model, queryPreview, len(query)) | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: searching for model='%s', query='%s' (len=%d chars), threshold=%.4f", | ||
| model, queryPreview, len(query), threshold) | ||
|
|
||
| // Generate semantic embedding for similarity comparison | ||
| queryEmbedding, err := candle_binding.GetEmbedding(query, 0) // Auto-detect dimension | ||
|
|
@@ -529,7 +534,7 @@ func (c *MilvusCache) FindSimilar(model string, query string) ([]byte, bool, err | |
| searchParam, | ||
| ) | ||
| if err != nil { | ||
| observability.Debugf("MilvusCache.FindSimilar: search failed: %v", err) | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: search failed: %v", err) | ||
| atomic.AddInt64(&c.missCount, 1) | ||
| metrics.RecordCacheOperation("milvus", "find_similar", "error", time.Since(start).Seconds()) | ||
| metrics.RecordCacheMiss() | ||
|
|
@@ -538,21 +543,21 @@ func (c *MilvusCache) FindSimilar(model string, query string) ([]byte, bool, err | |
|
|
||
| if len(searchResult) == 0 || searchResult[0].ResultCount == 0 { | ||
| atomic.AddInt64(&c.missCount, 1) | ||
| observability.Debugf("MilvusCache.FindSimilar: no entries found") | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: no entries found") | ||
| metrics.RecordCacheOperation("milvus", "find_similar", "miss", time.Since(start).Seconds()) | ||
| metrics.RecordCacheMiss() | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| bestScore := searchResult[0].Scores[0] | ||
| if bestScore < c.similarityThreshold { | ||
| if bestScore < threshold { | ||
| atomic.AddInt64(&c.missCount, 1) | ||
| observability.Debugf("MilvusCache.FindSimilar: CACHE MISS - best_similarity=%.4f < threshold=%.4f", | ||
| bestScore, c.similarityThreshold) | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: CACHE MISS - best_similarity=%.4f < threshold=%.4f", | ||
| bestScore, threshold) | ||
| observability.LogEvent("cache_miss", map[string]interface{}{ | ||
| "backend": "milvus", | ||
| "best_similarity": bestScore, | ||
| "threshold": c.similarityThreshold, | ||
| "threshold": threshold, | ||
| "model": model, | ||
| "collection": c.collectionName, | ||
| }) | ||
|
|
@@ -569,20 +574,20 @@ func (c *MilvusCache) FindSimilar(model string, query string) ([]byte, bool, err | |
| } | ||
|
|
||
| if responseBody == nil { | ||
| observability.Debugf("MilvusCache.FindSimilar: cache hit but response_body is missing or not a string") | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: cache hit but response_body is missing or not a string") | ||
| atomic.AddInt64(&c.missCount, 1) | ||
| metrics.RecordCacheOperation("milvus", "find_similar", "error", time.Since(start).Seconds()) | ||
| metrics.RecordCacheMiss() | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| atomic.AddInt64(&c.hitCount, 1) | ||
| observability.Debugf("MilvusCache.FindSimilar: CACHE HIT - similarity=%.4f >= threshold=%.4f, response_size=%d bytes", | ||
| bestScore, c.similarityThreshold, len(responseBody)) | ||
| observability.Debugf("MilvusCache.FindSimilarWithThreshold: CACHE HIT - similarity=%.4f >= threshold=%.4f, response_size=%d bytes", | ||
| bestScore, threshold, len(responseBody)) | ||
| observability.LogEvent("cache_hit", map[string]interface{}{ | ||
| "backend": "milvus", | ||
| "similarity": bestScore, | ||
| "threshold": c.similarityThreshold, | ||
| "threshold": threshold, | ||
| "model": model, | ||
| "collection": c.collectionName, | ||
| }) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
FindSimilarWithThresholdmethod duplicates significant logic from the existingFindSimilarmethod. Consider refactoringFindSimilarto callFindSimilarWithThresholdwith the default threshold to eliminate code duplication and ensure consistent behavior between both methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please address this and other duplicate functions