| | | 1 | | using Intentum.AI.Embeddings; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.Similarity; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Similarity engine that uses cosine similarity between embedding vectors. |
| | | 7 | | /// Falls back to simple average if vectors are not available. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class CosineSimilarityEngine : IIntentSimilarityEngine |
| | | 10 | | { |
| | | 11 | | public double CalculateIntentScore(IReadOnlyCollection<IntentEmbedding> embeddings) |
| | 7 | 12 | | { |
| | 7 | 13 | | if (embeddings.Count == 0) |
| | 1 | 14 | | return 0; |
| | | 15 | | |
| | | 16 | | // Check if all embeddings have vectors |
| | 16 | 17 | | var allHaveVectors = embeddings.All(e => e.Vector is { Count: > 0 }); |
| | | 18 | | |
| | 6 | 19 | | if (!allHaveVectors) |
| | 2 | 20 | | { |
| | | 21 | | // Fallback to simple average if vectors are not available |
| | 6 | 22 | | return embeddings.Average(e => e.Score); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | // Calculate cosine similarity between all pairs and average |
| | 4 | 26 | | var embeddingList = embeddings.ToList(); |
| | 4 | 27 | | var similarities = new List<double>(); |
| | | 28 | | |
| | 24 | 29 | | for (int i = 0; i < embeddingList.Count; i++) |
| | 8 | 30 | | { |
| | 26 | 31 | | for (int j = i + 1; j < embeddingList.Count; j++) |
| | 5 | 32 | | { |
| | 5 | 33 | | var similarity = CalculateCosineSimilarity( |
| | 5 | 34 | | embeddingList[i].Vector!, |
| | 5 | 35 | | embeddingList[j].Vector!); |
| | 5 | 36 | | similarities.Add(similarity); |
| | 5 | 37 | | } |
| | 8 | 38 | | } |
| | | 39 | | |
| | | 40 | | // If only one embedding, return its score |
| | 4 | 41 | | if (similarities.Count == 0) |
| | 1 | 42 | | return embeddings.First().Score; |
| | | 43 | | |
| | | 44 | | // Average of all pairwise cosine similarities |
| | 3 | 45 | | return similarities.Average(); |
| | 7 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Calculates cosine similarity between two vectors. |
| | | 50 | | /// Returns a value between 0 and 1, where 1 means identical, 0.5 means orthogonal, 0 means opposite. |
| | | 51 | | /// </summary> |
| | | 52 | | private static double CalculateCosineSimilarity( |
| | | 53 | | IReadOnlyList<double> vector1, |
| | | 54 | | IReadOnlyList<double> vector2) |
| | 5 | 55 | | { |
| | 5 | 56 | | if (vector1.Count != vector2.Count) |
| | 0 | 57 | | throw new ArgumentException("Vectors must have the same length"); |
| | | 58 | | |
| | 5 | 59 | | return CosineSimilarityHelper.CosineSimilarityNormalized(vector1.ToArray(), vector2.ToArray()); |
| | 5 | 60 | | } |
| | | 61 | | } |