@@ -143,14 +143,17 @@ func (db *ToolsDatabase) FindSimilarTools(query string, topK int) ([]openai.Chat
143
143
dotProduct += queryEmbedding [i ] * entry .Embedding [i ]
144
144
}
145
145
146
- results = append (results , SimilarityResult {
147
- Entry : entry ,
148
- Similarity : dotProduct ,
149
- })
150
-
151
146
// Debug logging to see similarity scores
152
147
observability .Debugf ("Tool '%s' similarity score: %.4f (threshold: %.4f)" ,
153
148
entry .Tool .Function .Name , dotProduct , db .similarityThreshold )
149
+
150
+ // Only consider if above threshold
151
+ if dotProduct >= db .similarityThreshold {
152
+ results = append (results , SimilarityResult {
153
+ Entry : entry ,
154
+ Similarity : dotProduct ,
155
+ })
156
+ }
154
157
}
155
158
156
159
// No results found
@@ -164,13 +167,12 @@ func (db *ToolsDatabase) FindSimilarTools(query string, topK int) ([]openai.Chat
164
167
})
165
168
166
169
// Select top-k tools that meet the threshold
167
- var selectedTools []openai.ChatCompletionToolParam
168
- for i := 0 ; i < len (results ) && i < topK ; i ++ {
169
- if results [i ].Similarity >= db .similarityThreshold {
170
- selectedTools = append (selectedTools , results [i ].Entry .Tool )
171
- observability .Infof ("Selected tool: %s (similarity=%.4f)" ,
172
- results [i ].Entry .Tool .Function .Name , results [i ].Similarity )
173
- }
170
+ limit := min (topK , len (results ))
171
+ selectedTools := make ([]openai.ChatCompletionToolParam , 0 , limit )
172
+ for i := range limit {
173
+ selectedTools = append (selectedTools , results [i ].Entry .Tool )
174
+ observability .Infof ("Selected tool: %s (similarity=%.4f)" ,
175
+ results [i ].Entry .Tool .Function .Name , results [i ].Similarity )
174
176
}
175
177
176
178
observability .Infof ("Found %d similar tools for query: %s" , len (selectedTools ), query )
0 commit comments