|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +using System.Security.Cryptography; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace KernelMemory.Core.Embeddings.Cache; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Cache key for embeddings. Uniquely identifies an embedding by provider, model, |
| 9 | +/// dimensions, normalization state, and content hash. |
| 10 | +/// The input text is NOT stored - only a SHA256 hash is used for security. |
| 11 | +/// </summary> |
| 12 | +public sealed class EmbeddingCacheKey |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Provider type name (e.g., "OpenAI", "Ollama", "AzureOpenAI", "HuggingFace"). |
| 16 | + /// </summary> |
| 17 | + public required string Provider { get; init; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Model name (e.g., "text-embedding-ada-002", "qwen3-embedding"). |
| 21 | + /// </summary> |
| 22 | + public required string Model { get; init; } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Vector dimensions produced by this model. |
| 26 | + /// </summary> |
| 27 | + public required int VectorDimensions { get; init; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Whether the vectors are normalized. |
| 31 | + /// </summary> |
| 32 | + public required bool IsNormalized { get; init; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Length of the original text in characters. |
| 36 | + /// Used as an additional collision prevention measure. |
| 37 | + /// </summary> |
| 38 | + public required int TextLength { get; init; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// SHA256 hash of the original text (hex string). |
| 42 | + /// The text itself is never stored for security/privacy. |
| 43 | + /// </summary> |
| 44 | + public required string TextHash { get; init; } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Creates a cache key from the given parameters. |
| 48 | + /// The text is hashed using SHA256 and not stored. |
| 49 | + /// </summary> |
| 50 | + /// <param name="provider">Provider type name.</param> |
| 51 | + /// <param name="model">Model name.</param> |
| 52 | + /// <param name="vectorDimensions">Vector dimensions.</param> |
| 53 | + /// <param name="isNormalized">Whether vectors are normalized.</param> |
| 54 | + /// <param name="text">The text to hash.</param> |
| 55 | + /// <returns>A new EmbeddingCacheKey instance.</returns> |
| 56 | + /// <exception cref="ArgumentNullException">When provider or model is null.</exception> |
| 57 | + /// <exception cref="ArgumentOutOfRangeException">When vectorDimensions is less than 1.</exception> |
| 58 | + public static EmbeddingCacheKey Create( |
| 59 | + string provider, |
| 60 | + string model, |
| 61 | + int vectorDimensions, |
| 62 | + bool isNormalized, |
| 63 | + string? text) |
| 64 | + { |
| 65 | + ArgumentNullException.ThrowIfNull(provider, nameof(provider)); |
| 66 | + ArgumentNullException.ThrowIfNull(model, nameof(model)); |
| 67 | + ArgumentOutOfRangeException.ThrowIfLessThan(vectorDimensions, 1, nameof(vectorDimensions)); |
| 68 | + |
| 69 | + // Normalize null to empty string for consistent hashing |
| 70 | + text ??= string.Empty; |
| 71 | + |
| 72 | + return new EmbeddingCacheKey |
| 73 | + { |
| 74 | + Provider = provider, |
| 75 | + Model = model, |
| 76 | + VectorDimensions = vectorDimensions, |
| 77 | + IsNormalized = isNormalized, |
| 78 | + TextLength = text.Length, |
| 79 | + TextHash = ComputeSha256Hash(text) |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Generates a composite key string for use as a database primary key. |
| 85 | + /// Format: Provider|Model|Dimensions|IsNormalized|TextLength|TextHash |
| 86 | + /// </summary> |
| 87 | + /// <returns>A string suitable for use as a cache key.</returns> |
| 88 | + public string ToCompositeKey() |
| 89 | + { |
| 90 | + return $"{this.Provider}|{this.Model}|{this.VectorDimensions}|{this.IsNormalized}|{this.TextLength}|{this.TextHash}"; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Computes SHA256 hash of the input text and returns as lowercase hex string. |
| 95 | + /// </summary> |
| 96 | + /// <param name="text">The text to hash.</param> |
| 97 | + /// <returns>64-character lowercase hex string.</returns> |
| 98 | + private static string ComputeSha256Hash(string text) |
| 99 | + { |
| 100 | + byte[] bytes = SHA256.HashData(Encoding.UTF8.GetBytes(text)); |
| 101 | + return Convert.ToHexStringLower(bytes); |
| 102 | + } |
| 103 | +} |
0 commit comments