@@ -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<AiSettings> options)
|
||||
public CachedRagAiClient(RagAiClient client, IRagRepository repository, IOptions<AiSettings> 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;
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<AiSettings> options)
|
||||
public RagAiClient(HttpClient http, IOptions<AiSettings> options)
|
||||
{
|
||||
_http = http;
|
||||
_settings = options.Value;
|
||||
@@ -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
|
||||
|
||||
+2
-2
@@ -34,9 +34,9 @@ try
|
||||
options.UseSqlServer(builder.Configuration.GetConnectionString("RagDb")
|
||||
?? throw new InvalidOperationException("Connection string 'RagDb' is missing.")));
|
||||
|
||||
builder.Services.AddHttpClient<RawAiClient>();
|
||||
builder.Services.AddHttpClient<RagAiClient>();
|
||||
builder.Services.AddScoped<IRagRepository, EfRagRepository>();
|
||||
builder.Services.AddScoped<IAiClient, CachedAiClient>();
|
||||
builder.Services.AddScoped<IAiClient, CachedRagAiClient>();
|
||||
builder.Services.AddSingleton<ITextExtractor, TextExtractor>();
|
||||
builder.Services.AddSingleton<ITextChunker, TextChunker>();
|
||||
builder.Services.AddSingleton<IDocumentClassifier, DocumentClassifier>();
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -77,6 +77,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\common-helpers\common-helpers.csproj" />
|
||||
<ProjectReference Include="..\rag-api-models\rag-api-models.csproj" />
|
||||
<ProjectReference Include="..\shared-models\shared-models.csproj" />
|
||||
<ProjectReference Include="..\startup-helpers\startup-helpers.csproj" />
|
||||
|
||||
Reference in New Issue
Block a user