| | | 1 | | using System.ClientModel; |
| | | 2 | | using System.ClientModel.Primitives; |
| | | 3 | | using EHonda.KicktippAi.Core; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 6 | | using Microsoft.Extensions.Configuration; |
| | | 7 | | using Microsoft.Extensions.FileProviders; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using OpenAI.Responses; |
| | | 10 | | |
| | | 11 | | namespace OpenAiIntegration; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Extension methods for configuring OpenAI services in dependency injection |
| | | 15 | | /// </summary> |
| | | 16 | | public static class ServiceCollectionExtensions |
| | | 17 | | { |
| | 1 | 18 | | private static readonly TimeSpan OpenAiNetworkTimeout = TimeSpan.FromMinutes(15); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Adds OpenAI predictor services to the service collection |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="services">The service collection</param> |
| | | 24 | | /// <param name="apiKey">The OpenAI API key</param> |
| | | 25 | | /// <param name="model">The OpenAI model to use (defaults to gpt-4o-mini)</param> |
| | | 26 | | /// <returns>The service collection for chaining</returns> |
| | | 27 | | public static IServiceCollection AddOpenAiPredictor( |
| | | 28 | | this IServiceCollection services, |
| | | 29 | | string apiKey, |
| | | 30 | | string model = "gpt-4o-mini") |
| | | 31 | | { |
| | 1 | 32 | | return services.AddOpenAiPredictor(apiKey, model, options: null); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Adds OpenAI predictor services to the service collection |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="services">The service collection</param> |
| | | 39 | | /// <param name="apiKey">The OpenAI API key</param> |
| | | 40 | | /// <param name="options">The prediction service options</param> |
| | | 41 | | /// <returns>The service collection for chaining</returns> |
| | | 42 | | public static IServiceCollection AddOpenAiPredictor( |
| | | 43 | | this IServiceCollection services, |
| | | 44 | | string apiKey, |
| | | 45 | | PredictionServiceOptions? options) |
| | | 46 | | { |
| | 0 | 47 | | return services.AddOpenAiPredictor(apiKey, "gpt-4o-mini", options); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Adds OpenAI predictor services to the service collection |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="services">The service collection</param> |
| | | 54 | | /// <param name="apiKey">The OpenAI API key</param> |
| | | 55 | | /// <param name="model">The OpenAI model to use</param> |
| | | 56 | | /// <param name="options">The prediction service options</param> |
| | | 57 | | /// <returns>The service collection for chaining</returns> |
| | | 58 | | public static IServiceCollection AddOpenAiPredictor( |
| | | 59 | | this IServiceCollection services, |
| | | 60 | | string apiKey, |
| | | 61 | | string model, |
| | | 62 | | PredictionServiceOptions? options) |
| | | 63 | | { |
| | 1 | 64 | | if (string.IsNullOrWhiteSpace(apiKey)) |
| | | 65 | | { |
| | 1 | 66 | | throw new ArgumentException("OpenAI API key cannot be null or empty", nameof(apiKey)); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | // Register the ResponsesClient as a singleton |
| | 1 | 70 | | services.TryAddSingleton<ResponsesClient>(serviceProvider => |
| | 1 | 71 | | { |
| | 1 | 72 | | return new ResponsesClient( |
| | 1 | 73 | | new ApiKeyCredential(apiKey), |
| | 1 | 74 | | new ResponsesClientOptions |
| | 1 | 75 | | { |
| | 1 | 76 | | NetworkTimeout = OpenAiNetworkTimeout, |
| | 1 | 77 | | RetryPolicy = new ClientRetryPolicy(maxRetries: 0) |
| | 1 | 78 | | }); |
| | 1 | 79 | | }); |
| | | 80 | | |
| | | 81 | | // Register the predictor context |
| | 1 | 82 | | services.TryAddScoped<PredictorContext>(_ => PredictorContext.CreateBasic()); |
| | | 83 | | |
| | | 84 | | // Register the predictor implementation |
| | 1 | 85 | | services.TryAddScoped<IPredictor<PredictorContext>>(serviceProvider => |
| | 0 | 86 | | new OpenAiPredictor( |
| | 0 | 87 | | serviceProvider.GetRequiredService<ResponsesClient>(), |
| | 0 | 88 | | serviceProvider.GetRequiredService<ILogger<OpenAiPredictor>>(), |
| | 0 | 89 | | model)); |
| | | 90 | | |
| | | 91 | | // Register the cost calculation service |
| | 1 | 92 | | services.TryAddScoped<ICostCalculationService, CostCalculationService>(); |
| | | 93 | | |
| | | 94 | | // Register the file provider for prompts |
| | 1 | 95 | | services.TryAddSingleton(PromptsFileProvider.Create()); |
| | | 96 | | |
| | | 97 | | // Register the instructions template provider |
| | 1 | 98 | | services.TryAddSingleton<IInstructionsTemplateProvider, InstructionsTemplateProvider>(); |
| | | 99 | | |
| | 1 | 100 | | services.TryAddScoped<IMatchPromptReconstructionService, MatchPromptReconstructionService>(); |
| | | 101 | | |
| | | 102 | | // Register the token usage tracker as singleton (to accumulate across requests) |
| | 1 | 103 | | services.TryAddSingleton<ITokenUsageTracker>(serviceProvider => |
| | 1 | 104 | | new TokenUsageTracker( |
| | 1 | 105 | | serviceProvider.GetRequiredService<ILogger<TokenUsageTracker>>(), |
| | 1 | 106 | | serviceProvider.GetRequiredService<ICostCalculationService>())); |
| | | 107 | | |
| | | 108 | | // Register the prediction service with model parameter |
| | 1 | 109 | | services.TryAddScoped<IPredictionService>(serviceProvider => |
| | 1 | 110 | | new PredictionService( |
| | 1 | 111 | | serviceProvider.GetRequiredService<ResponsesClient>(), |
| | 1 | 112 | | serviceProvider.GetRequiredService<ILogger<PredictionService>>(), |
| | 1 | 113 | | serviceProvider.GetRequiredService<ICostCalculationService>(), |
| | 1 | 114 | | serviceProvider.GetRequiredService<ITokenUsageTracker>(), |
| | 1 | 115 | | serviceProvider.GetRequiredService<IInstructionsTemplateProvider>(), |
| | 1 | 116 | | model, |
| | 1 | 117 | | options)); |
| | | 118 | | |
| | 1 | 119 | | return services; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Adds OpenAI predictor services to the service collection using configuration |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="services">The service collection</param> |
| | | 126 | | /// <param name="configuration">The configuration containing OpenAI settings</param> |
| | | 127 | | /// <returns>The service collection for chaining</returns> |
| | | 128 | | public static IServiceCollection AddOpenAiPredictor( |
| | | 129 | | this IServiceCollection services, |
| | | 130 | | IConfiguration configuration) |
| | | 131 | | { |
| | 1 | 132 | | return services.AddOpenAiPredictor(configuration, options: null); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Adds OpenAI predictor services to the service collection using configuration |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="services">The service collection</param> |
| | | 139 | | /// <param name="configuration">The configuration containing OpenAI settings</param> |
| | | 140 | | /// <param name="options">The prediction service options</param> |
| | | 141 | | /// <returns>The service collection for chaining</returns> |
| | | 142 | | public static IServiceCollection AddOpenAiPredictor( |
| | | 143 | | this IServiceCollection services, |
| | | 144 | | IConfiguration configuration, |
| | | 145 | | PredictionServiceOptions? options) |
| | | 146 | | { |
| | 1 | 147 | | var apiKey = configuration["OPENAI_API_KEY"] ?? |
| | 1 | 148 | | Environment.GetEnvironmentVariable("OPENAI_API_KEY"); |
| | 1 | 149 | | var model = configuration["OPENAI_MODEL"] ?? "gpt-4o-mini"; |
| | | 150 | | |
| | 1 | 151 | | return services.AddOpenAiPredictor(apiKey!, model, options); |
| | | 152 | | } |
| | | 153 | | } |