| | | 1 | | using Intentum.AI.Embeddings; |
| | | 2 | | |
| | | 3 | | namespace Intentum.AI.Caching; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Wrapper around an embedding provider that adds caching functionality. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class CachedEmbeddingProvider : IIntentEmbeddingProvider |
| | | 9 | | { |
| | | 10 | | private readonly IIntentEmbeddingProvider _innerProvider; |
| | | 11 | | private readonly IEmbeddingCache _cache; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Creates a cached embedding provider. |
| | | 15 | | /// </summary> |
| | 2 | 16 | | public CachedEmbeddingProvider( |
| | 2 | 17 | | IIntentEmbeddingProvider innerProvider, |
| | 2 | 18 | | IEmbeddingCache cache) |
| | 2 | 19 | | { |
| | 2 | 20 | | _innerProvider = innerProvider ?? throw new ArgumentNullException(nameof(innerProvider)); |
| | 2 | 21 | | _cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | public IntentEmbedding Embed(string behaviorKey) |
| | 6 | 25 | | { |
| | 6 | 26 | | var cached = _cache.Get(behaviorKey); |
| | 6 | 27 | | if (cached != null) |
| | 3 | 28 | | return cached; |
| | | 29 | | |
| | 3 | 30 | | var embedding = _innerProvider.Embed(behaviorKey); |
| | 3 | 31 | | _cache.Set(behaviorKey, embedding); |
| | 3 | 32 | | return embedding; |
| | 6 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<IntentEmbedding> EmbedAsync(string behaviorKey, CancellationToken cancellationToken = default) |
| | 0 | 36 | | { |
| | 0 | 37 | | var cached = _cache.Get(behaviorKey); |
| | 0 | 38 | | if (cached != null) |
| | 0 | 39 | | return cached; |
| | | 40 | | |
| | 0 | 41 | | var embedding = await _innerProvider.EmbedAsync(behaviorKey, cancellationToken); |
| | 0 | 42 | | _cache.Set(behaviorKey, embedding); |
| | 0 | 43 | | return embedding; |
| | 0 | 44 | | } |
| | | 45 | | } |