| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using GistBackend.Exceptions; |
| | | 3 | | using GistBackend.Types; |
| | | 4 | | using GistBackend.Utils; |
| | | 5 | | using Microsoft.Extensions.Caching.Memory; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | using SharpToken; |
| | | 8 | | |
| | | 9 | | namespace GistBackend.Handlers.AIHandler; |
| | | 10 | | |
| | | 11 | | public record SummarizeRequest(string Title, string Article, string Language); |
| | | 12 | | |
| | | 13 | | public record SummaryForRecap(string Title, string Summary, int Id); |
| | | 14 | | |
| | | 15 | | public record RecapRequest(List<SummaryForRecap> Summaries, string RecapType); |
| | | 16 | | |
| | | 17 | | public interface IAIHandler |
| | | 18 | | { |
| | | 19 | | public Task<float[]> GenerateEmbeddingAsync(string input, CancellationToken ct); |
| | | 20 | | public Task<SummaryAIResponse> GenerateSummaryAIResponseAsync(Language feedLanguage, string title, string article, |
| | | 21 | | CancellationToken ct); |
| | | 22 | | public Task<RecapAIResponse> GenerateDailyRecapAsync(IEnumerable<ConstructedGist> gists, CancellationToken ct); |
| | | 23 | | public Task<RecapAIResponse> GenerateWeeklyRecapAsync(IEnumerable<ConstructedGist> gists, CancellationToken ct); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public class AIHandler : IAIHandler |
| | | 27 | | { |
| | | 28 | | private readonly IEmbeddingClientHandler _embeddingClientHandler; |
| | | 29 | | private readonly HttpClient _httpClient; |
| | | 30 | | private readonly GptEncoding _encoding; |
| | | 31 | | private readonly IMemoryCache _embeddingCache; |
| | | 32 | | |
| | 1 | 33 | | private static readonly MemoryCacheEntryOptions CacheEntryOptions = new MemoryCacheEntryOptions() |
| | 1 | 34 | | .SetSlidingExpiration(TimeSpan.FromHours(1)) |
| | 1 | 35 | | .SetAbsoluteExpiration(TimeSpan.FromHours(24)); |
| | | 36 | | |
| | 5 | 37 | | public AIHandler(IEmbeddingClientHandler embeddingClientHandler, HttpClient httpClient, |
| | 5 | 38 | | IOptions<AIHandlerOptions> options, IMemoryCache embeddingCache) |
| | | 39 | | { |
| | 5 | 40 | | _embeddingClientHandler = embeddingClientHandler; |
| | 5 | 41 | | _encoding = GptEncoding.GetEncodingForModel(embeddingClientHandler.Model); |
| | 5 | 42 | | _httpClient = httpClient; |
| | 5 | 43 | | _httpClient.BaseAddress = new Uri(options.Value.Host); |
| | 5 | 44 | | _embeddingCache = embeddingCache; |
| | 5 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<float[]> GenerateEmbeddingAsync(string input, CancellationToken ct) |
| | | 48 | | { |
| | | 49 | | // Tokenize and truncate to stay under 8k context; keep a small buffer for prompts. |
| | | 50 | | const int maxTokens = 7500; |
| | 5 | 51 | | var tokens = _encoding.Encode(input); |
| | 5 | 52 | | var safeInput = tokens.Count > maxTokens ? _encoding.Decode(tokens.Take(maxTokens)) : input; |
| | | 53 | | |
| | 5 | 54 | | var cacheKey = $"embedding:{safeInput}"; |
| | 5 | 55 | | var result = await _embeddingCache.GetOrCreateAsync(cacheKey, async entry => |
| | 5 | 56 | | { |
| | 5 | 57 | | entry.SetOptions(CacheEntryOptions); |
| | 5 | 58 | | return await _embeddingClientHandler.GenerateEmbeddingAsync(safeInput, ct); |
| | 10 | 59 | | }); |
| | | 60 | | |
| | 5 | 61 | | return result!; |
| | 5 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<SummaryAIResponse> GenerateSummaryAIResponseAsync(Language feedLanguage, string title, |
| | | 65 | | string article, CancellationToken ct) |
| | | 66 | | { |
| | 0 | 67 | | var request = new SummarizeRequest(title, article, feedLanguage.ToString()); |
| | 0 | 68 | | var response = await _httpClient.PostAsJsonAsync("/summarize", request, SerializerDefaults.JsonOptions, ct); |
| | 0 | 69 | | if (!response.IsSuccessStatusCode) |
| | | 70 | | { |
| | 0 | 71 | | throw new ExternalServiceException($"Failed to get summary from AI API: {response.StatusCode}"); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | var aiResponse = |
| | 0 | 75 | | await response.Content.ReadFromJsonAsync<SummaryAIResponse>(cancellationToken: ct, |
| | 0 | 76 | | options: SerializerDefaults.JsonOptions); |
| | 0 | 77 | | return aiResponse ?? throw new ExternalServiceException("Could not parse summary AI response"); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public Task<RecapAIResponse> GenerateDailyRecapAsync(IEnumerable<ConstructedGist> gists, CancellationToken ct) => |
| | 0 | 81 | | GenerateRecapAsync(gists, RecapType.Daily, ct); |
| | | 82 | | |
| | | 83 | | public Task<RecapAIResponse> GenerateWeeklyRecapAsync(IEnumerable<ConstructedGist> gists, CancellationToken ct) => |
| | 0 | 84 | | GenerateRecapAsync(gists, RecapType.Weekly, ct); |
| | | 85 | | |
| | | 86 | | private async Task<RecapAIResponse> GenerateRecapAsync(IEnumerable<ConstructedGist> gists, RecapType recapType, |
| | | 87 | | CancellationToken ct) |
| | | 88 | | { |
| | 0 | 89 | | var summaries = gists.Select(gist => new SummaryForRecap(gist.Title, gist.Summary, gist.Id)).ToList(); |
| | 0 | 90 | | var request = new RecapRequest(summaries, recapType.ToString()); |
| | 0 | 91 | | var response = await _httpClient.PostAsJsonAsync("/recap", request, SerializerDefaults.JsonOptions, ct); |
| | 0 | 92 | | if (!response.IsSuccessStatusCode) |
| | | 93 | | { |
| | 0 | 94 | | throw new ExternalServiceException($"Failed to get recap from AI API: {response.StatusCode}"); |
| | | 95 | | } |
| | 0 | 96 | | var aiResponse = |
| | 0 | 97 | | await response.Content.ReadFromJsonAsync<RecapAIResponse>(cancellationToken: ct, |
| | 0 | 98 | | options: SerializerDefaults.JsonOptions); |
| | 0 | 99 | | return aiResponse ?? throw new ExternalServiceException("Could not parse recap AI response"); |
| | 0 | 100 | | } |
| | | 101 | | } |