@@ -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;
|
||||
Reference in New Issue
Block a user