diff --git a/common-helpers/HashHelpers.cs b/common-helpers/HashHelpers.cs new file mode 100644 index 0000000..00b8dcb --- /dev/null +++ b/common-helpers/HashHelpers.cs @@ -0,0 +1,14 @@ +using System.Security.Cryptography; +using System.Text; + +namespace CommonHelpers +{ + public static class HashHelper + { + public static string Compute(string value) + { + using var sha = SHA256.Create(); + return Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(value ?? string.Empty))); + } + } +} diff --git a/common-helpers/common-helpers.csproj b/common-helpers/common-helpers.csproj new file mode 100644 index 0000000..00b739b --- /dev/null +++ b/common-helpers/common-helpers.csproj @@ -0,0 +1,10 @@ + + + + net10.0 + CommonHelpers + enable + enable + + + diff --git a/cv-matcher-api/Clients/Ai/HashHelper.cs b/cv-matcher-api/Clients/Ai/HashHelper.cs deleted file mode 100644 index 33950c6..0000000 --- a/cv-matcher-api/Clients/Ai/HashHelper.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Security.Cryptography; -using System.Text; - -namespace Api.Clients.Ai; - -public static class HashHelper -{ - public static string Compute(string value) - { - using var sha = SHA256.Create(); - return Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(value ?? string.Empty))); - } -} diff --git a/cv-matcher-api/Clients/Ai/MatcherAiClient.cs b/cv-matcher-api/Clients/Ai/MatcherAiClient.cs index e5e8374..ae8f9cb 100644 --- a/cv-matcher-api/Clients/Ai/MatcherAiClient.cs +++ b/cv-matcher-api/Clients/Ai/MatcherAiClient.cs @@ -4,6 +4,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using Api.Clients.Ai.Contracts; using Api.Data.Repositories.Contracts; +using CommonHelpers; using CvMatcher.Models.Settings; using Microsoft.Extensions.Options; diff --git a/cv-matcher-api/Dockerfile b/cv-matcher-api/Dockerfile index e1c653a..ca99573 100644 --- a/cv-matcher-api/Dockerfile +++ b/cv-matcher-api/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /src COPY cv-matcher-api/cv-matcher-api.csproj cv-matcher-api/ COPY shared-models/shared-models.csproj shared-models/ COPY cv-matcher-api-models/cv-matcher-api-models.csproj cv-matcher-api-models/ +COPY common-helpers/ common-helpers/ COPY startup-helpers/startup-helpers.csproj startup-helpers/ RUN dotnet restore cv-matcher-api/cv-matcher-api.csproj @@ -12,6 +13,7 @@ RUN dotnet restore cv-matcher-api/cv-matcher-api.csproj COPY cv-matcher-api/ cv-matcher-api/ COPY shared-models/ shared-models/ COPY cv-matcher-api-models/ cv-matcher-api-models/ +COPY common-helpers/ common-helpers/ COPY startup-helpers/ startup-helpers/ RUN dotnet publish cv-matcher-api/cv-matcher-api.csproj -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false diff --git a/cv-matcher-api/cv-matcher-api.csproj b/cv-matcher-api/cv-matcher-api.csproj index ded3a6a..e430aeb 100644 --- a/cv-matcher-api/cv-matcher-api.csproj +++ b/cv-matcher-api/cv-matcher-api.csproj @@ -77,6 +77,7 @@ + diff --git a/myAi.sln b/myAi.sln index 6435304..0684ac2 100644 --- a/myAi.sln +++ b/myAi.sln @@ -28,6 +28,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Helpers", "Helpers", "{43E9 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "startup-helpers", "startup-helpers\startup-helpers.csproj", "{7446D193-8636-4E58-96E4-0C8CB8790679}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "common-helpers", "common-helpers\common-helpers.csproj", "{4EDDEE9A-E9C7-4972-9C4A-3177611CCFE3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -74,6 +76,10 @@ Global {7446D193-8636-4E58-96E4-0C8CB8790679}.Debug|Any CPU.Build.0 = Debug|Any CPU {7446D193-8636-4E58-96E4-0C8CB8790679}.Release|Any CPU.ActiveCfg = Release|Any CPU {7446D193-8636-4E58-96E4-0C8CB8790679}.Release|Any CPU.Build.0 = Release|Any CPU + {4EDDEE9A-E9C7-4972-9C4A-3177611CCFE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4EDDEE9A-E9C7-4972-9C4A-3177611CCFE3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4EDDEE9A-E9C7-4972-9C4A-3177611CCFE3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4EDDEE9A-E9C7-4972-9C4A-3177611CCFE3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -87,6 +93,7 @@ Global {6A1ADA81-28E9-4A64-A32D-0755876D5EB7} = {E08A1D43-24A3-4F93-B66A-4230FD8261BA} {185A8BB0-344A-4856-AEB4-213866EB2EE7} = {E08A1D43-24A3-4F93-B66A-4230FD8261BA} {7446D193-8636-4E58-96E4-0C8CB8790679} = {43E9CD21-25B6-4CB4-B94E-5B953B2E1284} + {4EDDEE9A-E9C7-4972-9C4A-3177611CCFE3} = {43E9CD21-25B6-4CB4-B94E-5B953B2E1284} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6246A67B-299E-4E64-8DBE-1A66771E7C67} diff --git a/rag-api/Clients/Ai/CachedAiClient.cs b/rag-api/Clients/Ai/CachedRagAiClient.cs similarity index 81% rename from rag-api/Clients/Ai/CachedAiClient.cs rename to rag-api/Clients/Ai/CachedRagAiClient.cs index 22a472f..4821285 100644 --- a/rag-api/Clients/Ai/CachedAiClient.cs +++ b/rag-api/Clients/Ai/CachedRagAiClient.cs @@ -2,18 +2,19 @@ using Microsoft.Extensions.Options; using Rag.Models.Settings; using Api.Data.Repositories.Contracts; using Api.Clients.Ai.Contracts; +using CommonHelpers; namespace Api.Clients.Ai; -public sealed class CachedAiClient : IAiClient +public sealed class CachedRagAiClient : IAiClient { - private readonly RawAiClient _raw; + private readonly RagAiClient _client; private readonly IRagRepository _repository; private readonly AiSettings _settings; - public CachedAiClient(RawAiClient raw, IRagRepository repository, IOptions options) + public CachedRagAiClient(RagAiClient client, IRagRepository repository, IOptions options) { - _raw = raw; + _client = client; _repository = repository; _settings = options.Value; } @@ -26,7 +27,7 @@ public sealed class CachedAiClient : IAiClient var cached = await _repository.GetEmbeddingAsync(cacheKey, ct); if (cached is not null) return cached; - var vector = await _raw.CreateEmbeddingAsync(input, ct); + var vector = await _client.CreateEmbeddingAsync(input, ct); await _repository.SaveEmbeddingAsync(cacheKey, model, textHash, vector, ct); return vector; } @@ -38,7 +39,7 @@ public sealed class CachedAiClient : IAiClient var cached = await _repository.GetChatCompletionAsync(cacheKey, ct); if (cached is not null) return cached; - var response = await _raw.CreateChatCompletionAsync(systemPrompt, userPrompt, temperature, ct); + var response = await _client.CreateChatCompletionAsync(systemPrompt, userPrompt, temperature, ct); await _repository.SaveChatCompletionAsync(cacheKey, model, temperature, response, ct); return response; } diff --git a/rag-api/Clients/Ai/HashHelper.cs b/rag-api/Clients/Ai/HashHelper.cs deleted file mode 100644 index e1a67a8..0000000 --- a/rag-api/Clients/Ai/HashHelper.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Security.Cryptography; -using System.Text; - -namespace Api.Clients.Ai; - -public static class HashHelper -{ - public static string Compute(string value) - { - using var sha = SHA256.Create(); - var bytes = sha.ComputeHash(Encoding.UTF8.GetBytes(value ?? string.Empty)); - return Convert.ToHexString(bytes); - } -} diff --git a/rag-api/Clients/Ai/RawAiClient.cs b/rag-api/Clients/Ai/RagAiClient.cs similarity index 98% rename from rag-api/Clients/Ai/RawAiClient.cs rename to rag-api/Clients/Ai/RagAiClient.cs index 2ce5e59..3c07821 100644 --- a/rag-api/Clients/Ai/RawAiClient.cs +++ b/rag-api/Clients/Ai/RagAiClient.cs @@ -8,7 +8,7 @@ using Api.Clients.Ai.Contracts; namespace Api.Clients.Ai; -public sealed class RawAiClient : IAiClient +public sealed class RagAiClient : IAiClient { private readonly HttpClient _http; private readonly AiSettings _settings; @@ -17,7 +17,7 @@ public sealed class RawAiClient : IAiClient DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; - public RawAiClient(HttpClient http, IOptions options) + public RagAiClient(HttpClient http, IOptions options) { _http = http; _settings = options.Value; diff --git a/rag-api/Dockerfile b/rag-api/Dockerfile index f3c9975..13f9459 100644 --- a/rag-api/Dockerfile +++ b/rag-api/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /src COPY rag-api/rag-api.csproj rag-api/ COPY shared-models/shared-models.csproj shared-models/ COPY rag-api-models/rag-api-models.csproj rag-api-models/ +COPY common-helpers/common-helpers.csproj common-helpers/ COPY startup-helpers/startup-helpers.csproj startup-helpers/ RUN dotnet restore rag-api/rag-api.csproj @@ -13,6 +14,7 @@ COPY rag-api/ rag-api/ COPY shared-models/ shared-models/ COPY rag-api-models/ rag-api-models/ COPY shared-models/ shared-models/ +COPY common-helpers/ common-helpers/ COPY startup-helpers/ startup-helpers/ RUN dotnet publish rag-api/rag-api.csproj -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false diff --git a/rag-api/Program.cs b/rag-api/Program.cs index fd43a3e..fdcc3a1 100644 --- a/rag-api/Program.cs +++ b/rag-api/Program.cs @@ -34,9 +34,9 @@ try options.UseSqlServer(builder.Configuration.GetConnectionString("RagDb") ?? throw new InvalidOperationException("Connection string 'RagDb' is missing."))); - builder.Services.AddHttpClient(); + builder.Services.AddHttpClient(); builder.Services.AddScoped(); - builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); diff --git a/rag-api/Services/RagService.cs b/rag-api/Services/RagService.cs index f744d4b..9799feb 100644 --- a/rag-api/Services/RagService.cs +++ b/rag-api/Services/RagService.cs @@ -6,8 +6,8 @@ using Rag.Models.Responses; using Rag.Models.Settings; using Api.Data.Repositories.Contracts; using Api.Clients.Ai.Contracts; -using Api.Clients.Ai; using Rag.Models; +using CommonHelpers; namespace Api.Services; diff --git a/rag-api/rag-api.csproj b/rag-api/rag-api.csproj index 8fc583f..c4dc2dc 100644 --- a/rag-api/rag-api.csproj +++ b/rag-api/rag-api.csproj @@ -77,6 +77,7 @@ + diff --git a/startup-helpers/Class1.cs b/startup-helpers/Class1.cs deleted file mode 100644 index 9393d11..0000000 --- a/startup-helpers/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace startup_helpers -{ - public class Class1 - { - - } -}