| | | 1 | | using Intentum.AI.Embeddings; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.Mock; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Deterministic mock for offline and CI usage. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class MockEmbeddingProvider : IIntentEmbeddingProvider |
| | | 9 | | { |
| | | 10 | | public IntentEmbedding Embed(string behaviorKey) |
| | 246 | 11 | | { |
| | 246 | 12 | | var hash = behaviorKey.GetHashCode(); |
| | 246 | 13 | | var normalized = Math.Abs(hash % 100) / 100.0; |
| | | 14 | | |
| | | 15 | | // Generate a simple deterministic vector for cosine similarity support |
| | 246 | 16 | | var vector = GenerateDeterministicVector(behaviorKey, dimension: 8); |
| | | 17 | | |
| | 246 | 18 | | return new IntentEmbedding( |
| | 246 | 19 | | Source: behaviorKey, |
| | 246 | 20 | | Score: normalized, |
| | 246 | 21 | | Vector: vector |
| | 246 | 22 | | ); |
| | 246 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Generates a deterministic vector from a behavior key for testing purposes. |
| | | 27 | | /// Uses a deterministic hash expansion (no PRNG) so the same key always yields the same vector. |
| | | 28 | | /// </summary> |
| | | 29 | | private static double[] GenerateDeterministicVector(string behaviorKey, int dimension) |
| | 246 | 30 | | { |
| | 246 | 31 | | var vector = new double[dimension]; |
| | 4428 | 32 | | for (int i = 0; i < dimension; i++) |
| | 1968 | 33 | | { |
| | 1968 | 34 | | var h = HashForKeyAndIndex(behaviorKey, i); |
| | 1968 | 35 | | vector[i] = (h / (double)int.MaxValue) * 2 - 1; // Range: [-1, 1] |
| | 1968 | 36 | | } |
| | | 37 | | |
| | 2214 | 38 | | var magnitude = Math.Sqrt(vector.Sum(x => x * x)); |
| | 246 | 39 | | if (magnitude > 0) |
| | 246 | 40 | | { |
| | 4428 | 41 | | for (int i = 0; i < dimension; i++) |
| | 1968 | 42 | | vector[i] /= magnitude; |
| | 246 | 43 | | } |
| | 246 | 44 | | return vector; |
| | 246 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static int HashForKeyAndIndex(string key, int index) |
| | 1968 | 48 | | { |
| | | 49 | | unchecked |
| | 1968 | 50 | | { |
| | 1968 | 51 | | var h = 17; |
| | 62560 | 52 | | foreach (var c in key) |
| | 28328 | 53 | | h = h * 31 + c; |
| | 1968 | 54 | | h = h * 31 + index; |
| | 1968 | 55 | | return Math.Abs(h); |
| | | 56 | | } |
| | 1968 | 57 | | } |
| | | 58 | | } |