Clamp AI summarize lengths for CV rewrite

This commit is contained in:
2026-04-11 21:55:51 +02:00
parent 534534b333
commit 591c9b8a64
2 changed files with 62 additions and 2 deletions
+13 -2
View File
@@ -76,6 +76,10 @@ namespace JobTrackerApi.Services
public class SummarizerService : ISummarizerService
{
private const int AiSummarizeMaxInputChars = 20000;
private const int AiServiceMaxSummaryLength = 256;
private const int AiServiceMaxMinLength = 180;
private const int AiServiceMinSummaryLength = 24;
private const int AiServiceMinMinLength = 8;
private readonly IHttpClientFactory _httpFactory;
private readonly IMemoryCache _cache;
private readonly object _metricsLock = new();
@@ -172,7 +176,14 @@ namespace JobTrackerApi.Services
private async Task<string?> SummarizeCoreAsync(string text, int maxLength, int minLength)
{
var key = BuildCacheKey(text, maxLength, minLength);
var normalizedMaxLength = Math.Clamp(maxLength, AiServiceMinSummaryLength, AiServiceMaxSummaryLength);
var normalizedMinLength = Math.Clamp(minLength, AiServiceMinMinLength, AiServiceMaxMinLength);
if (normalizedMinLength >= normalizedMaxLength)
{
normalizedMinLength = Math.Max(AiServiceMinMinLength, normalizedMaxLength - 1);
}
var key = BuildCacheKey(text, normalizedMaxLength, normalizedMinLength);
Interlocked.Increment(ref _requests);
if (_cache.TryGetValue<string>(key, out var cached))
@@ -189,7 +200,7 @@ namespace JobTrackerApi.Services
Interlocked.Increment(ref _cacheMisses);
var client = _httpFactory.CreateClient("ai-service");
var payload = JsonSerializer.Serialize(new { text, max_length = maxLength, min_length = minLength });
var payload = JsonSerializer.Serialize(new { text, max_length = normalizedMaxLength, min_length = normalizedMinLength });
using var content = new StringContent(payload, Encoding.UTF8, "application/json");
var sw = Stopwatch.StartNew();