[FA-misc] Translation engine work
Some checks failed
CI / build-backend (pull_request) Failing after 1m48s
CI / build-frontend (pull_request) Successful in 1m27s

This commit is contained in:
gamer147
2026-08-25 10:38:27 -04:00
parent 327c03c098
commit d5a9529d6f
14 changed files with 641 additions and 19 deletions

View File

@@ -9,6 +9,10 @@ using FictionArchive.Service.TranslationService.Services;
using FictionArchive.Service.TranslationService.Services.Database;
using FictionArchive.Service.TranslationService.Services.TranslationEngines;
using FictionArchive.Service.TranslationService.Services.TranslationEngines.DeepLTranslate;
using System.Net.Http.Headers;
using FictionArchive.Service.TranslationService.Services.Resilience;
using FictionArchive.Service.TranslationService.Services.TranslationEngines.NanoGpt;
using Polly;
namespace FictionArchive.Service.TranslationService;
@@ -55,12 +59,44 @@ public class Program
#region Translation Adapter
builder.Services.AddTransient<DeepLClient>(provider =>
{
return new DeepLClient(builder.Configuration["DeepL:ApiKey"]);
});
// Shared resilience pipeline used by both DeepL and NanoGPT engines.
builder.Services.AddSingleton(TranslationResiliencePipeline.Build());
// DeepL
builder.Services.AddTransient<DeepLClient>(_ => new DeepLClient(builder.Configuration["DeepL:ApiKey"]));
builder.Services.AddTransient<ITranslationEngine, DeepLTranslationEngine>();
// NanoGPT — typed HttpClient (bearer auth + base address + 120s timeout).
builder.Services.AddHttpClient<NanoGptApiClient>(client =>
{
var baseAddress = builder.Configuration["NanoGpt:BaseAddress"];
if (!string.IsNullOrWhiteSpace(baseAddress))
{
client.BaseAddress = new Uri(baseAddress);
}
var apiKey = builder.Configuration["NanoGpt:ApiKey"];
if (!string.IsNullOrWhiteSpace(apiKey) && apiKey != "REPLACE_ME")
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
}
client.Timeout = TimeSpan.FromSeconds(120);
});
// Register one ITranslationEngine per configured NanoGPT model.
var nanoGptModels = builder.Configuration
.GetSection("NanoGpt:Models")
.Get<NanoGptModelOptions[]>() ?? [];
foreach (var modelOptions in nanoGptModels)
{
builder.Services.AddTransient<ITranslationEngine>(sp => new NanoGptTranslationEngine(
sp.GetRequiredService<NanoGptApiClient>(),
modelOptions,
sp.GetRequiredService<ResiliencePipeline>(),
sp.GetRequiredService<ILogger<NanoGptTranslationEngine>>()));
}
builder.Services.AddTransient<TranslationEngineService>();
#endregion