diff --git a/global.json b/global.json index 570baa6..cca5e9a 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.102", + "version": "10.0.100", "rollForward": "latestPatch", "allowPrerelease": false } diff --git a/src/PoVicTranslate.Web/Program.cs b/src/PoVicTranslate.Web/Program.cs index 2e2ebcf..5cc9b55 100644 --- a/src/PoVicTranslate.Web/Program.cs +++ b/src/PoVicTranslate.Web/Program.cs @@ -116,20 +116,20 @@ builder.Services.Configure(builder.Configuration.GetSection("ApiSettings")); // Register core services -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); // Register validation services builder.Services.AddSingleton(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); // Register diagnostic validators -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); // Register utility services builder.Services.AddSingleton(); diff --git a/src/PoVicTranslate.Web/Services/TranslationService.cs b/src/PoVicTranslate.Web/Services/TranslationService.cs index 8f7920e..e52a967 100644 --- a/src/PoVicTranslate.Web/Services/TranslationService.cs +++ b/src/PoVicTranslate.Web/Services/TranslationService.cs @@ -2,6 +2,7 @@ using Azure.AI.OpenAI; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using OpenAI.Chat; using PoVicTranslate.Web.Configuration; @@ -27,10 +28,20 @@ Maintain the sentiment and intent of the original text. Do not add any explanations, only provide the translated text. """; + [ActivatorUtilitiesConstructor] public TranslationService( IOptions apiSettings, TelemetryClient telemetryClient, ILogger logger) + : this(apiSettings, telemetryClient, logger, null) + { + } + + public TranslationService( + IOptions apiSettings, + TelemetryClient telemetryClient, + ILogger logger, + ChatClient? chatClient) { ArgumentNullException.ThrowIfNull(apiSettings); _telemetryClient = telemetryClient; @@ -39,6 +50,13 @@ public TranslationService( var settings = apiSettings.Value; _deploymentName = settings.AzureOpenAIDeploymentName; + if (chatClient is not null) + { + _chatClient = chatClient; + _logger.LogInformation("TranslationService initialized with injected ChatClient"); + return; + } + if (string.IsNullOrWhiteSpace(settings.AzureOpenAIApiKey) || string.IsNullOrWhiteSpace(settings.AzureOpenAIEndpoint) || string.IsNullOrWhiteSpace(settings.AzureOpenAIDeploymentName))