From e3bfcb11a484ad36c15ddd6801a667f5de61ff8f Mon Sep 17 00:00:00 2001 From: Gelu Mihes Date: Wed, 6 May 2026 20:04:24 +0300 Subject: [PATCH] Changes --- .../Clients/Ai/CachedMatcherAiClient.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cv-matcher-api/Clients/Ai/CachedMatcherAiClient.cs diff --git a/cv-matcher-api/Clients/Ai/CachedMatcherAiClient.cs b/cv-matcher-api/Clients/Ai/CachedMatcherAiClient.cs new file mode 100644 index 0000000..05014e6 --- /dev/null +++ b/cv-matcher-api/Clients/Ai/CachedMatcherAiClient.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Options; +using CvMatcher.Models.Settings; +using Api.Data.Repositories.Contracts; +using Api.Clients.Ai.Contracts; +using CommonHelpers; + +namespace Api.Clients.Ai; + +public sealed class CachedMatcherAiClient : IMatcherAiClient +{ + private readonly MatcherAiClient _client; + private readonly IMatcherRepository _repository; + private readonly AiSettings _settings; + + public CachedMatcherAiClient(MatcherAiClient client, IMatcherRepository repository, IOptions options) + { + _client = client; + _repository = repository; + _settings = options.Value; + } + + public async Task CreateChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct) + { + var model = GetChatModel(); + var cacheKey = HashHelper.Compute($"chat:{_settings.Provider}:{model}:{temperature:0.00}:{systemPrompt}:{userPrompt}"); + var cached = await _repository.GetChatCompletionAsync(cacheKey, ct); + if (cached is not null) return cached; + + var response = await _client.CreateChatCompletionAsync(systemPrompt, userPrompt, temperature, ct); + await _repository.SaveChatCompletionAsync(cacheKey, model, temperature, response, ct); + return response; + } + + private string GetChatModel() => string.Equals(_settings.Provider, "Ollama", StringComparison.OrdinalIgnoreCase) + ? _settings.Ollama.ChatModel + : _settings.OpenAI.ChatModel; +}