Changes
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace Api.Clients.Ai.Contracts;
|
||||
|
||||
public interface IMatcherAiClient
|
||||
{
|
||||
Task<string> CreateChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Api.Clients.Ai.Contracts;
|
||||
using Api.Data.Repositories.Contracts;
|
||||
using Api.Models.Settings;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Api.Clients.Ai;
|
||||
|
||||
public sealed class MatcherAiClient : IMatcherAiClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly IMatcherRepository _repository;
|
||||
private readonly AiSettings _settings;
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
|
||||
public MatcherAiClient(HttpClient http, IMatcherRepository repository, IOptions<AiSettings> options)
|
||||
{
|
||||
_http = http;
|
||||
_repository = repository;
|
||||
_settings = options.Value;
|
||||
}
|
||||
|
||||
public async Task<string> CreateChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct)
|
||||
{
|
||||
var model = GetModel();
|
||||
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 = IsOllama()
|
||||
? await CreateOllamaChatCompletionAsync(systemPrompt, userPrompt, temperature, ct)
|
||||
: await CreateOpenAiChatCompletionAsync(systemPrompt, userPrompt, temperature, ct);
|
||||
|
||||
await _repository.SaveChatCompletionAsync(cacheKey, model, temperature, response, ct);
|
||||
return response;
|
||||
}
|
||||
|
||||
private bool IsOllama() => string.Equals(_settings.Provider, "Ollama", StringComparison.OrdinalIgnoreCase);
|
||||
private string GetModel() => IsOllama() ? _settings.Ollama.ChatModel : _settings.OpenAI.ChatModel;
|
||||
|
||||
private async Task<string> CreateOpenAiChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_settings.OpenAI.ApiKey)) throw new InvalidOperationException("OpenAI API key is missing.");
|
||||
using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.openai.com/v1/chat/completions");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _settings.OpenAI.ApiKey);
|
||||
request.Content = ToJson(new
|
||||
{
|
||||
model = _settings.OpenAI.ChatModel,
|
||||
temperature,
|
||||
response_format = new { type = "json_object" },
|
||||
messages = new[]
|
||||
{
|
||||
new { role = "system", content = systemPrompt },
|
||||
new { role = "user", content = userPrompt }
|
||||
}
|
||||
});
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(Math.Max(15, _settings.OpenAI.TimeoutSeconds)));
|
||||
using var response = await _http.SendAsync(request, cts.Token);
|
||||
var json = await response.Content.ReadAsStringAsync(cts.Token);
|
||||
if (!response.IsSuccessStatusCode) throw new InvalidOperationException($"OpenAI chat failed: {(int)response.StatusCode} {json}");
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
return doc.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString() ?? "{}";
|
||||
}
|
||||
|
||||
private async Task<string> CreateOllamaChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct)
|
||||
{
|
||||
var baseUrl = _settings.Ollama.BaseUrl.TrimEnd('/');
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(Math.Max(30, _settings.Ollama.TimeoutSeconds)));
|
||||
using var response = await _http.PostAsync($"{baseUrl}/api/chat", ToJson(new
|
||||
{
|
||||
model = _settings.Ollama.ChatModel,
|
||||
stream = false,
|
||||
format = "json",
|
||||
messages = new[]
|
||||
{
|
||||
new { role = "system", content = systemPrompt },
|
||||
new { role = "user", content = userPrompt }
|
||||
},
|
||||
options = new { temperature = (float)temperature }
|
||||
}), cts.Token);
|
||||
var json = await response.Content.ReadAsStringAsync(cts.Token);
|
||||
if (!response.IsSuccessStatusCode) throw new InvalidOperationException($"Ollama chat failed: {(int)response.StatusCode} {json}");
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
return doc.RootElement.GetProperty("message").GetProperty("content").GetString() ?? "{}";
|
||||
}
|
||||
|
||||
private static StringContent ToJson<T>(T payload) => new(JsonSerializer.Serialize(payload, JsonOptions), Encoding.UTF8, "application/json");
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Api.Models.Requests;
|
||||
using Api.Models.Responses;
|
||||
|
||||
namespace Api.Clients.Api.Contracts;
|
||||
|
||||
public interface IRagApiClient
|
||||
{
|
||||
Task<RagIndexResponse> IndexCvPdfAsync(IFormFile file, CancellationToken ct);
|
||||
Task<RagIndexResponse> IndexJobTextAsync(string text, string? url, string? title, CancellationToken ct);
|
||||
Task<RagDocumentDetails?> GetDocumentAsync(string documentId, CancellationToken ct);
|
||||
Task<RagSearchResponse> SearchAsync(RagSearchRequest request, CancellationToken ct);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Refit;
|
||||
using Api.Models.Responses;
|
||||
using Api.Models.Requests;
|
||||
|
||||
namespace Api.Clients.Api.Contracts;
|
||||
|
||||
[Headers("Accept: application/json")]
|
||||
public interface IRefitRagApi
|
||||
{
|
||||
[Multipart]
|
||||
[Post("/api/rag/documents")]
|
||||
Task<RagIndexResponse> IndexDocumentAsync([AliasAs("file")] StreamPart file,
|
||||
[AliasAs("documentType")] string documentType,
|
||||
[AliasAs("title")] string title,
|
||||
CancellationToken ct = default);
|
||||
|
||||
[Multipart]
|
||||
[Post("/api/rag/documents")]
|
||||
Task<RagIndexResponse> IndexDocumentWithTextAsync([AliasAs("text")] string text,
|
||||
[AliasAs("documentType")] string documentType,
|
||||
[AliasAs("title")] string title,
|
||||
[AliasAs("sourceUrl")] string? sourceUrl = null,
|
||||
CancellationToken ct = default);
|
||||
|
||||
[Get("/api/rag/documents/{documentId}")]
|
||||
Task<RagDocumentDetails> GetDocumentAsync(string documentId, CancellationToken ct = default);
|
||||
|
||||
[Post("/api/rag/search")]
|
||||
Task<RagSearchResponse> SearchAsync([Body] RagSearchRequest request, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Net;
|
||||
using Refit;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Api.Clients.Api.Contracts;
|
||||
using Api.Models.Responses;
|
||||
using Api.Models.Settings;
|
||||
using Api.Models.Requests;
|
||||
|
||||
namespace Api.Clients.Api;
|
||||
|
||||
public sealed class RagApiClient : IRagApiClient
|
||||
{
|
||||
private readonly IRefitRagApi _refit;
|
||||
|
||||
public RagApiClient(IRefitRagApi refit, IOptions<RagApiSettings> options)
|
||||
{
|
||||
_refit = refit;
|
||||
}
|
||||
|
||||
public async Task<RagIndexResponse> IndexCvPdfAsync(IFormFile file, CancellationToken ct)
|
||||
{
|
||||
await using var stream = file.OpenReadStream();
|
||||
var part = new StreamPart(stream, file.FileName, "application/pdf");
|
||||
return await _refit.IndexDocumentAsync(part, "cv", file.FileName, ct);
|
||||
}
|
||||
|
||||
public async Task<RagIndexResponse> IndexJobTextAsync(string text, string? url, string? title, CancellationToken ct)
|
||||
{
|
||||
return await _refit.IndexDocumentWithTextAsync(text, "job", title ?? "Job description", url, ct);
|
||||
}
|
||||
|
||||
public async Task<RagDocumentDetails?> GetDocumentAsync(string documentId, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _refit.GetDocumentAsync(documentId, ct);
|
||||
}
|
||||
catch (ApiException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<RagSearchResponse> SearchAsync(RagSearchRequest request, CancellationToken ct)
|
||||
{
|
||||
return await _refit.SearchAsync(request, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user