| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.ClientModel; |
| | | 3 | | using System.ClientModel.Primitives; |
| | | 4 | | using System.Net.Http; |
| | | 5 | | using EHonda.KicktippAi.Core; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using OpenAI.Responses; |
| | | 8 | | using OpenAiIntegration; |
| | | 9 | | using Orchestrator.Infrastructure; |
| | | 10 | | |
| | | 11 | | namespace Orchestrator.Infrastructure.Factories; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Default implementation of <see cref="IOpenAiServiceFactory"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// Reads the API key from OPENAI_API_KEY environment variable. |
| | | 18 | | /// Caches services by model to avoid recreating them for repeated requests. |
| | | 19 | | /// The <see cref="ITokenUsageTracker"/> is shared across all prediction services. |
| | | 20 | | /// </remarks> |
| | | 21 | | public sealed class OpenAiServiceFactory : IOpenAiServiceFactory |
| | | 22 | | { |
| | 1 | 23 | | private static readonly TimeSpan OpenAiNetworkTimeout = TimeSpan.FromMinutes(15); |
| | | 24 | | |
| | | 25 | | private readonly ILoggerFactory _loggerFactory; |
| | | 26 | | private readonly IHttpClientFactory? _httpClientFactory; |
| | | 27 | | private readonly Lazy<string> _apiKey; |
| | 1 | 28 | | private readonly ConcurrentDictionary<string, IPredictionService> _predictionServiceCache = new(); |
| | | 29 | | private ITokenUsageTracker? _tokenUsageTracker; |
| | | 30 | | private ICostCalculationService? _costCalculationService; |
| | | 31 | | private IInstructionsTemplateProvider? _instructionsTemplateProvider; |
| | 1 | 32 | | private readonly object _lock = new(); |
| | | 33 | | |
| | 1 | 34 | | public OpenAiServiceFactory(ILoggerFactory loggerFactory, IHttpClientFactory? httpClientFactory = null) |
| | | 35 | | { |
| | 1 | 36 | | _loggerFactory = loggerFactory; |
| | 1 | 37 | | _httpClientFactory = httpClientFactory; |
| | 1 | 38 | | _apiKey = new Lazy<string>(GetApiKeyFromEnvironment); |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | public IPredictionService CreatePredictionService(string model) |
| | | 43 | | { |
| | 1 | 44 | | return CreatePredictionService(model, PredictionServiceOptions.Default); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public IPredictionService CreatePredictionService(string model, PredictionServiceOptions options) |
| | | 49 | | { |
| | 1 | 50 | | ArgumentException.ThrowIfNullOrWhiteSpace(model); |
| | 1 | 51 | | ArgumentNullException.ThrowIfNull(options); |
| | | 52 | | |
| | 1 | 53 | | var apiKey = _apiKey.Value; |
| | 1 | 54 | | var reasoningEffort = string.IsNullOrWhiteSpace(options.ReasoningEffort) |
| | 1 | 55 | | ? string.Empty |
| | 1 | 56 | | : options.ReasoningEffort.Trim().ToLowerInvariant(); |
| | 1 | 57 | | var cacheKey = $"{model}|disableFlex={options.DisableFlexProcessing}|reasoningEffort={reasoningEffort}"; |
| | | 58 | | |
| | | 59 | | // Cache key includes model to handle different configurations |
| | 1 | 60 | | return _predictionServiceCache.GetOrAdd(cacheKey, _ => |
| | 1 | 61 | | { |
| | 1 | 62 | | return CreatePredictionServiceCore( |
| | 1 | 63 | | model, |
| | 1 | 64 | | options, |
| | 1 | 65 | | GetOrCreateInstructionsTemplateProvider(), |
| | 1 | 66 | | apiKey); |
| | 1 | 67 | | }); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public IPredictionService CreatePredictionService( |
| | | 72 | | string model, |
| | | 73 | | PredictionServiceOptions options, |
| | | 74 | | IInstructionsTemplateProvider templateProvider) |
| | | 75 | | { |
| | 0 | 76 | | ArgumentException.ThrowIfNullOrWhiteSpace(model); |
| | 0 | 77 | | ArgumentNullException.ThrowIfNull(options); |
| | 0 | 78 | | ArgumentNullException.ThrowIfNull(templateProvider); |
| | | 79 | | |
| | 0 | 80 | | return CreatePredictionServiceCore(model, options, templateProvider, _apiKey.Value); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public ITokenUsageTracker GetTokenUsageTracker() |
| | | 85 | | { |
| | 1 | 86 | | if (_tokenUsageTracker == null) |
| | | 87 | | { |
| | 1 | 88 | | lock (_lock) |
| | | 89 | | { |
| | 1 | 90 | | _tokenUsageTracker ??= new TokenUsageTracker( |
| | 1 | 91 | | _loggerFactory.CreateLogger<TokenUsageTracker>(), |
| | 1 | 92 | | GetOrCreateCostCalculationService()); |
| | 1 | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | return _tokenUsageTracker; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static string GetApiKeyFromEnvironment() |
| | | 100 | | { |
| | 1 | 101 | | var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY"); |
| | 1 | 102 | | if (string.IsNullOrWhiteSpace(apiKey)) |
| | | 103 | | { |
| | 1 | 104 | | throw new InvalidOperationException("OPENAI_API_KEY environment variable is required"); |
| | | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | return apiKey; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | private ICostCalculationService GetOrCreateCostCalculationService() |
| | | 111 | | { |
| | 1 | 112 | | if (_costCalculationService == null) |
| | | 113 | | { |
| | 1 | 114 | | lock (_lock) |
| | | 115 | | { |
| | 1 | 116 | | _costCalculationService ??= new CostCalculationService( |
| | 1 | 117 | | _loggerFactory.CreateLogger<CostCalculationService>()); |
| | 1 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 1 | 121 | | return _costCalculationService; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private IInstructionsTemplateProvider GetOrCreateInstructionsTemplateProvider() |
| | | 125 | | { |
| | 1 | 126 | | if (_instructionsTemplateProvider == null) |
| | | 127 | | { |
| | 1 | 128 | | lock (_lock) |
| | | 129 | | { |
| | 1 | 130 | | _instructionsTemplateProvider ??= new InstructionsTemplateProvider( |
| | 1 | 131 | | PromptsFileProvider.Create()); |
| | 1 | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | 1 | 135 | | return _instructionsTemplateProvider; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | private IPredictionService CreatePredictionServiceCore( |
| | | 139 | | string model, |
| | | 140 | | PredictionServiceOptions options, |
| | | 141 | | IInstructionsTemplateProvider templateProvider, |
| | | 142 | | string apiKey) |
| | | 143 | | { |
| | 1 | 144 | | var logger = _loggerFactory.CreateLogger<PredictionService>(); |
| | 1 | 145 | | var responsesClient = new ResponsesClient(new ApiKeyCredential(apiKey), CreateResponsesClientOptions()); |
| | | 146 | | |
| | 1 | 147 | | return new PredictionService( |
| | 1 | 148 | | responsesClient, |
| | 1 | 149 | | logger, |
| | 1 | 150 | | GetOrCreateCostCalculationService(), |
| | 1 | 151 | | GetTokenUsageTracker(), |
| | 1 | 152 | | templateProvider, |
| | 1 | 153 | | model, |
| | 1 | 154 | | options); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private ResponsesClientOptions CreateResponsesClientOptions() |
| | | 158 | | { |
| | 1 | 159 | | var options = new ResponsesClientOptions |
| | 1 | 160 | | { |
| | 1 | 161 | | // OpenAI recommends allowing long-running model requests up to 15 minutes. |
| | 1 | 162 | | // This timeout belongs to the OpenAI client pipeline, while HttpClient.Timeout |
| | 1 | 163 | | // stays infinite so it does not race the .NET HTTP resilience handler. |
| | 1 | 164 | | NetworkTimeout = OpenAiNetworkTimeout, |
| | 1 | 165 | | RetryPolicy = new ClientRetryPolicy(maxRetries: 0) |
| | 1 | 166 | | }; |
| | | 167 | | |
| | 1 | 168 | | if (_httpClientFactory is not null) |
| | | 169 | | { |
| | 0 | 170 | | options.Transport = new HttpClientPipelineTransport( |
| | 0 | 171 | | _httpClientFactory.CreateClient(ServiceRegistrationExtensions.OpenAiHttpClientName)); |
| | | 172 | | } |
| | | 173 | | |
| | 1 | 174 | | return options; |
| | | 175 | | } |
| | | 176 | | } |