| | | 1 | | using Intentum.AI.Embeddings; |
| | | 2 | | using Microsoft.Extensions.Diagnostics.HealthChecks; |
| | | 3 | | |
| | | 4 | | namespace Intentum.AspNetCore.HealthChecks; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Health check for embedding providers. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class EmbeddingProviderHealthCheck : IHealthCheck |
| | | 10 | | { |
| | | 11 | | private readonly IIntentEmbeddingProvider _embeddingProvider; |
| | | 12 | | |
| | 2 | 13 | | public EmbeddingProviderHealthCheck(IIntentEmbeddingProvider embeddingProvider) |
| | 2 | 14 | | { |
| | 2 | 15 | | _embeddingProvider = embeddingProvider ?? throw new ArgumentNullException(nameof(embeddingProvider)); |
| | 2 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task<HealthCheckResult> CheckHealthAsync( |
| | | 19 | | HealthCheckContext context, |
| | | 20 | | CancellationToken cancellationToken = default) |
| | 2 | 21 | | { |
| | | 22 | | try |
| | 2 | 23 | | { |
| | 2 | 24 | | var testKey = "health:check"; |
| | 2 | 25 | | var embedding = await _embeddingProvider.EmbedAsync(testKey, cancellationToken); |
| | 1 | 26 | | return HealthCheckResult.Healthy( |
| | 1 | 27 | | $"Embedding provider is healthy. Test embedding score: {embedding.Score:F2}"); |
| | | 28 | | } |
| | 1 | 29 | | catch (Exception ex) |
| | 1 | 30 | | { |
| | 1 | 31 | | return HealthCheckResult.Unhealthy( |
| | 1 | 32 | | "Embedding provider health check failed", |
| | 1 | 33 | | ex); |
| | | 34 | | } |
| | 2 | 35 | | } |
| | | 36 | | } |