Changes
Build and Push Docker Images / build (push) Successful in 37s

This commit is contained in:
2026-05-04 21:02:35 +03:00
parent 34625ae242
commit fa1ef23c02
87 changed files with 3151 additions and 522 deletions
+52
View File
@@ -0,0 +1,52 @@
using Microsoft.Extensions.Options;
using Api.Services.Contracts;
using Api.Settings;
namespace Api.Services;
public sealed class CachedAiClient : IAiClient
{
private readonly RawAiClient _raw;
private readonly IRagRepository _repository;
private readonly AiSettings _settings;
public CachedAiClient(RawAiClient raw, IRagRepository repository, IOptions<AiSettings> options)
{
_raw = raw;
_repository = repository;
_settings = options.Value;
}
public async Task<float[]> CreateEmbeddingAsync(string input, CancellationToken ct)
{
var model = GetEmbeddingModel();
var textHash = HashHelper.Compute(input);
var cacheKey = HashHelper.Compute($"embedding:{_settings.Provider}:{model}:{textHash}");
var cached = await _repository.GetEmbeddingAsync(cacheKey, ct);
if (cached is not null) return cached;
var vector = await _raw.CreateEmbeddingAsync(input, ct);
await _repository.SaveEmbeddingAsync(cacheKey, model, textHash, vector, ct);
return vector;
}
public async Task<string> 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 _raw.CreateChatCompletionAsync(systemPrompt, userPrompt, temperature, ct);
await _repository.SaveChatCompletionAsync(cacheKey, model, temperature, response, ct);
return response;
}
private string GetEmbeddingModel() => string.Equals(_settings.Provider, "Ollama", StringComparison.OrdinalIgnoreCase)
? _settings.Ollama.EmbeddingModel
: _settings.OpenAI.EmbeddingModel;
private string GetChatModel() => string.Equals(_settings.Provider, "Ollama", StringComparison.OrdinalIgnoreCase)
? _settings.Ollama.ChatModel
: _settings.OpenAI.ChatModel;
}
+7
View File
@@ -0,0 +1,7 @@
namespace Api.Services.Contracts;
public interface IAiClient
{
Task<float[]> CreateEmbeddingAsync(string input, CancellationToken ct);
Task<string> CreateChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct);
}
@@ -0,0 +1,8 @@
using Api.Services.Contracts.Models;
namespace Api.Services.Contracts;
public interface IDocumentClassifier
{
Task<DocumentClassification> ClassifyAsync(string text, string? providedType, string? providedTitle, CancellationToken ct);
}
@@ -0,0 +1,16 @@
using Api.Services.Contracts.Models;
namespace Api.Services.Contracts;
public interface IRagRepository
{
Task InitializeAsync(CancellationToken ct);
Task<RagDocumentRecord?> GetDocumentByTextHashAsync(string textHash, string? sourceUrl, CancellationToken ct);
Task<RagDocumentRecord?> GetDocumentByIdAsync(string id, CancellationToken ct);
Task SaveDocumentAsync(RagDocumentRecord document, IReadOnlyList<RagChunkRecord> chunks, CancellationToken ct);
Task<IReadOnlyList<SearchCandidateChunk>> SearchChunksAsync(float[] queryEmbedding, IReadOnlyList<string>? targetTypes, int topK, CancellationToken ct);
Task<float[]?> GetEmbeddingAsync(string cacheKey, CancellationToken ct);
Task SaveEmbeddingAsync(string cacheKey, string model, string textHash, float[] vector, CancellationToken ct);
Task<string?> GetChatCompletionAsync(string cacheKey, CancellationToken ct);
Task SaveChatCompletionAsync(string cacheKey, string model, decimal temperature, string responseText, CancellationToken ct);
}
+13
View File
@@ -0,0 +1,13 @@
using Api.Requests;
using Api.Responses;
using Api.Services.Contracts.Models;
namespace Api.Services.Contracts;
public interface IRagService
{
Task<IndexDocumentResponse> IndexTextAsync(IndexDocumentRequest request, CancellationToken ct);
Task<IndexDocumentResponse> IndexPdfAsync(IFormFile file, string? documentType, string? title, string? sourceUrl, CancellationToken ct);
Task<SearchResponse> SearchAsync(SearchRequest request, CancellationToken ct);
Task<RagDocumentDetails?> GetDocumentAsync(string documentId, CancellationToken ct);
}
@@ -0,0 +1,6 @@
namespace Api.Services.Contracts;
public interface ITextChunker
{
IReadOnlyList<string> Chunk(string text, int chunkSize, int overlap);
}
@@ -0,0 +1,7 @@
namespace Api.Services.Contracts;
public interface ITextExtractor
{
Task<string> ExtractPdfAsync(Stream stream, CancellationToken ct);
string Normalize(string value);
}
@@ -0,0 +1,10 @@
namespace Api.Services.Contracts.Models
{
public sealed class DocumentClassification
{
public required string DocumentType { get; init; }
public double Confidence { get; init; }
public required string Title { get; init; }
public Dictionary<string, string> Metadata { get; init; } = [];
}
}
@@ -0,0 +1,11 @@
namespace Api.Services.Contracts.Models
{
public sealed class RagChunkRecord
{
public required string Id { get; init; }
public required string DocumentId { get; init; }
public int ChunkIndex { get; init; }
public required string Text { get; init; }
public required float[] Embedding { get; init; }
}
}
@@ -0,0 +1,13 @@
namespace Api.Services.Contracts.Models
{
public sealed class RagDocumentDetails
{
public required string Id { get; init; }
public required string DocumentType { get; init; }
public required string Title { get; init; }
public string? SourceUrl { get; init; }
public required string Text { get; init; }
public required string TextHash { get; init; }
public DateTimeOffset CreatedAt { get; init; }
}
}
@@ -0,0 +1,15 @@
namespace Api.Services.Contracts.Models
{
public sealed class RagDocumentRecord
{
public required string Id { get; init; }
public required string DocumentType { get; init; }
public required string Title { get; init; }
public string? SourceUrl { get; init; }
public required string Text { get; init; }
public required string TextHash { get; init; }
public double TypeConfidence { get; init; }
public string MetadataJson { get; init; } = "{}";
public DateTimeOffset CreatedAt { get; init; }
}
}
@@ -0,0 +1,9 @@
namespace Api.Services.Contracts.Models
{
public sealed class SearchCandidateChunk
{
public required RagDocumentRecord Document { get; init; }
public required RagChunkRecord Chunk { get; init; }
public double Score { get; init; }
}
}
+65
View File
@@ -0,0 +1,65 @@
using System.Text.RegularExpressions;
using Api.Services.Contracts;
using Api.Services.Contracts.Models;
namespace Api.Services;
public sealed class DocumentClassifier : IDocumentClassifier
{
private static readonly HashSet<string> KnownTypes = new(StringComparer.OrdinalIgnoreCase)
{
"cv", "job", "article", "contract", "invoice", "product", "documentation", "unknown"
};
public Task<DocumentClassification> ClassifyAsync(string text, string? providedType, string? providedTitle, CancellationToken ct)
{
if (!string.IsNullOrWhiteSpace(providedType))
{
var normalized = NormalizeType(providedType);
return Task.FromResult(new DocumentClassification
{
DocumentType = normalized,
Confidence = KnownTypes.Contains(normalized) && normalized != "unknown" ? 1.0 : 0.6,
Title = BuildTitle(providedTitle, text, normalized)
});
}
var lower = text.ToLowerInvariant();
var scores = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
["cv"] = Count(lower, "curriculum vitae", "resume", "work experience", "professional experience", "education", "skills", "technologies", "linkedin", "github"),
["job"] = Count(lower, "job description", "requirements", "responsibilities", "qualifications", "apply", "we are looking", "salary", "benefits", "remote", "hybrid"),
["contract"] = Count(lower, "agreement", "contract", "party", "parties", "liability", "termination", "confidentiality", "governing law"),
["invoice"] = Count(lower, "invoice", "vat", "subtotal", "total", "amount due", "due date", "billing"),
["documentation"] = Count(lower, "api", "endpoint", "configuration", "install", "usage", "parameters", "response", "request"),
["product"] = Count(lower, "features", "pricing", "sku", "product", "specification", "warranty")
};
var best = scores.OrderByDescending(x => x.Value).First();
var type = best.Value <= 0 ? "unknown" : best.Key;
var confidence = best.Value <= 0 ? 0.25 : Math.Min(0.95, 0.45 + best.Value * 0.08);
return Task.FromResult(new DocumentClassification
{
DocumentType = type,
Confidence = confidence,
Title = BuildTitle(providedTitle, text, type)
});
}
private static int Count(string lower, params string[] terms) => terms.Count(term => lower.Contains(term));
private static string NormalizeType(string value)
{
var cleaned = Regex.Replace(value.Trim().ToLowerInvariant(), "[^a-z0-9_-]", "-");
return string.IsNullOrWhiteSpace(cleaned) ? "unknown" : cleaned;
}
private static string BuildTitle(string? providedTitle, string text, string documentType)
{
if (!string.IsNullOrWhiteSpace(providedTitle)) return providedTitle.Trim();
var firstLine = text.Split('.', '\n', '\r').Select(x => x.Trim()).FirstOrDefault(x => x.Length > 20);
if (!string.IsNullOrWhiteSpace(firstLine)) return firstLine.Length <= 120 ? firstLine : firstLine[..120];
return $"{documentType} document";
}
}
+14
View File
@@ -0,0 +1,14 @@
using System.Security.Cryptography;
using System.Text;
namespace Api.Services;
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);
}
}
+179
View File
@@ -0,0 +1,179 @@
using System.Text.Json;
using Microsoft.Extensions.Options;
using Api.Services.Contracts;
using Api.Settings;
using Api.Responses;
using Api.Requests;
using Api.Services.Contracts.Models;
namespace Api.Services;
public sealed class RagService : IRagService
{
private readonly ITextExtractor _textExtractor;
private readonly ITextChunker _chunker;
private readonly IDocumentClassifier _classifier;
private readonly IAiClient _ai;
private readonly IRagRepository _repository;
private readonly RagSettings _settings;
public RagService(
ITextExtractor textExtractor,
ITextChunker chunker,
IDocumentClassifier classifier,
IAiClient ai,
IRagRepository repository,
IOptions<RagSettings> options)
{
_textExtractor = textExtractor;
_chunker = chunker;
_classifier = classifier;
_ai = ai;
_repository = repository;
_settings = options.Value;
}
public async Task<IndexDocumentResponse> IndexTextAsync(IndexDocumentRequest request, CancellationToken ct)
{
var text = _textExtractor.Normalize(request.Text ?? string.Empty);
if (text.Length < 40) throw new InvalidOperationException("Document text is too short.");
if (text.Length > _settings.MaxTextChars) text = text[.._settings.MaxTextChars];
return await IndexNormalizedTextAsync(text, request.DocumentType, request.Title, request.SourceUrl, request.Metadata, ct);
}
public async Task<IndexDocumentResponse> IndexPdfAsync(IFormFile file, string? documentType, string? title, string? sourceUrl, CancellationToken ct)
{
if (file.Length <= 0) throw new InvalidOperationException("Uploaded file is empty.");
if (file.Length > _settings.MaxFileSizeMb * 1024L * 1024L) throw new InvalidOperationException($"File is too large. Max size is {_settings.MaxFileSizeMb} MB.");
if (!string.Equals(Path.GetExtension(file.FileName), ".pdf", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("Only PDF files are supported by this endpoint.");
await using var stream = file.OpenReadStream();
var text = await _textExtractor.ExtractPdfAsync(stream, ct);
if (text.Length > _settings.MaxTextChars) text = text[.._settings.MaxTextChars];
if (text.Length < 40) throw new InvalidOperationException("Could not extract enough text from the PDF.");
return await IndexNormalizedTextAsync(text, documentType, title ?? file.FileName, sourceUrl, new Dictionary<string, string> { ["fileName"] = file.FileName }, ct);
}
public async Task<SearchResponse> SearchAsync(SearchRequest request, CancellationToken ct)
{
var query = _textExtractor.Normalize(request.QueryText);
if (query.Length < 10) throw new InvalidOperationException("Search query is too short.");
var topK = Math.Clamp(request.TopK ?? _settings.DefaultTopK, 1, Math.Max(1, _settings.MaxTopK));
var queryEmbedding = await _ai.CreateEmbeddingAsync(query, ct);
var candidates = await _repository.SearchChunksAsync(queryEmbedding, request.TargetDocumentTypes, topK, ct);
var results = candidates
.GroupBy(x => x.Document.Id)
.Select(group =>
{
var best = group.OrderByDescending(x => x.Score).First();
return new SearchDocumentResult
{
DocumentId = best.Document.Id,
DocumentType = best.Document.DocumentType,
Title = best.Document.Title,
SourceUrl = best.Document.SourceUrl,
Score = group.Max(x => x.Score),
MatchedChunks = group
.OrderByDescending(x => x.Score)
.Take(3)
.Select(x => new SearchChunkResult
{
ChunkId = x.Chunk.Id,
ChunkIndex = x.Chunk.ChunkIndex,
Text = x.Chunk.Text,
Score = x.Score
})
.ToList()
};
})
.OrderByDescending(x => x.Score)
.Take(topK)
.ToList();
return new SearchResponse { Results = results };
}
public async Task<RagDocumentDetails?> GetDocumentAsync(string documentId, CancellationToken ct)
{
var document = await _repository.GetDocumentByIdAsync(documentId, ct);
return document is null ? null : new RagDocumentDetails
{
Id = document.Id,
DocumentType = document.DocumentType,
Title = document.Title,
SourceUrl = document.SourceUrl,
Text = document.Text,
TextHash = document.TextHash,
CreatedAt = document.CreatedAt
};
}
private async Task<IndexDocumentResponse> IndexNormalizedTextAsync(
string text,
string? documentType,
string? title,
string? sourceUrl,
Dictionary<string, string>? metadata,
CancellationToken ct)
{
var textHash = HashHelper.Compute(text);
var cached = await _repository.GetDocumentByTextHashAsync(textHash, sourceUrl, ct);
if (cached is not null)
{
return new IndexDocumentResponse
{
DocumentId = cached.Id,
TextHash = cached.TextHash,
DocumentType = cached.DocumentType,
DocumentTypeConfidence = cached.TypeConfidence,
Title = cached.Title,
Chunks = 0,
Characters = cached.Text.Length,
Cached = true
};
}
var classification = await _classifier.ClassifyAsync(text, documentType, title, ct);
var chunks = _chunker.Chunk(text, _settings.ChunkSize, _settings.ChunkOverlap);
var document = new RagDocumentRecord
{
Id = Guid.NewGuid().ToString("N"),
DocumentType = classification.DocumentType,
Title = classification.Title,
SourceUrl = sourceUrl,
Text = text,
TextHash = textHash,
TypeConfidence = classification.Confidence,
MetadataJson = JsonSerializer.Serialize(metadata ?? classification.Metadata),
CreatedAt = DateTimeOffset.UtcNow
};
var records = new List<RagChunkRecord>();
for (var i = 0; i < chunks.Count; i++)
{
ct.ThrowIfCancellationRequested();
records.Add(new RagChunkRecord
{
Id = Guid.NewGuid().ToString("N"),
DocumentId = document.Id,
ChunkIndex = i,
Text = chunks[i],
Embedding = await _ai.CreateEmbeddingAsync(chunks[i], ct)
});
}
await _repository.SaveDocumentAsync(document, records, ct);
return new IndexDocumentResponse
{
DocumentId = document.Id,
TextHash = document.TextHash,
DocumentType = document.DocumentType,
DocumentTypeConfidence = document.TypeConfidence,
Title = document.Title,
Chunks = records.Count,
Characters = text.Length,
Cached = false
};
}
}
+116
View File
@@ -0,0 +1,116 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using Api.Services.Contracts;
using Api.Settings;
namespace Api.Services;
public sealed class RawAiClient : IAiClient
{
private readonly HttpClient _http;
private readonly AiSettings _settings;
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
public RawAiClient(HttpClient http, IOptions<AiSettings> options)
{
_http = http;
_settings = options.Value;
}
public async Task<float[]> CreateEmbeddingAsync(string input, CancellationToken ct)
{
return IsOllama() ? await CreateOllamaEmbeddingAsync(input, ct) : await CreateOpenAiEmbeddingAsync(input, ct);
}
public async Task<string> CreateChatCompletionAsync(string systemPrompt, string userPrompt, decimal temperature, CancellationToken ct)
{
return IsOllama()
? await CreateOllamaChatCompletionAsync(systemPrompt, userPrompt, temperature, ct)
: await CreateOpenAiChatCompletionAsync(systemPrompt, userPrompt, temperature, ct);
}
private bool IsOllama() => string.Equals(_settings.Provider, "Ollama", StringComparison.OrdinalIgnoreCase);
private async Task<float[]> CreateOpenAiEmbeddingAsync(string input, 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/embeddings");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _settings.OpenAI.ApiKey);
request.Content = ToJson(new { model = _settings.OpenAI.EmbeddingModel, input });
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 embeddings failed: {(int)response.StatusCode} {json}");
using var doc = JsonDocument.Parse(json);
return doc.RootElement.GetProperty("data")[0].GetProperty("embedding").EnumerateArray().Select(x => x.GetSingle()).ToArray();
}
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<float[]> CreateOllamaEmbeddingAsync(string input, 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/embeddings", ToJson(new { model = _settings.Ollama.EmbeddingModel, prompt = input }), cts.Token);
var json = await response.Content.ReadAsStringAsync(cts.Token);
if (!response.IsSuccessStatusCode) throw new InvalidOperationException($"Ollama embeddings failed: {(int)response.StatusCode} {json}");
using var doc = JsonDocument.Parse(json);
return doc.RootElement.GetProperty("embedding").EnumerateArray().Select(x => x.GetSingle()).ToArray();
}
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");
}
+238
View File
@@ -0,0 +1,238 @@
using Microsoft.Data.SqlClient;
using Api.Services.Contracts;
using Api.Services.Contracts.Models;
namespace Api.Services;
public sealed class SqlRagRepository : IRagRepository
{
private readonly string _connectionString;
public SqlRagRepository(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("RagDb")
?? throw new InvalidOperationException("Connection string 'RagDb' is missing.");
}
public async Task InitializeAsync(CancellationToken ct)
{
await EnsureDatabaseExistsAsync(ct);
var sql = await File.ReadAllTextAsync(Path.Combine(AppContext.BaseDirectory, "Database", "schema.sql"), ct);
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
foreach (var commandText in sql.Split("GO", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
await using var command = new SqlCommand(commandText, connection);
await command.ExecuteNonQueryAsync(ct);
}
}
public async Task<RagDocumentRecord?> GetDocumentByTextHashAsync(string textHash, string? sourceUrl, CancellationToken ct)
{
const string sql = """
SELECT TOP 1 Id, DocumentType, Title, SourceUrl, RawText, TextHash, TypeConfidence, MetadataJson, CreatedAt
FROM RagDocuments
WHERE TextHash = @TextHash AND (@SourceUrl IS NULL OR SourceUrl = @SourceUrl)
ORDER BY CreatedAt DESC
""";
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@TextHash", textHash);
command.Parameters.AddWithValue("@SourceUrl", (object?)sourceUrl ?? DBNull.Value);
await using var reader = await command.ExecuteReaderAsync(ct);
return await reader.ReadAsync(ct) ? ReadDocument(reader) : null;
}
public async Task<RagDocumentRecord?> GetDocumentByIdAsync(string id, CancellationToken ct)
{
const string sql = """
SELECT Id, DocumentType, Title, SourceUrl, RawText, TextHash, TypeConfidence, MetadataJson, CreatedAt
FROM RagDocuments
WHERE Id = @Id
""";
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@Id", id);
await using var reader = await command.ExecuteReaderAsync(ct);
return await reader.ReadAsync(ct) ? ReadDocument(reader) : null;
}
public async Task SaveDocumentAsync(RagDocumentRecord document, IReadOnlyList<RagChunkRecord> chunks, CancellationToken ct)
{
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var tx = (SqlTransaction)await connection.BeginTransactionAsync(ct);
try
{
const string insertDoc = """
INSERT INTO RagDocuments (Id, DocumentType, Title, SourceUrl, RawText, TextHash, TypeConfidence, MetadataJson, CreatedAt)
VALUES (@Id, @DocumentType, @Title, @SourceUrl, @RawText, @TextHash, @TypeConfidence, @MetadataJson, @CreatedAt)
""";
await using (var command = new SqlCommand(insertDoc, connection, tx))
{
command.Parameters.AddWithValue("@Id", document.Id);
command.Parameters.AddWithValue("@DocumentType", document.DocumentType);
command.Parameters.AddWithValue("@Title", document.Title);
command.Parameters.AddWithValue("@SourceUrl", (object?)document.SourceUrl ?? DBNull.Value);
command.Parameters.AddWithValue("@RawText", document.Text);
command.Parameters.AddWithValue("@TextHash", document.TextHash);
command.Parameters.AddWithValue("@TypeConfidence", document.TypeConfidence);
command.Parameters.AddWithValue("@MetadataJson", document.MetadataJson);
command.Parameters.AddWithValue("@CreatedAt", document.CreatedAt.UtcDateTime);
await command.ExecuteNonQueryAsync(ct);
}
const string insertChunk = """
INSERT INTO RagChunks (Id, DocumentId, ChunkIndex, Text, Embedding)
VALUES (@Id, @DocumentId, @ChunkIndex, @Text, @Embedding)
""";
foreach (var chunk in chunks)
{
await using var command = new SqlCommand(insertChunk, connection, tx);
command.Parameters.AddWithValue("@Id", chunk.Id);
command.Parameters.AddWithValue("@DocumentId", document.Id);
command.Parameters.AddWithValue("@ChunkIndex", chunk.ChunkIndex);
command.Parameters.AddWithValue("@Text", chunk.Text);
command.Parameters.AddWithValue("@Embedding", VectorSerializer.ToBytes(chunk.Embedding));
await command.ExecuteNonQueryAsync(ct);
}
await tx.CommitAsync(ct);
}
catch
{
await tx.RollbackAsync(ct);
throw;
}
}
public async Task<IReadOnlyList<SearchCandidateChunk>> SearchChunksAsync(float[] queryEmbedding, IReadOnlyList<string>? targetTypes, int topK, CancellationToken ct)
{
var types = targetTypes?.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim().ToLowerInvariant()).Distinct().ToArray() ?? [];
var sql = """
SELECT d.Id, d.DocumentType, d.Title, d.SourceUrl, d.RawText, d.TextHash, d.TypeConfidence, d.MetadataJson, d.CreatedAt,
c.Id, c.DocumentId, c.ChunkIndex, c.Text, c.Embedding
FROM RagChunks c
INNER JOIN RagDocuments d ON d.Id = c.DocumentId
""";
if (types.Length > 0)
{
sql += " WHERE LOWER(d.DocumentType) IN (" + string.Join(',', types.Select((_, i) => $"@Type{i}")) + ")";
}
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
for (var i = 0; i < types.Length; i++) command.Parameters.AddWithValue($"@Type{i}", types[i]);
await using var reader = await command.ExecuteReaderAsync(ct);
var candidates = new List<SearchCandidateChunk>();
while (await reader.ReadAsync(ct))
{
var doc = ReadDocument(reader, 0);
var chunk = new RagChunkRecord
{
Id = reader.GetString(9),
DocumentId = reader.GetString(10),
ChunkIndex = reader.GetInt32(11),
Text = reader.GetString(12),
Embedding = VectorSerializer.FromBytes((byte[])reader[13])
};
candidates.Add(new SearchCandidateChunk
{
Document = doc,
Chunk = chunk,
Score = VectorSerializer.CosineSimilarity(queryEmbedding, chunk.Embedding)
});
}
return candidates
.OrderByDescending(x => x.Score)
.Take(Math.Max(topK * 4, topK))
.ToList();
}
public async Task<float[]?> GetEmbeddingAsync(string cacheKey, CancellationToken ct)
{
const string sql = "SELECT Vector FROM RagEmbeddingCache WHERE CacheKey = @CacheKey";
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@CacheKey", cacheKey);
var value = await command.ExecuteScalarAsync(ct);
return value is byte[] bytes ? VectorSerializer.FromBytes(bytes) : null;
}
public async Task SaveEmbeddingAsync(string cacheKey, string model, string textHash, float[] vector, CancellationToken ct)
{
const string sql = """
IF NOT EXISTS (SELECT 1 FROM RagEmbeddingCache WHERE CacheKey = @CacheKey)
INSERT INTO RagEmbeddingCache (CacheKey, Model, TextHash, Vector, CreatedAt)
VALUES (@CacheKey, @Model, @TextHash, @Vector, SYSUTCDATETIME())
""";
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@CacheKey", cacheKey);
command.Parameters.AddWithValue("@Model", model);
command.Parameters.AddWithValue("@TextHash", textHash);
command.Parameters.AddWithValue("@Vector", VectorSerializer.ToBytes(vector));
await command.ExecuteNonQueryAsync(ct);
}
public async Task<string?> GetChatCompletionAsync(string cacheKey, CancellationToken ct)
{
const string sql = "SELECT ResponseText FROM RagChatCompletionCache WHERE CacheKey = @CacheKey";
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@CacheKey", cacheKey);
return await command.ExecuteScalarAsync(ct) as string;
}
public async Task SaveChatCompletionAsync(string cacheKey, string model, decimal temperature, string responseText, CancellationToken ct)
{
const string sql = """
IF NOT EXISTS (SELECT 1 FROM RagChatCompletionCache WHERE CacheKey = @CacheKey)
INSERT INTO RagChatCompletionCache (CacheKey, Model, Temperature, ResponseText, CreatedAt)
VALUES (@CacheKey, @Model, @Temperature, @ResponseText, SYSUTCDATETIME())
""";
await using var connection = new SqlConnection(_connectionString);
await connection.OpenAsync(ct);
await using var command = new SqlCommand(sql, connection);
command.Parameters.AddWithValue("@CacheKey", cacheKey);
command.Parameters.AddWithValue("@Model", model);
command.Parameters.AddWithValue("@Temperature", temperature);
command.Parameters.AddWithValue("@ResponseText", responseText);
await command.ExecuteNonQueryAsync(ct);
}
private static RagDocumentRecord ReadDocument(SqlDataReader reader, int offset = 0) => new()
{
Id = reader.GetString(offset),
DocumentType = reader.GetString(offset + 1),
Title = reader.GetString(offset + 2),
SourceUrl = reader.IsDBNull(offset + 3) ? null : reader.GetString(offset + 3),
Text = reader.GetString(offset + 4),
TextHash = reader.GetString(offset + 5),
TypeConfidence = Convert.ToDouble(reader.GetValue(offset + 6)),
MetadataJson = reader.GetString(offset + 7),
CreatedAt = new DateTimeOffset(reader.GetDateTime(offset + 8), TimeSpan.Zero)
};
private async Task EnsureDatabaseExistsAsync(CancellationToken ct)
{
var builder = new SqlConnectionStringBuilder(_connectionString);
var databaseName = builder.InitialCatalog;
if (string.IsNullOrWhiteSpace(databaseName)) return;
builder.InitialCatalog = "master";
await using var connection = new SqlConnection(builder.ConnectionString);
await connection.OpenAsync(ct);
var safeName = databaseName.Replace("]", "]]" );
await using var command = new SqlCommand($"IF DB_ID(@DatabaseName) IS NULL EXEC('CREATE DATABASE [{safeName}]')", connection);
command.Parameters.AddWithValue("@DatabaseName", databaseName);
await command.ExecuteNonQueryAsync(ct);
}
}
+24
View File
@@ -0,0 +1,24 @@
using Api.Services.Contracts;
namespace Api.Services;
public sealed class TextChunker : ITextChunker
{
public IReadOnlyList<string> Chunk(string text, int chunkSize, int overlap)
{
if (string.IsNullOrWhiteSpace(text)) return [];
chunkSize = Math.Clamp(chunkSize, 300, 3000);
overlap = Math.Clamp(overlap, 0, chunkSize / 2);
var chunks = new List<string>();
var start = 0;
while (start < text.Length)
{
var length = Math.Min(chunkSize, text.Length - start);
var chunk = text.Substring(start, length).Trim();
if (!string.IsNullOrWhiteSpace(chunk)) chunks.Add(chunk);
start += chunkSize - overlap;
}
return chunks;
}
}
+27
View File
@@ -0,0 +1,27 @@
using System.Text;
using Api.Services.Contracts;
using UglyToad.PdfPig;
namespace Api.Services;
public sealed class TextExtractor : ITextExtractor
{
public Task<string> ExtractPdfAsync(Stream stream, CancellationToken ct)
{
using var document = PdfDocument.Open(stream);
var builder = new StringBuilder();
foreach (var page in document.GetPages())
{
ct.ThrowIfCancellationRequested();
builder.AppendLine(page.Text);
builder.AppendLine();
}
return Task.FromResult(Normalize(builder.ToString()));
}
public string Normalize(string value)
{
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
return string.Join(' ', value.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)).Trim();
}
}
+31
View File
@@ -0,0 +1,31 @@
namespace Api.Services;
public static class VectorSerializer
{
public static byte[] ToBytes(float[] vector)
{
var bytes = new byte[vector.Length * sizeof(float)];
Buffer.BlockCopy(vector, 0, bytes, 0, bytes.Length);
return bytes;
}
public static float[] FromBytes(byte[] bytes)
{
var vector = new float[bytes.Length / sizeof(float)];
Buffer.BlockCopy(bytes, 0, vector, 0, bytes.Length);
return vector;
}
public static double CosineSimilarity(float[] a, float[] b)
{
if (a.Length == 0 || a.Length != b.Length) return 0;
double dot = 0, magA = 0, magB = 0;
for (var i = 0; i < a.Length; i++)
{
dot += a[i] * b[i];
magA += a[i] * a[i];
magB += b[i] * b[i];
}
return magA == 0 || magB == 0 ? 0 : dot / (Math.Sqrt(magA) * Math.Sqrt(magB));
}
}